java郵件介面實現
最近公司在開發一個專案,用到了以前沒接觸的介面。現在在這裡做下記錄。
1:引入jar包:mail.jar。
2:配置郵件地址(mail.properties)
mail.smtp.auth = true
mail.smtp.host = smtp.sina.com
mail.transport.protocol = smtp
mail.user = ********@sina.com
mail.password =*******
3:郵件模板
package com.jyt.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
/**
*
* @author ganjing
* @date 2016年5月16日
@Description: 郵件模板
*/
public class MailTempUtil {
/**
*
* @Description:找回密碼郵件模板
* @param @return
* @return String
* @throws
* @author ganjing
* @date 2016年5月16日
*/
public static String getLostPasswordTemp(String username){
//生成隨機碼
String code = CheckCode.createCheckCodeNoFour();
String email = "";
email += "<div>親愛的使用者 "+username+":您好!</div>";
email += "<div>您收到這封這封電子郵件是因為您 (也可能是某人冒充您的名義) 申請了一個新的密碼。假如這不是您本人所申請, 請不用理會這封電子郵件, 但是如果您持續收到這類的信件騷擾, 請您儘快聯絡管理員。</div>";
email += "<div> 要使用新的密碼, 請使用以下連結啟用密碼。</div>";
//md5加密字串
email += "<div><a href=www.baidu.com'</a></div>";
email += "<div> 注意:請您在收到郵件1個小時內使用,否則該連結將會失效。</div>";
return email;
}
}
4:傳送郵件
package com.jyt.util;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
/**
* 傳送郵件的測試程式
*
* @author lwq
*
*/
public class MailUtil {
final static String mailUrl = "\\mail.properties";
final static Properties props = new Properties();//配置檔案物件
static String rootPath= "";
/**
*
* @Description: 找回密碼郵件
* @param @param InternetAddress
* @param @return
* @return String
* @throws
* @author ganjing
* @date 2016年5月13日
*/
public static boolean getPasswordMail(String InternetAddress,String username){
try {
MimeMessage message = commonMailConfig(InternetAddress);
// 設定郵件標題
message.setSubject("**************");
// 設定郵件的內容體
String str = MailTempUtil.getLostPasswordTemp(username);
message.setContent(str, "text/html;charset=UTF-8");
Transport.send(message);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
*
* @Description: 郵件引數的設定
* @param @param InternetAddress
* @param @return
* @return MimeMessage
* @throws
* @author ganjing
* @date 2016年5月13日
*/
public static MimeMessage commonMailConfig(String InternetAddress){
MimeMessage message = null;
// 傳送郵件
try {
/*
* 可用的屬性: mail.store.protocol / mail.transport.protocol / mail.host /
* mail.user / mail.from
*/
// 表示SMTP傳送郵件,需要進行身份驗證
rootPath = MailUtil.class.getResource("").toString();
String url = rootPath.substring(6, rootPath.length()-1).split("/com")[0];
props.load(new BufferedInputStream(new FileInputStream(url+mailUrl)));
// 構建授權資訊,用於進行SMTP進行身份驗證
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 使用者名稱、密碼
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用環境屬性和授權資訊,建立郵件會話
Session mailSession = Session.getInstance(props, authenticator);
// 建立郵件訊息
message = new MimeMessage(mailSession);
// 設定發件人
InternetAddress form = new InternetAddress(
props.getProperty("mail.user"));
message.setFrom(form);
// 設定收件人
InternetAddress to = new InternetAddress(InternetAddress);
message.setRecipient(RecipientType.TO, to);
// 設定抄送
InternetAddress cc = new InternetAddress(InternetAddress);
message.setRecipient(RecipientType.CC, cc);
// 設定密送,其他的收件人不能看到密送的郵件地址
InternetAddress bcc = new InternetAddress(InternetAddress);
message.setRecipient(RecipientType.CC, bcc);
return message;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return message;
}
}
}