spring发QQ邮件


一、今天做发邮件功能测试一直报: authentication failed 的错误。搜索了下资料才知道,一般的企业内部邮箱不需要认证,但是QQ的已经需要认证才能用。原来好像QQ邮件不需要认证,所以网上很多资料都不行。然后多方找资料后找到个,分享出来。具体步骤如下

 写个简单的main发邮件,代码如下

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

public class Email {

    public void send(ApplicationContext ctx) {
        JavaMailSender sender = (JavaMailSender) ctx.getBean("javaMailSender");
        SimpleMailMessage mail = new SimpleMailMessage(); 
        try {
            mail.setTo("222222222@qq.com");// 接受者
            mail.setFrom("111111111@qq.com");// 发送者,
            mail.setSubject("测试主题");// 主题
            mail.setText("发送邮件内容测试");// 邮件内容
            sender.send(mail);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "spring-servlet.xml");
        Email e = new Email();
        e.send(ctx);
    }
}
spring-servlet.xml配置文件(红色部分为邮件发送配置有关的东西)
<?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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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">

    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:email.properties</value>
                <!--要是有多个配置文件,只需在这里继续添加即可 -->
            </list>
        </property>
    </bean>
    <!-- 启用spring mvc 注解 -->
    <context:annotation-config />

    <!-- 设置使用注解的类所在的jar包 -->
    <context:component-scan base-package="mail"></context:component-scan>

    <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${mail.smtp.host}" />
        <property name="port" value="${mail.smtp.port}" />
        <property name="username" value="${mail.smtp.username}" />
        <property name="password" value="${mail.smtp.password}" />
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
                <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
                <prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
                <prop key="mail.smtp.socketFactory.port">${mail.smtp.port}</prop>
                <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
                <prop key="mail.smtp.socketFactory.fallback">false</prop>
            </props>
        </property>
    </bean>
    <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/" p:suffix=".jsp" />
</beans>

 email.properties

mail.smtp.host=smtp.qq.com
mail.smtp.auth=true
mail.smtp.timeout=50000
mail.smtp.username=1111111@qq.com
#这个是QQ邮箱的授权码,获取的方式,打开你的邮箱(设置 -> 账户 -> POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务 ->
POP3/SMTP服务 ,点击开启,短信验证后就会获得授权码)
mail.smtp.password=iklnzunrgdzkcbab mail.smtp.port=465 mail.smtp.starttls.enable=true 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM