Advertisement
ballchaichana

bak

Oct 29th, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.87 KB | None | 0 0
  1. package th.in.oneauthen.servlet;
  2.  
  3. import java.util.Properties;
  4.  
  5. import javax.activation.DataHandler;
  6. import javax.activation.DataSource;
  7. import javax.activation.FileDataSource;
  8. import javax.mail.Message;
  9. import javax.mail.MessagingException;
  10. import javax.mail.Multipart;
  11. import javax.mail.PasswordAuthentication;
  12. import javax.mail.Session;
  13. import javax.mail.Transport;
  14. import javax.mail.internet.InternetAddress;
  15. import javax.mail.internet.MimeBodyPart;
  16. import javax.mail.internet.MimeMessage;
  17. import javax.mail.internet.MimeMultipart;
  18.  
  19. import th.in.oneauthen.object.SystemConfigDB;
  20. import th.in.oneauthen.object.DAO.SystemConfigDAO;
  21.  
  22. public class OneMailServlet {
  23.    
  24.     public static final String CONFIG_HOST = "SMTP_HOST";
  25.     public static final String CONFIG_SOCKETFACTORY = "SMTP_SOCKETFACTORY";
  26.     public static final String CONFIG_SOCKETFACTORY_CLASS = "SMTP_SOCKETFACTORY_CLASS";
  27.     public static final String CONFIG_SMTP_AUTH = "SMTP_AUTH";
  28.     public static final String CONFIG_SMTP_PORT = "SMTP_PORT";
  29.     public static final String CONFIG_MAIL_OFFICIAL = "MAIL_OFFICIAL";
  30.     public static String MAIL_OFFICIAL = null;
  31.     public static final String CONFIG_MAIL_OFFICIAL_PWS = "PWS_MAIL_OFFICIAL";
  32.     public static String MAIL_OFFICIAL_PWS = null;
  33.     public static final String CONFIG_MAIL_NAME = "NAME_MAIL";
  34.     public static String MAIL_NAME = null;
  35.    
  36.     public static void SendToOneMail(String locationfile, String email) {
  37.        
  38.        
  39.         String mailHost =   null;
  40.         try {
  41.             SystemConfigDB sysConfig = new SystemConfigDAO().find(CONFIG_HOST);
  42.             if (sysConfig != null)
  43.                 mailHost = sysConfig.getValue();
  44.         } catch (Exception e) {
  45.             e.printStackTrace();
  46.         }
  47.        
  48.         String socketFactoryPort =  null;
  49.         try {
  50.             SystemConfigDB sysConfig = new SystemConfigDAO().find(CONFIG_SOCKETFACTORY);
  51.             if (sysConfig != null)
  52.                 socketFactoryPort = sysConfig.getValue();
  53.         } catch (Exception e) {
  54.             e.printStackTrace();
  55.         }
  56.        
  57.         String socketFactoryClass =     null;
  58.         try {
  59.             SystemConfigDB sysConfig = new SystemConfigDAO().find(CONFIG_SOCKETFACTORY_CLASS);
  60.             if (sysConfig != null)
  61.                 socketFactoryClass = sysConfig.getValue();
  62.         } catch (Exception e) {
  63.             e.printStackTrace();
  64.         }
  65.        
  66.         String smtpAuth =   null;
  67.         try {
  68.             SystemConfigDB sysConfig = new SystemConfigDAO().find(CONFIG_SMTP_AUTH);
  69.             if (sysConfig != null)
  70.                 smtpAuth = sysConfig.getValue();
  71.         } catch (Exception e) {
  72.             e.printStackTrace();
  73.         }
  74.        
  75.         String smtpPort =   null;
  76.         try {
  77.             SystemConfigDB sysConfig = new SystemConfigDAO().find(CONFIG_SMTP_PORT);
  78.             if (sysConfig != null)
  79.                 smtpPort = sysConfig.getValue();
  80.         } catch (Exception e) {
  81.             e.printStackTrace();
  82.         }
  83.        
  84.  
  85.         try {
  86.             SystemConfigDB sysConfig = new SystemConfigDAO().find(CONFIG_MAIL_OFFICIAL);
  87.             if (sysConfig != null)
  88.                 MAIL_OFFICIAL = sysConfig.getValue();
  89.         } catch (Exception e) {
  90.             e.printStackTrace();
  91.         }
  92.        
  93.         try {
  94.             SystemConfigDB sysConfig = new SystemConfigDAO().find(CONFIG_MAIL_OFFICIAL_PWS);
  95.             if (sysConfig != null)
  96.                 MAIL_OFFICIAL_PWS = sysConfig.getValue();
  97.         } catch (Exception e) {
  98.             e.printStackTrace();
  99.         }
  100.        
  101.         try {
  102.             SystemConfigDB sysConfig = new SystemConfigDAO().find(CONFIG_MAIL_NAME);
  103.             if (sysConfig != null)
  104.                 MAIL_NAME = sysConfig.getValue();
  105.         } catch (Exception e) {
  106.             e.printStackTrace();
  107.         }
  108.        
  109.        
  110.        
  111.         Properties props = new Properties();
  112.         props.put("mail.smtp.host", mailHost);
  113.         props.put("mail.smtp.socketFactory.port", socketFactoryPort);
  114.         props.put("mail.smtp.socketFactory.class",socketFactoryClass);
  115.         props.put("mail.smtp.auth", smtpAuth);
  116.         props.put("mail.smtp.port", smtpPort);
  117.  
  118.         Session session = Session.getDefaultInstance(props,
  119.             new javax.mail.Authenticator() {
  120.                 protected PasswordAuthentication getPasswordAuthentication() {
  121.                     return new PasswordAuthentication(MAIL_OFFICIAL,MAIL_OFFICIAL_PWS);
  122.                 }
  123.             });
  124.  
  125.         try {
  126.  
  127.             Message message = new MimeMessage(session);
  128.             message.setFrom(new InternetAddress(MAIL_NAME));
  129.             message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(email));
  130.             //message.setRecipients(Message.RecipientType.CC, InternetAddress.parse("chaichana.si@oneauthen.in.th", false));
  131.             message.setSubject("Signing Document");
  132.  
  133.            
  134.            
  135.             MimeBodyPart messageBodyPart = new MimeBodyPart();
  136.             Multipart multipart = new MimeMultipart();
  137.             messageBodyPart = new MimeBodyPart();
  138.             String file = locationfile;
  139.             String fileName = "Document.pdf";
  140.             DataSource source = new FileDataSource(file);
  141.             messageBodyPart.setDataHandler(new DataHandler(source));
  142.             messageBodyPart.setFileName(fileName);
  143.             multipart.addBodyPart(messageBodyPart);
  144.             message.setContent(multipart);
  145.             System.out.println("Sending");
  146.             Transport.send(message);
  147.             System.out.println("Done");
  148.  
  149.  
  150.         } catch (MessagingException e) {
  151.             throw new RuntimeException(e);
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement