一、 开启SMTP服务
1.基本都在邮箱设置里,开启后会获得神秘代码,后面有用。
2.记得添加依赖,或者自己添加jar包。
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0-b01</version>
</dependency>
二、发送新浪邮箱带附件
public static void sinaMail() throws GeneralSecurityException {
// 收件人电子邮箱
String to = "XXXXXXXXX@qq.com"; //也可以的
// 发件人电子邮箱
String from = "XXXXXXXXX@sina.com";
// 获取系统属性
Properties properties = new Properties();
//发送邮件协议
properties.setProperty("mail.transport.protocol", "SMTP");
// 设置邮件服务器
properties.setProperty("mail.smtp.host", "smtp.sina.com");
// 设置邮件服务器是否需要登录认证
properties.setProperty("mail.smtp.auth", "true");
// 验证账号及密码,密码需要是第三方授权码
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("XXXXXXXXX@sina.com", "ed0dc82fcd7cea3f");
}
};
// 获取默认session对象
Session session = Session.getInstance(properties,auth);
try{
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// Set From: 头部头字段
message.setFrom(new InternetAddress(from));
// Set To: 邮件接收人
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: 主题名称
message.setSubject("This is the Subject Line!");
// 创建消息部分
BodyPart messageBodyPart = new MimeBodyPart();
// 消息内容
messageBodyPart.setText("TEST/TEST");
// 创建多重消息
Multipart multipart = new MimeMultipart();
// 设置文本消息部分
multipart.addBodyPart(messageBodyPart);
// 附件部分
messageBodyPart = new MimeBodyPart();
//把文件,添加到附件1中
//数据源
DataSource source = new FileDataSource(new File("D:/1.pptx"));
//设置第一个附件的数据
messageBodyPart.setDataHandler(new DataHandler(source));
//设置附件的文件名
messageBodyPart.setFileName("1.pptx");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
// 发送消息
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
三、发送QQ邮箱带附件
public static void qqMail() throws GeneralSecurityException {
// 收件人电子邮箱
String to = "XXXXXXXXX@qq.com"; //也可以的
// 发件人电子邮箱
String from = "XXXXXXXXX@qq.com";
// 获取系统属性
Properties properties = new Properties();
//发送邮件协议
properties.setProperty("mail.transport.protocol", "SMTP");
// 设置邮件服务器
properties.setProperty("mail.host", "smtp.qq.com");
// 设置邮件服务器端口
properties.setProperty("mail.smtp.socketFactory.port", "587");
//如果是阿里云服务器
properties.put("mail.smtp.port", "587");
// 设置邮件服务器是否需要登录认证
properties.setProperty("mail.smtp.auth", "true");
// 验证账号及密码,密码需要是第三方授权码
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("XXXXXXXXX@qq.com", "urzgdvdyflesbjhh");
}
};
// 获取默认session对象
Session session = Session.getInstance(properties,auth);
try{
// 创建默认的 MimeMessage 对象
MimeMessage message = new MimeMessage(session);
// Set From: 头部头字段
message.setFrom(new InternetAddress(from));
// Set To: 邮件接收人
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: 主题名称
message.setSubject("This is the Subject Line!");
// 创建消息部分
BodyPart messageBodyPart = new MimeBodyPart();
// 消息内容
messageBodyPart.setText("TEST/TEST");
// 创建多重消息
Multipart multipart = new MimeMultipart();
// 设置文本消息部分
multipart.addBodyPart(messageBodyPart);
// 附件部分
messageBodyPart = new MimeBodyPart();
//把文件,添加到附件1中
//数据源
DataSource source = new FileDataSource(new File("D:/1.pptx"));
//设置第一个附件的数据
messageBodyPart.setDataHandler(new DataHandler(source));
//设置附件的文件名
messageBodyPart.setFileName("1.pptx");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
// 发送消息
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
四、效果
五、补充
1.邮件内容换行
// 设置文本消息部分
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart,"text/html;charset=UTF-8");
用下面这个换行
<br>