博主也在学习阶段,暂且贴上代码,随后讲解:
package com.test;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.util.MailSSLSocketFactory;
/**
* 测试类
*/
public class TestMail {
public static void main(String[] args) {
if(sendMail("123456@qq.com","code")) {
System.out.println("发送成功!");
}
}
/**
* 发送激活邮件
* @param to 收件人邮箱地址
* @param code 激活码(暂时未完成)
*/
public static boolean sendMail(String to, String code) {
try {
//开启ssl加密
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(