- 代码
package com.ainy.demo;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
public class SendQQ {
public static void main(String[] args) throws Exception{
sendMeg("您好,这是一封激活邮箱", "点击激活<a href=\\\"www.baidu.com\\\"></a>","发送人邮箱@163.com");
}
public static void sendMeg(String subject,String content,String sendTo) {
HtmlEmail email = new HtmlEmail();
try {
email.setHostName("smtp.qq.com");
email.setSmtpPort(465);
email.setSSLOnConnect(true);
email.setCharset("utf-8");
email.addTo(sendTo);
email.setFrom("发送者邮箱@qq.com", "(发件人名称)");
email.setAuthentication("发送人邮箱@qq.com", "授权码");
email.setSubject(subject);
email.setMsg(content);
email.send();
System.out.println ( "邮件发送成功!" );
} catch (EmailException e) {
System.out.println ( "邮件发送失败!" );
}
}
}
- 运行
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/11f808d43912b2f6746ad075bfbaeefa.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9744e0e141c4c2d12326fcbd912d9550.png)