zoukankan      html  css  js  c++  java
  • 使用Spring Mail发送QQ邮件

    一、邮箱设置

    需要设置独立密码

    二、配置文件  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();
            }
        }
    }
  • 相关阅读:
    用Python实现的数据结构与算法:双端队列
    用Python实现的数据结构与算法:队列
    用Python实现的数据结构与算法:堆栈
    用Python实现的数据结构与算法:开篇
    用Markdown写博客
    一个简单的web.py论坛
    在OpenShift上托管web.py应用
    SQLite中的自增关键字:AUTO_INCREMENT、INTEGER PRIMARY KEY与AUTOINCREMENT
    【读书笔记】《HTTP权威指南》:Web Hosting
    【读书笔记】《HTTP权威指南》:Web Robots
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/12104671.html
Copyright © 2011-2022 走看看