1,先導入一個jar包 html
在pom.xml 中寫入java
<!-- 郵件start -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<!-- 郵件end -->服務器
2,寫一個郵件的類包括,發送內容,端口等session
public class MailSenderInfo {
// 發送郵件的服務器的IP和端口
private String mailServerHost;
private String mailServerPort = "25";
// 郵件發送者的地址
private String fromAddress;
// 郵件接收者的地址
private String toAddress;
// 登錄郵件發送服務器的用戶名和密碼
private String userName;
private String password;
// 是否須要身份驗證
private boolean validate = false;
// 郵件主題
private String subject;
// 郵件的文本內容
private String content;
// 郵件附件的文件名
private String[] attachFileNames;
private String iwallHost ;
/**
* 得到郵件會話屬性
*/
public Properties getProperties(){
Properties p = new Properties();
p.put("mail.smtp.host", this.mailServerHost);
p.put("mail.smtp.port", this.mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
return p;
}
public String getMailServerHost() {
return mailServerHost;
}
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
public String getMailServerPort() {
return mailServerPort;
}
public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}
public boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(String[] fileNames) {
this.attachFileNames = fileNames;
}
public String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getToAddress() {
return toAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String textContent) {
this.content = textContent;
}
public String getIwallHost() {
return iwallHost;
}
public void setIwallHost(String iwallHost) {
this.iwallHost = iwallHost;
}
} ide
3,爲了可配置性,可將上面的類的所需信息寫入配置文件裏this
<bean id = "mailInfo" class = "xxxxxxx.MailSenderInfo" >
<property name="mailServerHost" value = "smtp.exmail.qq.com"></property>
<property name="mailServerPort" value = "25"></property>
<property name="fromAddress" value = "xxxxxxx@xx.xxx"></property>
<property name="userName" value = "xxxxxxxx"></property>
<property name="password" value = "xxxxx"></property>
<property name="iwallHost" value = "http://localhost:8080/xxxxx"></property>
</bean>.net
4,寫一個簡單的發送郵件的類,包含發送text或者 html的方法線程
public class SimpleMailSender {
/**
* 以文本格式發送郵件
* @param mailInfo 待發送的郵件的信息
*/
public boolean sendTextMail(MailSenderInfo mailInfo) {
// 判斷是否須要身份認證
MyMailAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 若是須要身份認證,則建立一個密碼驗證器
authenticator = new MyMailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// 根據郵件會話屬性和密碼驗證器構造一個發送郵件的session
Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
try {
// 根據session建立一個郵件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 建立郵件發送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 設置郵件消息的發送者
mailMessage.setFrom(from);
// 建立郵件的接收者地址,並設置到郵件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO,to);
// 設置郵件消息的主題
mailMessage.setSubject(mailInfo.getSubject());
// 設置郵件消息發送的時間
mailMessage.setSentDate(new Date());
// 設置郵件消息的主要內容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
// 發送郵件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
/**
* 以HTML格式發送郵件
* @param mailInfo 待發送的郵件信息
*/
public static boolean sendHtmlMail(MailSenderInfo mailInfo){
// 判斷是否須要身份認證
MyMailAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
//若是須要身份認證,則建立一個密碼驗證器
if (mailInfo.isValidate()) {
authenticator = new MyMailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// 根據郵件會話屬性和密碼驗證器構造一個發送郵件的session
Session sendMailSession = Session.getDefaultInstance(pro,authenticator);
try {
// 根據session建立一個郵件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 建立郵件發送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 設置郵件消息的發送者
mailMessage.setFrom(from);
// 建立郵件的接收者地址,並設置到郵件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
// Message.RecipientType.TO屬性表示接收者的類型爲TO
mailMessage.setRecipient(Message.RecipientType.TO,to);
// 設置郵件消息的主題
mailMessage.setSubject(mailInfo.getSubject());
// 設置郵件消息發送的時間
mailMessage.setSentDate(new Date());
// MiniMultipart類是一個容器類,包含MimeBodyPart類型的對象
Multipart mainPart = new MimeMultipart();
// 建立一個包含HTML內容的MimeBodyPart
BodyPart html = new MimeBodyPart();
// 設置HTML內容
html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
// 將MiniMultipart對象設置爲郵件內容
mailMessage.setContent(mainPart);
// 發送郵件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
} code
5,在類中,實現發送郵件,建議另起線程發送郵件xml
/**
* 發送郵件內部類
*
*/
class SendEmailThread implements Runnable {
private MailSenderInfo mailInfo;
public SendEmailThread(MailSenderInfo mailInfo) {
this.mailInfo = mailInfo;
}
@Override
public void run() {
try {
if (this.mailInfo != null) {
SimpleMailSender.sendHtmlMail(mailInfo);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 發送郵件方法
*
* @param account * @param code */ public void sendEmail(String account, String code) { WebApplicationContext ac = ContextLoader.getCurrentWebApplicationContext(); // 獲取當前服務IP MailSenderInfo mailInfo = (MailSenderInfo) ac.getBean("mailInfo"); mailInfo.setValidate(Boolean.TRUE); mailInfo.setToAddress(account); mailInfo.setSubject("驗證碼郵件"); mailInfo.setContent( "<p>您好!</p><p>感謝您註冊成爲xxxxxx用戶!</p><p>您的驗證碼是:" + code + "。祝您使用愉快!</p><p>點擊xxxxxxxxx瞭解更多產品。</p>xxxxxxxx</p>"); SendEmailThread d = new CloudResourceImpl().new SendEmailThread(mailInfo); Thread t = new Thread(d); t.start(); }