通过Java实现从 QQ邮箱向别的邮箱发送邮件

先开启发送人邮箱的POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务,拿QQ邮箱来说明

登录->设置->(下滑)POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务->开启服务

获得授权码

通过Java实现从 QQ邮箱向别的邮箱发送邮件_第1张图片

import com.sun.mail.util.MailSSLSocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 邮箱激活的工具类
 */
public class MailUtil{

    private static Logger logger = LoggerFactory.getLogger(MailUtil.class);

	/**
	 * 发送邮件
	 * @parma email 接收人邮箱
	 * @param userId 接收人注册id
	 * @param code 激活码
	 */
    public static void send(String email, String userId , String code) {
        // 1.创建连接对象javax.mail.Session
        // 2.创建邮件对象 javax.mail.Message
        // 3.发送一封激活邮件
        String from = "[email protected]";// 发件人电子邮箱
        String host = "smtp.qq.com"; // 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易)

        Properties properties = System.getProperties();// 获取系统属性

        properties.setProperty("mail.smtp.host", host);// 设置邮件服务器
        properties.setProperty("mail.smtp.auth", "true");// 打开认证

        try {
            //QQ邮箱需要下面这段代码,163邮箱不需要
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);


            // 1.获取默认session对象,代表邮件服务器的网络连接信息
            //  1.1 使用 getDefalutInstance() 获取的 session 是单例模式下的
            //  1.2 如果要同时发送两份 email 就要使用 getInstance() 获取两个 session 来进行发送
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                // 验证发件人信息, Authenticator是验证信息返回器,会返回用户名和密码
                public PasswordAuthentication getPasswordAuthentication() {
                    // 返回用户名和密码,就这两个信息,主要用于 Transport.send() 发送邮件时
                    return new PasswordAuthentication("[email protected]", "xkxnxxxxeljdga"); // 发件人邮箱账号、授权码
                }
            });
            // 2.创建邮件对象
            Message message = new MimeMessage(session);
            // 2.1 设置发件人
            message.setFrom(new InternetAddress(from));
            // 2.2 设置接收人
            //  2.2.1可以设置多个接收人 message.addRecipient(Message.RecipientType.TO,
            //                                 new Address[]{new InternetAddress(email1)
            //                                 , new InternetAddress(email2)...};
            //  2.2.2 下面是一个接收人的
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
            // 2.3 设置邮件主题
            message.setSubject("账号激活");
            // 2.4 设置邮件内容
            String content = "

这是一封激活邮件,激活请点击以下链接

" + "http://localhost:8081/user/active?userId=" + userId + "&code=" + code + "

"
; // 2.5 设置MIME类型 message.setContent(content, "text/html;charset=UTF-8"); // 3.发送邮件 // 3.1 连接指定的邮件服务器 // 3.2 发送邮件 // 3.3 关闭连接 Transport.send(message); logger.info("email send successfully"); } catch (Exception e) { e.printStackTrace(); } } }

你可能感兴趣的:(Java)