使用Spring Mail发送QQ邮件

摘要:
--启用身份验证--˃true˂!

一、邮箱设置

需要设置独立密码

二、配置文件  spring-mail

<?xml version="1.0" encoding="UTF-8"?>
<beans 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop       
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    default-autowire="no" default-lazy-init="false" >

    <bean id="sender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <!-- 主机(QQ的smtp服务器) -->
        <property name="host" value="smtp.qq.com"/>
        <!-- 邮箱名 -->
        <property name="username" value="90XXXX31@qq.com"/>
        <!-- 独立密码 -->
        <property name="password" value="123456"/>
        <property name="javaMailProperties">
            <props>
                <!-- 开启身份认证 -->
                <prop key="mail.smtp.auth">true</prop>
                <!-- 使用默认端口 -->
                <prop key="mail.smtp.starttls.enable">true</prop>
                <!-- 设置超时 -->
                <prop key="mail.smtp.timeout">25000</prop>
            </props>
        </property>
    </bean>
    
</beans>

三、发送类

package Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import java.util.Date;

public class SpringJavaMailTest {
    public static void main(String[] args) {
        sendMail();
    }

    public static void sendMail() {
        ApplicationContext application = new ClassPathXmlApplicationContext("spring/spring-*.xml");
        JavaMailSenderImpl sender = (JavaMailSenderImpl) application.getBean("sender");
        sender.setDefaultEncoding("UTF-8");
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);
        try {
            helper.setFrom("90XXXX31@qq.com");
            helper.setTo("229XXXX72@qq.com");
            helper.setSentDate(new Date());
            helper.setSubject("邮件主题");
            helper.setText("邮件内容 " + new Date());
            sender.send(message);
            System.out.println("正在发送邮件....");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

免责声明:文章转载自《使用Spring Mail发送QQ邮件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Tarjan求割点Linux运维 -- 文件备份同步系列下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

DEP(数据执行保护)介绍

数据执行保护是一组软件和硬件技术,可以对内存执行额外检查,以帮助防止恶意代码在系统上运行。硬件实现DEP来检测从这些位置运行的代码,并在发现执行时抛出异常。此功能也称为非执行和执行保护。为了与DEP合作,AMD和微软共同设计并开发了AMD的新芯片功能“增强病毒防护”。[1] DEP的安全机制,即“数据执行保护”,是一种Windows安全机制,主要用于防止病毒...

jQuery 时间控件推荐

它支持同时显示多个月日历,标记特殊日期,通过CSS轻松自定义外观,自定义一周的第一天,以及自定义月和周的显示名称。DatePickerDateRangePicker是基于jQueryUI1.6和jQueryUICSS框架开发的日期选择控件。它易于使用,并已测试支持IE6、IE7beta2、Firefox 1.0、Firefox 1.5、Opera 8、Ope...

Android Exception 10(server)' ~ Channel is unrecoverably broken and will be disposed!)

08-1119:22:35.028:W/MemoryDealer(2123):madvise(0x43e1600012288,MADV_REMOVE)返回操作不支持传输端点08-1119:22-35.038:W/InputDispatcher(2714):channel'4236b890com.tongyan.activi...

数据可视化之powerBI技巧(十四)采悟:PowerBI中自制中文单位万和亿

最令人不快的事情之一是数据单元的设置。现在让我们看看如何通过设置测量值来切换单位。需要动态选择1万元和1亿元的单位进行显示。首先,手动创建单位表,然后使用单位表中的[unit]字段生成切片器。下一步是建立销售衡量标准。销售额=总和('订单'[销售额])为了按过滤单位显示销售额,SELECTEDVALUE函数可以根据切片器选择动态更改分母。如果切片器未进行任何...

scan chain的原理和实现——5.UDTP

UDTP(用户定义的测试点)指示DFTC在设计中用户指定的位置插入控制点和观察点。1.为什么使用UDTP?修复不可控的时钟和/或异步输入;增加设计的测试覆盖率;减少模式数量2.UDTP类型① 力0、力1、力01、力z0、力z1、力z01②控制_ 0...

传奇服务端各文件用途说明

传奇外传服务端├数据库服务器│├联系│├美国联邦储备银行│├日志│├! ID列表。txt(付款帐户列表,在Setup.exe中ServiceMode=TRUE时有效)!服务器信息.txt│├DBServer.exe│└DBSrc.ini├登录门│├登录网关.exe│└配置ini├登录服务器│├Chr日志│├ConLog公司│├计数日志│├国际数据库││├ID...