原理
通过SMTP(邮件发送请求)协议发送,POP3(邮件接收请求)协议接收
需要准备两个包:mali.jar
activation.jar
复制包名可以到这里找:下载地址
导包教程
获取授权码:
在QQ邮箱的设置-账户
实现代码
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();
}
}
但一般邮件都附带有图片或者附件,这时候我们需要将邮件封装一层,让MimeMultipart作为一个MimeBodyPart添加到内嵌资源层,要是还有附件,也许进行封装
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();
}
如果你的代码无误,可能服务器把你的邮件当作垃圾邮件处理,可以在垃圾箱找到
觉得看完有帮助,帮忙点个赞
感谢b站狂神老师的视频教程:视频链接