最近需要寫一個 watch dog 來定時檢視我們提供的 service 是否還活著, 由於需要再 service break 發 mail 通知相關人等, 故拜了 Google 大神將實作細節整理在這.
這邊使用 Yahoo SMTP/POP 作為 mail sending 的平台, 因此你需要有對應的 Yahoo Mail account; 另外使用的 Java 套件是 JavaMail, 你需要將它加到你專案的 classpath 中.
Mail account 設定 :
在開使撰寫代碼前, 先要設定你的 Mail Account. 請進入你的 Mail account 後, 在左上方按照下面指示進入設定頁面 :
接著按照下面說明完成設定 :
範例代碼 :
接著我建立的一個類別 MailAgent 方便後續使用, 完整代碼如下 :
- MailAgent.java :
- package test;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Properties;
- import javax.mail.Authenticator;
- import javax.mail.Message;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Store;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import org.apache.commons.codec.binary.Base64;
- public class MailAgent {
- private String username = ""; // Your account in Yahoo
- private String password = ""; // Your password of account in Yahoo
- private String smtpHost = "smtp.mail.yahoo.com";
- private String popHost = "pop.mail.yahoo.com";
- private String fromAddress = "";
- private List
toAddressList = null; - private Properties props = null;
- public boolean isDebug = false;
- public MailAgent(){}
- public MailAgent(String user,String pwd, String smtpHost, String popHost)
- {
- this.username = user;
- this.password = pwd;
- this.smtpHost = smtpHost;
- this.popHost = popHost;
- }
- public void resetProp()
- {
- props = System.getProperties();
- props.put("mail.smtp.host", smtpHost);
- //props.put("mail.from",from);
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.auth", "true");
- props.put("mail.debug", String.valueOf(isDebug));
- }
- 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 void setPasswordInBase64(String passwordInBase64)
- {
- Base64 base64 = new Base64();
- this.password = new String(base64.decode(passwordInBase64.getBytes()));
- }
- public List
getToAddressList() { - return toAddressList;
- }
- public void setToAddressList(List
toAddressList) { - this.toAddressList = toAddressList;
- }
- public String getSmtpHost() {
- return smtpHost;
- }
- public void setSmtpHost(String smtpHost) {
- this.smtpHost = smtpHost;
- }
- public String getPopHost() {
- return popHost;
- }
- public void setPopHost(String popHost) {
- this.popHost = popHost;
- }
- public String getFromAddress() {
- return fromAddress;
- }
- public void setFromAddress(String fromAddress) {
- this.fromAddress = fromAddress;
- }
- public boolean isDebug() {
- return isDebug;
- }
- public void setDebug(boolean isDebug) {
- this.isDebug = isDebug;
- }
- public boolean sendMail(List
toAddressList, String subject, String body) - {
- try
- {
- if(props==null) resetProp();
- // Get session
- Session session = Session.getDefaultInstance(props, new Authenticator() {
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, password);
- }
- });
- session.setDebug(isDebug);
- // Pop Authenticate yourself
- Store store = session.getStore("pop3");
- store.connect(popHost, username, password);
- MimeMessage message = new MimeMessage(session);
- message.setFrom(new InternetAddress(fromAddress));
- message.setSubject(subject);
- message.setText(body);
- for(String to:toAddressList)
- {
- message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
- }
- // Send message
- Transport.send(message);
- return true;
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- return false;
- }
- public boolean sendMail(String toAddress, String subject, String body)
- {
- List
toAddressList = new LinkedList (); - toAddressList.add(toAddress);
- return this.sendMail(toAddressList, subject, body);
- }
- public boolean sendMail(String subject, String body)
- {
- if(toAddressList==null || fromAddress==null || fromAddress.trim().isEmpty()) return false;
- else
- {
- return sendMail(toAddressList, subject, body);
- }
- }
- }
- public static void main(String[] args) {
- MailAgent mailAgent = new MailAgent();
- mailAgent.setUsername("your yahoo account");
- mailAgent.setPassword("password of your account");
- mailAgent.setFromAddress("from address of your mail");
- List
toAddressList = new LinkedList (); - toAddressList.add("johnlee@ntnu.edu.tw"); // Add to-address1 of mail
- toAddressList.add("puremonkey2007@gmail.com"); // Add to-address2 of mail
- mailAgent.sendMail(toAddressList, "Hello Test", "This is for testing");
- }
Supplement :
* How do I use the Yahoo SMTP server to send mail with the JavaMail API?
* TLS issue when sending to gmail through JavaMail
沒有留言:
張貼留言