Coverage Report - jp.co.y_net.amm.common.AppSendMail
 
Classes in this File Line Coverage Branch Coverage Complexity
AppSendMail
0%
0/41
N/A
2.167
AppSendMail$MailSetting
0%
0/18
0%
0/6
2.167
 
 1  
 package jp.co.y_net.amm.common;
 2  
 
 3  
 import java.io.UnsupportedEncodingException;
 4  
 import java.util.ArrayList;
 5  
 import java.util.Arrays;
 6  
 import java.util.Date;
 7  
 import java.util.List;
 8  
 import java.util.Properties;
 9  
 
 10  
 import javax.mail.Address;
 11  
 import javax.mail.Message;
 12  
 import javax.mail.MessagingException;
 13  
 import javax.mail.Session;
 14  
 import javax.mail.Transport;
 15  
 import javax.mail.internet.InternetAddress;
 16  
 import javax.mail.internet.MimeMessage;
 17  
 
 18  
 import org.slf4j.Logger;
 19  
 import org.slf4j.LoggerFactory;
 20  
 
 21  0
 public class AppSendMail {
 22  0
     private static Logger loggerSlfMail = LoggerFactory.getLogger("jp.co.y_net.amm.mail");
 23  
     
 24  0
     public static class MailSetting{
 25  0
         private String mailTo;
 26  0
         private String title;
 27  0
         private String body;
 28  
         public void setMailTo(String mailTo) {
 29  0
             this.mailTo = mailTo;
 30  0
         }
 31  
         public void setTemplate(String text) {
 32  0
             String tmp = text;
 33  0
             List<String> lines = Arrays.asList(tmp.split("\n"));
 34  0
             for (String line: lines) {
 35  0
                 if(title == null) {
 36  0
                     title = line; // 1行目はタイトル
 37  0
                 } else {
 38  0
                     if(body == null) body = "";
 39  0
                     body += line + "\n";
 40  
                 }
 41  
             }
 42  0
         }
 43  
         public void replace(String key, String value) {
 44  0
             body = body.replace("@"+ key +"@", value);
 45  0
         }
 46  
 
 47  
         
 48  
         @Override
 49  
         public String toString() {
 50  0
             return "MailSetting [title=" + title + ", body=" + body + "]";
 51  
         }
 52  
 
 53  
     }
 54  
 
 55  
 
 56  
     
 57  
     public static boolean send(MailSetting mainSetting) {
 58  
         
 59  0
         Properties objPrp = new Properties();
 60  0
         objPrp.put("mail.smtp.host", ResourceReader.getStringQuick("Smtp"));
 61  0
         objPrp.put("mail.host", ResourceReader.getStringQuick("Smtp"));
 62  0
         objPrp.put("mail.from", ResourceReader.getStringQuick("MailFrom"));
 63  
         
 64  
         /* メールセッションを確立 */
 65  0
         Session mailSes = Session.getDefaultInstance(objPrp,null);
 66  
         
 67  
         /* 送信メッセージを生成 */
 68  0
         MimeMessage objMsg = new MimeMessage(mailSes);
 69  
         
 70  
         try {
 71  
             /* 設定:送信先 */
 72  0
             List<Address> lstAddress = new ArrayList<Address>();
 73  0
             Address to1 = new InternetAddress(mainSetting.mailTo);
 74  0
             lstAddress.add(to1);
 75  0
             Address[] to = (Address[]) lstAddress.toArray(new Address[0]);
 76  0
             objMsg.setRecipients(Message.RecipientType.TO, to);
 77  
             
 78  
             /* 設定:Fromヘッダ */
 79  0
             InternetAddress objFrm = new InternetAddress(ResourceReader.getStringQuick("MailFrom"));
 80  0
             objMsg.setFrom(objFrm);
 81  
 
 82  
             /* 設定:件名 */
 83  0
             objMsg.setSubject(mainSetting.title, "ISO-2022-JP");
 84  
             
 85  
             /* 設定:本文 */
 86  0
             String honbun = AppStringUtils.utf8ToSjis(mainSetting.body);
 87  0
             objMsg.setText(new String(honbun.getBytes("ISO-2022-JP")), "ISO-2022-JP");
 88  0
             objMsg.setSentDate(new Date());
 89  
             
 90  
             /* 送信メールのロギング */
 91  
             
 92  0
             loggerSlfMail.info("-----------------------------------------------------------");
 93  0
             loggerSlfMail.info("Recipients:" + mainSetting.mailTo);
 94  0
             loggerSlfMail.info("Subject:" + mainSetting.title);
 95  0
             loggerSlfMail.info("Text:" + mainSetting.body);
 96  
             
 97  
             
 98  
             /* メール送信実行 */
 99  0
             Transport.send(objMsg);
 100  
             
 101  0
             loggerSlfMail.info("Result:Success");
 102  0
             return true;
 103  
             
 104  0
         } catch (MessagingException e) {
 105  0
             loggerSlfMail.info("Result:Failure!!(see application log...)");
 106  0
             AppLogger.error(e);
 107  0
             return false;
 108  0
         } catch (UnsupportedEncodingException e) {
 109  0
             loggerSlfMail.info("Result:Failure!!(see application log...)");
 110  0
             AppLogger.error(e);
 111  0
             return false;
 112  
         }
 113  
     }
 114  
 
 115  
     /*
 116  
      * メールサーバー構築
 117  
      * http://itpro.nikkeibp.co.jp/article/COLUMN/20060427/236452/
 118  
      * https://technet.microsoft.com/ja-jp/library/cc772058%28v=ws.10%29.aspx
 119  
      *  サーバーマネージャー>機能ビュー
 120  
      */
 121  
     
 122  
     public static void main(String[] args) {
 123  
         // TODO インフラに ZASEKI サーバー 25ポート を開けてもらってテスト。
 124  
         
 125  
         //sendMail();
 126  
         
 127  
         /*
 128  
          * 新着申請内容受信メール
 129  
          */
 130  0
         MailSetting setting = new MailSetting();
 131  0
         setting.setTemplate(ResourceReader.getText("message/mail.orgentry"));
 132  0
         setting.mailTo = ResourceReader.getStringQuick("mailto.olgentry");
 133  0
         setting.replace("申請画面URL", "http://localhost:8080/AMM/orgentrydetail?orgid=" + 3);
 134  0
         send(setting);
 135  
         
 136  0
         System.out.println(setting.toString());
 137  0
     }
 138  
     
 139  
 }
 140