java 发送 mail 纯文本发送和html格式发送

时间:2023-03-08 20:54:10
java 发送 mail 纯文本发送和html格式发送

一:需要引入mail maven jar包

 <!--邮件发送包-->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>

二:代码示例

2.1 定义消息实体类

package textAnalysis.dto;

/**
* Created by 草帽boy on 2016/12/8.
*/ public class MailInfo {
//邮箱服务器 如smtp.163.com
private String host ;
//用户邮箱 如**@163
private String formName ;
//用户授权码 不是用户名密码 可以自行查看相关邮件服务器怎么查看
private String formPassword ;
//消息回复邮箱
private String replayAddress ;
//发送地址
private String toAddress ;
//发送主题
private String subject ;
//发送内容
private String content ; public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getFormName() {
return formName;
}
public void setFormName(String formName) {
this.formName = formName;
}
public String getFormPassword() {
return formPassword;
}
public void setFormPassword(String formPassword) {
this.formPassword = formPassword;
}
public String getReplayAddress() {
return replayAddress;
}
public void setReplayAddress(String replayAddress) {
this.replayAddress = replayAddress;
}
public String getToAddress() {
return toAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}

2.2 两种方式实现

首先你需要获取你的授权码,以@163.com为例 http://blog.csdn.net/hughnes/article/details/52070878

sendTextMail是纯文本发送
sendHtmlMail是HTML格式标签发送 这种方式可以识别html标签
package textAnalysis.util;

import textAnalysis.dto.MailInfo;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Properties; /**
* Created by 草帽boy on 2016/12/8.
*/
public class MailSendUtil {
private final static String host = "smtp.163.com"; //163的服务器
private final static String formName = "YYYXXX@163.com";//你的邮箱
private final static String password = "***********"; //授权码
private final static String replayAddress = "YYYXXX@163.com"; //你的邮箱 public static void sendHtmlMail(MailInfo info)throws Exception{
info.setHost(host);
info.setFormName(formName);
info.setFormPassword(password); //网易邮箱的授权码~不一定是密码
info.setReplayAddress(replayAddress); Message message = getMessage(info);
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
// 设置HTML内容
html.setContent(info.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
message.setContent(mainPart);
Transport.send(message);
} public static void sendTextMail(MailInfo info) throws Exception { info.setHost(host);
info.setFormName(formName);
info.setFormPassword(password); //网易邮箱的授权码~不一定是密码
info.setReplayAddress(replayAddress);
Message message = getMessage(info);
//消息发送的内容
message.setText(info.getContent()); Transport.send(message);
} private static Message getMessage(MailInfo info) throws Exception{
final Properties p = System.getProperties() ;
p.setProperty("mail.smtp.host", info.getHost());
p.setProperty("mail.smtp.auth", "true");
p.setProperty("mail.smtp.user", info.getFormName());
p.setProperty("mail.smtp.pass", info.getFormPassword()); // 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session session = Session.getInstance(p, new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(p.getProperty("mail.smtp.user"),p.getProperty("mail.smtp.pass"));
}
});
session.setDebug(true);
Message message = new MimeMessage(session);
//消息发送的主题
message.setSubject(info.getSubject());
//接受消息的人
message.setReplyTo(InternetAddress.parse(info.getReplayAddress()));
//消息的发送者
message.setFrom(new InternetAddress(p.getProperty("mail.smtp.user"),"余高峰"));
// 创建邮件的接收者地址,并设置到邮件消息中
message.setRecipient(Message.RecipientType.TO,new InternetAddress(info.getToAddress()));
// 消息发送的时间
message.setSentDate(new Date()); return message ;
}
}

三:测试

@Test
public void send(){
String mail = "mmmmmmm@qq.com"; //发送对象的邮箱
String title = "我有一句话跟你说";
String content = "<div>你不在学校吗?</div><br/><hr/><div>记得28号来学校</div>";
MailInfo info = new MailInfo();
info.setToAddress(mail);
info.setSubject(title);
info.setContent(content);
try {
       //MailSendUtil.sendTextMail(info);
MailSendUtil.sendHtmlMail(info);
} catch (Exception e) {
System.out.print("'"+title+"'的邮件发送失败!");
e.printStackTrace();
}
}

采用的Junit的方式测试的采用  sendTextMail  的结果是

java 发送 mail 纯文本发送和html格式发送

采用的Junit的方式测试的采用  sendHtmlMai l的结果是

java 发送 mail 纯文本发送和html格式发送