package com.example.pringscreen.email;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
public class QEmail {
public static void main(String[] agrs) throws EmailException{
try{
HtmlEmail email=new HtmlEmail();//创建电子邮件对象
email.setSSL(true);
email.setDebug(true);
email.setSSLOnConnect(false);
email.setHostName("smtp.qq.com");//设置发送电子邮件使用的服务器主机名
email.setSmtpPort(587);//设置发送电子邮件使用的邮件服务器的TCP端口地址
email.setAuthenticator(new DefaultAuthenticator("qq账号", "授权码"));//邮件服务器身份验证
email.setFrom("发信人邮箱");//设置发信人邮箱
email.setSubject("无言独上西楼");//设置邮件主题
email.setMsg("哦哦哦哦哦");//设置邮件文本内容
email.setMsg("this is mail with test1");
email.addTo("收件人账号@qq.com");//设置收件人
EmailAttachment attach =new EmailAttachment();//附件对象
attach.setPath("发送附件本地地址");//附件文件在系统中的路径
attach.setDescription(EmailAttachment.ATTACHMENT);
email.attach(attach);//添加附件
email.send();//发送邮件
System.out.println("程序通过");
}catch(EmailException e){
e.printStackTrace();
}
}
}
email.setAuthenticator(new DefaultAuthenticator(“qq账号”, “授权码”))
授权码在 顶部设置-账号