1.maven jar管理:pom.xml
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
2.java代码:
package com.neusoft.nhip.neubi.dashboard.util;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailUtil {
public static void sendMail(MailMessageDTO mailMessageDTO)
throws Exception {
Properties props = new Properties(); // 可以加载一个配置文件
// 使用smtp:简单邮件传输协议
props.put("mail.smtp.host", "smtp.neusoft.com");// 存储发送邮件服务器的信息
props.put("mail.smtp.auth", "true");// 同时通过验证
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props);// 根据属性新建一个邮件会话
// session.setDebug(true); //有他会打印一些调试信息。
MimeMessage message = new MimeMessage(session);// 由邮件会话新建一个消息对象
message.setFrom(new InternetAddress(mailMessageDTO.getFromMail()));// 设置发件人的地址
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
mailMessageDTO.getToMail()));// 设置收件人,并设置其接收类型为TO
message.setSubject(mailMessageDTO.getMailTitle());// 设置标题
// 设置信件内容
// message.setText(mailContent); //发送 纯文本 邮件 todo
message.setContent(mailMessageDTO.getMailContent(), "text/html;charset=UTF-8"); // 发送HTML邮件,内容样式比较丰富
message.setSentDate(new Date());// 设置发信时间
message.saveChanges();// 存储邮件信息
// 发送邮件
Transport transport = session.getTransport("smtp");
//Transport transport = session.getTransport();
transport.connect(mailMessageDTO.getUserName(), mailMessageDTO.getPassword());
transport.sendMessage(message, message.getAllRecipients());// 发送邮件,其中第二个参数是所有已设好的收件人地址
transport.close();
}
public static void main(String[] args) throws Exception {
MailMessageDTO dto = new MailMessageDTO();
dto.setFromMail("li.cui@neusoft.com");
dto.setUserName("li.cui");
dto.setPassword("licui1107@");
dto.setToMail("li.cui@neusoft.com");
dto.setMailTitle("Java Mail 测试邮件");
dto.setMailContent("<a href='#'>测试的HTML邮件</a>");
sendMail(dto);
}
}
package com.neusoft.nhip.neubi.dashboard.util;
/**
*
* @ClassName: MailMessageDTO
* @Description: 发送邮件对象
* @author li.cui
* @date 2016年6月2日 下午4:53:41
*
*/
public class MailMessageDTO {
/**
* 服务器信息
*/
private String smtpHost; //发件服务器smtp.neusoft.com
private String smtpPort; //端口
/**
* 发件箱信息
*/
private String fromMail; //发件箱
private String userName; //发件箱用户名
private String password; //发信密码
/**
* 收件信息
*/
private String toMail; //收件箱地址
/**
* 邮件内容
*/
private String mailTitle; //标题
private String mailContent; //内容
public String getSmtpHost() {
return smtpHost;
}
public void setSmtpHost(String smtpHost) {
this.smtpHost = smtpHost;
}
public String getSmtpPort() {
return smtpPort;
}
public void setSmtpPort(String smtpPort) {
this.smtpPort = smtpPort;
}
public String getFromMail() {
return fromMail;
}
public void setFromMail(String fromMail) {
this.fromMail = fromMail;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getToMail() {
return toMail;
}
public void setToMail(String toMail) {
this.toMail = toMail;
}
public String getMailTitle() {
return mailTitle;
}
public void setMailTitle(String mailTitle) {
this.mailTitle = mailTitle;
}
public String getMailContent() {
return mailContent;
}
public void setMailContent(String mailContent) {
this.mailContent = mailContent;
}
}
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
2.java代码:
package com.neusoft.nhip.neubi.dashboard.util;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailUtil {
public static void sendMail(MailMessageDTO mailMessageDTO)
throws Exception {
Properties props = new Properties(); // 可以加载一个配置文件
// 使用smtp:简单邮件传输协议
props.put("mail.smtp.host", "smtp.neusoft.com");// 存储发送邮件服务器的信息
props.put("mail.smtp.auth", "true");// 同时通过验证
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props);// 根据属性新建一个邮件会话
// session.setDebug(true); //有他会打印一些调试信息。
MimeMessage message = new MimeMessage(session);// 由邮件会话新建一个消息对象
message.setFrom(new InternetAddress(mailMessageDTO.getFromMail()));// 设置发件人的地址
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
mailMessageDTO.getToMail()));// 设置收件人,并设置其接收类型为TO
message.setSubject(mailMessageDTO.getMailTitle());// 设置标题
// 设置信件内容
// message.setText(mailContent); //发送 纯文本 邮件 todo
message.setContent(mailMessageDTO.getMailContent(), "text/html;charset=UTF-8"); // 发送HTML邮件,内容样式比较丰富
message.setSentDate(new Date());// 设置发信时间
message.saveChanges();// 存储邮件信息
// 发送邮件
Transport transport = session.getTransport("smtp");
//Transport transport = session.getTransport();
transport.connect(mailMessageDTO.getUserName(), mailMessageDTO.getPassword());
transport.sendMessage(message, message.getAllRecipients());// 发送邮件,其中第二个参数是所有已设好的收件人地址
transport.close();
}
public static void main(String[] args) throws Exception {
MailMessageDTO dto = new MailMessageDTO();
dto.setFromMail("li.cui@neusoft.com");
dto.setUserName("li.cui");
dto.setPassword("licui1107@");
dto.setToMail("li.cui@neusoft.com");
dto.setMailTitle("Java Mail 测试邮件");
dto.setMailContent("<a href='#'>测试的HTML邮件</a>");
sendMail(dto);
}
}
package com.neusoft.nhip.neubi.dashboard.util;
/**
*
* @ClassName: MailMessageDTO
* @Description: 发送邮件对象
* @author li.cui
* @date 2016年6月2日 下午4:53:41
*
*/
public class MailMessageDTO {
/**
* 服务器信息
*/
private String smtpHost; //发件服务器smtp.neusoft.com
private String smtpPort; //端口
/**
* 发件箱信息
*/
private String fromMail; //发件箱
private String userName; //发件箱用户名
private String password; //发信密码
/**
* 收件信息
*/
private String toMail; //收件箱地址
/**
* 邮件内容
*/
private String mailTitle; //标题
private String mailContent; //内容
public String getSmtpHost() {
return smtpHost;
}
public void setSmtpHost(String smtpHost) {
this.smtpHost = smtpHost;
}
public String getSmtpPort() {
return smtpPort;
}
public void setSmtpPort(String smtpPort) {
this.smtpPort = smtpPort;
}
public String getFromMail() {
return fromMail;
}
public void setFromMail(String fromMail) {
this.fromMail = fromMail;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getToMail() {
return toMail;
}
public void setToMail(String toMail) {
this.toMail = toMail;
}
public String getMailTitle() {
return mailTitle;
}
public void setMailTitle(String mailTitle) {
this.mailTitle = mailTitle;
}
public String getMailContent() {
return mailContent;
}
public void setMailContent(String mailContent) {
this.mailContent = mailContent;
}
}