JavaMail使用qq邮箱(转)

1.JavaMail jar包

https://java.net/downloads/javamail
我使用的:https://java.net/downloads/javamail/javax.mail.jar

2.邮箱设置

JavaMail使用qq邮箱(转)_第1张图片

3.代码

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

public class SendJavamail {

//发件人的邮箱和密码
public static String myEmailAccount = "发送邮箱@qq.com";
public static String myEmailPassword = "此处为授权码";

//发件人邮箱的SMTP服务器地址,必须准确,不同邮件服务器得知不同,一般格式为: smtp.xxx.com
public static String myEmailSMTPHost = "smtp.qq.com";

//收件人邮箱
public static String receiveMailAccount = "接收邮箱@qq.com";
public static void main(String[] args) throws UnsupportedEncodingException, MessagingException {
    //1.创建参数配置,用于连接右键服务器的参数配置
    **Properties props = new Properties();                  //参数配置
    props.setProperty("mail.transport.protocol", "smtp");   //使用的协议(JavaMail规范要求)
    props.setProperty("mail.host", myEmailSMTPHost);        //发件人邮箱的SMTP服务器地址
    props.setProperty("mail.smtp.auth", "true");            //请求认证,参数名称与具体实现有关
    props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.setProperty("mail.smtp.port", "465");
    props.setProperty("mail.smtp.socketFactory.port", "465");**
    //刚开始总是报邮箱验证错误,然后加了后面3行就可以了(未追究搞懂)
    //2.根据配置创建会话对象,用于和邮件服务器交互
    Session session = Session.getDefaultInstance(props);
    session.setDebug(true);                                 //设置为debug模式,可以查看详细的发送log

    //3.创建一封邮件
    MimeMessage message = createMimeMessage(session, myEmailAccount, receiveMailAccount);

    //4.根据Session获取邮件传输对象
    Transport transport = session.getTransport();

    //5.使用邮箱账号和密码链接邮件服务器
    //  这里认证的邮箱必须与message中的发件人邮箱一致,否则报错
    transport.connect(myEmailAccount, myEmailPassword);

    //6.发送邮件,发到所有的收件地址。 message.getAllRecipients()获取到的实在创建邮件对象时添加的所有收件人,抄送人,密送人
    transport.sendMessage(message, message.getAllRecipients());

    //7.关闭连接
    transport.close();
}

public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail) throws UnsupportedEncodingException, MessagingException{
    //1.创建一封邮件
    MimeMessage message = new MimeMessage(session);

    //2.From:发件人
    message.setFrom(new InternetAddress(sendMail, "发件人","UTF-8"));

    //3.TO:收件人(可以增加多个收件人,抄送,密送)
    message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "收件人","UTF-8"));

    //4.Subject:邮件主题
    message.setSubject("这是标题","UTF-8");

    //5.Content:邮件正文(可以使用html标签)
    List list = VerificationCode();
    message.setContent("
" + ""+list.get(0)+"" + ""+list.get(1)+"" + ""+list.get(2)+"" + ""+list.get(3)+"" +"
","text/html;charset=UTF-8"); //6.设置发件时间 message.setSentDate(new Date()); //7.保存设置 message.saveChanges(); return message; } public static List VerificationCode(){ List list = new ArrayList(); String colorStr = "#"; int num[] = new int[4]; for(int i=0;i

}

你可能感兴趣的:(笔记)