import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;
public class MailTest{
public static void main(String[] args) throws MessagingException, GeneralSecurityException {
Properties props = new Properties();
props.setProperty("mail.debug", "true");// 开启debug调试
props.setProperty("mail.transport.protocol", "smtp");//使用协议:smtp
props.setProperty("mail.smtp.host", "smtp.qq.com");//协议地址
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.auth", "true");//需要授权
//QQ:ssl安全认证
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getInstance(props);
//消息对象
Message msg = new MimeMessage(session);
msg.setSubject("王雪亮的私人博客");
StringBuilder builder = new StringBuilder();
builder.append("url = " + "https://blog.csdn.net/qq_34082113");
builder.append("\n博客地址");
msg.setText(builder.toString());
msg.setFrom(new InternetAddress("***发件人qq邮箱***"));
//发送消息
Transport transport = session.getTransport();
transport.connect("smtp.qq.com", "***发件人qq邮箱***", "***授权码***");
transport.sendMessage(msg, new Address[] { new InternetAddress("***收件人qq邮箱***") });
transport.close();
}
}