principle
Send through SMTP (mail sending request) protocol, POP3 (mail receiving request) protocol receiving
You need to prepare two packages: mali.jar
activation.jar
copy the package name and find it here: download
the package guide
Get authorization code:
in QQ mailbox settings-account
Implementation code
public class mailDome {
public static void main(String[] args) throws GeneralSecurityException, MessagingException {
Properties prop = new Properties();
prop.setProperty("mail.host","smtp.qq.com");//设置服务器
prop.setProperty("mail.transport.protocol","smtp");//邮件发送协议
prop.setProperty("mail.smtp.auth","true");//需要验证用户密码
//QQ邮箱需要ssl加密
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.enable","true"); //ssl安全连接
prop.put("mail.smtp.ssl.socketFactory",sf);
//1.创建定义整个应该程序所需要的环境信息session对象
Session session = Session.getDefaultInstance(prop, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//用户,授权码
return new PasswordAuthentication("发件人[email protected]", "授权码");//授权码看上面,通过QQ邮箱获取
}
});
//2.通过session获取getTransport,连接和发送
Transport ts = session.getTransport();
//3.使用邮箱的的用户名和授权码连上服务器
ts.connect("smtp.qq.com","发件人[email protected]", "授权码");
//4.创建邮件
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("发件人[email protected]")); //发件人
message.setRecipient(Message.RecipientType.TO,new InternetAddress("收件人[email protected]")); //收件人,可以+s指定多个
message.setSubject("标题"); //标题
message.setContent("<h1 style='color:red'>你好</h1>","text/html;charset=UTF-8"); //内容
//5.发送邮件
ts.sendMessage(message,message.getAllRecipients());//信件,收件的对象(上面设置好的,直接get)
//关闭流
ts.close();
}
}
But general emails are accompanied by pictures or attachments. At this time, we need to encapsulate the email with a layer, and let MimeMultipart as a MimeBodyPart to add to the embedded resource layer. If there are attachments, maybe encapsulate
public static void main(String[] args) throws GeneralSecurityException, MessagingException {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.qq.com");//设置服务器
prop.setProperty("mail.transport.protocol", "smtp");//邮件发送协议
prop.setProperty("mail.smtp.auth", "true");//需要验证用户密码
//QQ邮箱需要ssl加密
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.enable", "true"); //ssl安全连接
prop.put("mail.smtp.ssl.socketFactory", sf);
//1.创建定义整个应该程序所需要的环境信息session对象(QQ邮箱需要)
Session session = Session.getDefaultInstance(prop, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//用户,授权码
return new PasswordAuthentication("发件人[email protected]", "授权码");
}
});
//2.通过session获取getTransport,连接和发送
Transport ts = session.getTransport();
//3.使用邮箱的的用户名和授权码连上服务器
ts.connect("smtp.qq.com", "发件人[email protected]", "授权码");
//4.创建邮件
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("发件人[email protected]")); //发件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress("收件人[email protected]")); //收件人,可以+s指定多个
message.setSubject("标题"); //标题
//---------准备内容---------------------------------------
//准备内嵌资源
MimeBodyPart image = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("D:\\note\\JAVA\\web\\发送邮件.png"));//文件需要DataHandler处理数据
image.setDataHandler(dh);
image.setContentID("1.png");//设置内容id
//准备文本,其实信件也只是发一堆标签过去,但内嵌资源需要提前准备
MimeBodyPart text = new MimeBodyPart();
text.setContent("带图片和附件的邮件<img src='cid:1.png'>bay~","text/html;charset=UTF-8");
//文本和内嵌资源封装
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);//文本
mm.addBodyPart(image);//图片
mm.setSubType("related"); //设置邮件类型 文本:alternative 内嵌资源(图片):related 附件:mixed
//-----要是只发图片,可以直接把mm当作信件发出去了
//封装内嵌资源层
MimeBodyPart contentText = new MimeBodyPart();
contentText.setContent(mm);
//准备附件
MimeBodyPart file = new MimeBodyPart();
DataHandler fdh = new DataHandler(new FileDataSource("D:\\note\\JAVA\\web\\javaweb.md"));
file.setDataHandler(fdh);
file.setFileName("web.md");//不设置文件名后缀,默认是后缀是bin
//封装附件
MimeMultipart fmm = new MimeMultipart();
fmm.addBodyPart(file);
fmm.addBodyPart(contentText);
mm.setSubType("mixed"); //设置邮件类型 文本:alternative 内嵌资源(图片):related 附件:mixed
//-------------------------------------------------------
message.setContent(fmm);
//5.发送邮件
ts.sendMessage(message, message.getAllRecipients());//信件,收件的对象(上面设置好的)
//关闭流
ts.close();
}
If your code is correct, the server may treat your email as spam, which can be found in the trash bin
I think it's helpful after reading it, please give me a thumbs up
Thank you for the video tutorial of B Station Kuangshen teacher: video link