邮件发送
邮件发送
maven依赖
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
普通邮件
package org.example.test;
import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class MailDemo {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.setProperty("mail.host","smtp.qq.com");//设置QQ邮件服务器
properties.setProperty("mail.transport.protocol","smtp");//邮件发送协议
properties.setProperty("mail.smtp.auth","true");//需要验证用户名和密码
//关于QQ邮箱,还要设置SSL加密,加上以下代码即可
MailSSLSocketFactory mailSSLSocketFactory = new MailSSLSocketFactory();
mailSSLSocketFactory.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable","true");
properties.put("mail.smtp.ssl.socketFactory",mailSSLSocketFactory);
//使用JavaMail发送邮件的5个步骤
//1、创建定义整个应用程序所需的环境信息的Session对象
//QQ才有!其他邮箱不用
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//发件人邮件用户名、授权码
return new PasswordAuthentication("你的qq号", "你的授权码");
}
});
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport transport = session.getTransport();
//3、使用邮箱的用户名和授权码连上邮件服务器
transport.connect("smtp.qq.com","你的qq号","你的授权码");
//4、创建邮件
//需要传递Session
MimeMessage mimeMessage = new MimeMessage(session);
//指明邮件的发件人
mimeMessage.setFrom(new InternetAddress("你的qq号"));
//指明邮件的收件人,如果发件人和收件人是一样的,那就是自己给自己发
mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("收件人qq号"));
//邮件的标题
mimeMessage.setSubject("测试邮件");
//邮件的文本内容
mimeMessage.setContent("<h3>您好!测试邮件请勿回复!!!</h3>","text/html;charset=utf-8");
//5、发送邮件
transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
//6、关闭连接
transport.close();
}
}
附件邮件
package org.example.test;
import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class MailDemo {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.setProperty("mail.host","smtp.qq.com");//设置QQ邮件服务器
properties.setProperty("mail.transport.protocol","smtp");//邮件发送协议
properties.setProperty("mail.smtp.auth","true");//需要验证用户名和密码
//关于QQ邮箱,还要设置SSL加密,加上以下代码即可
MailSSLSocketFactory mailSSLSocketFactory = new MailSSLSocketFactory();
mailSSLSocketFactory.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable","true");
properties.put("mail.smtp.ssl.socketFactory",mailSSLSocketFactory);
//使用JavaMail发送邮件的5个步骤
//1、创建定义整个应用程序所需的环境信息的Session对象
//QQ才有!其他邮箱不用
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//发件人邮件用户名、授权码
return new PasswordAuthentication("你的qq号", "你的授权码");
}
});
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport transport = session.getTransport();
//3、使用邮箱的用户名和授权码连上邮件服务器
transport.connect("smtp.qq.com","你的qq号","你的授权码");
//4、创建邮件
//需要传递Session
MimeMessage mimeMessage = new MimeMessage(session);
//指明邮件的发件人
mimeMessage.setFrom(new InternetAddress("你的qq号"));
//指明邮件的收件人,如果发件人和收件人是一样的,那就是自己给自己发
mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("收件人qq号"));
//邮件的标题
mimeMessage.setSubject("测试邮件");
//准备图片数据
MimeBodyPart image = new MimeBodyPart();
//图片需要经过数据处理,DataHandler:数据处理
DataHandler dataHandler = new DataHandler(new FileDataSource("附件路径"));
image.setDataHandler(dataHandler);//在MimeBodyPart中放入处理的图片的数据
image.setContentID("起一个附件id");//给图片设置一个ID,在后面可以使用
//准备正文数据
MimeBodyPart text = new MimeBodyPart();
text.setContent("这是一封附件邮件。<img src='cid:附件id'/>","text/html;charset=utf-8");
//描述数据关系
MimeMultipart mimeMultipart = new MimeMultipart();
mimeMultipart.addBodyPart(text);
mimeMultipart.addBodyPart(image);
mimeMultipart.setSubType("related");
//设置到消息中,保存修改
mimeMessage.setContent(mimeMultipart);//把编辑好的邮件放入到消息中
mimeMessage.saveChanges();//保存修改
//5、发送邮件
transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
//6、关闭连接
transport.close();
}
}
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!