Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package th.in.oneauthen.servlet;
- import java.util.Properties;
- import javax.activation.DataHandler;
- import javax.activation.DataSource;
- import javax.activation.FileDataSource;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Multipart;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- import th.in.oneauthen.object.SystemConfigDB;
- import th.in.oneauthen.object.DAO.SystemConfigDAO;
- public class OneMailServlet {
- public static final String CONFIG_HOST = "SMTP_HOST";
- public static final String CONFIG_SOCKETFACTORY = "SMTP_SOCKETFACTORY";
- public static final String CONFIG_SOCKETFACTORY_CLASS = "SMTP_SOCKETFACTORY_CLASS";
- public static final String CONFIG_SMTP_AUTH = "SMTP_AUTH";
- public static final String CONFIG_SMTP_PORT = "SMTP_PORT";
- public static final String CONFIG_MAIL_OFFICIAL = "MAIL_OFFICIAL";
- public static String MAIL_OFFICIAL = null;
- public static final String CONFIG_MAIL_OFFICIAL_PWS = "PWS_MAIL_OFFICIAL";
- public static String MAIL_OFFICIAL_PWS = null;
- public static final String CONFIG_MAIL_NAME = "NAME_MAIL";
- public static String MAIL_NAME = null;
- public static void SendToOneMail(String locationfile, String email) {
- String mailHost = null;
- try {
- SystemConfigDB sysConfig = new SystemConfigDAO().find(CONFIG_HOST);
- if (sysConfig != null)
- mailHost = sysConfig.getValue();
- } catch (Exception e) {
- e.printStackTrace();
- }
- String socketFactoryPort = null;
- try {
- SystemConfigDB sysConfig = new SystemConfigDAO().find(CONFIG_SOCKETFACTORY);
- if (sysConfig != null)
- socketFactoryPort = sysConfig.getValue();
- } catch (Exception e) {
- e.printStackTrace();
- }
- String socketFactoryClass = null;
- try {
- SystemConfigDB sysConfig = new SystemConfigDAO().find(CONFIG_SOCKETFACTORY_CLASS);
- if (sysConfig != null)
- socketFactoryClass = sysConfig.getValue();
- } catch (Exception e) {
- e.printStackTrace();
- }
- String smtpAuth = null;
- try {
- SystemConfigDB sysConfig = new SystemConfigDAO().find(CONFIG_SMTP_AUTH);
- if (sysConfig != null)
- smtpAuth = sysConfig.getValue();
- } catch (Exception e) {
- e.printStackTrace();
- }
- String smtpPort = null;
- try {
- SystemConfigDB sysConfig = new SystemConfigDAO().find(CONFIG_SMTP_PORT);
- if (sysConfig != null)
- smtpPort = sysConfig.getValue();
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- SystemConfigDB sysConfig = new SystemConfigDAO().find(CONFIG_MAIL_OFFICIAL);
- if (sysConfig != null)
- MAIL_OFFICIAL = sysConfig.getValue();
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- SystemConfigDB sysConfig = new SystemConfigDAO().find(CONFIG_MAIL_OFFICIAL_PWS);
- if (sysConfig != null)
- MAIL_OFFICIAL_PWS = sysConfig.getValue();
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- SystemConfigDB sysConfig = new SystemConfigDAO().find(CONFIG_MAIL_NAME);
- if (sysConfig != null)
- MAIL_NAME = sysConfig.getValue();
- } catch (Exception e) {
- e.printStackTrace();
- }
- Properties props = new Properties();
- props.put("mail.smtp.host", mailHost);
- props.put("mail.smtp.socketFactory.port", socketFactoryPort);
- props.put("mail.smtp.socketFactory.class",socketFactoryClass);
- props.put("mail.smtp.auth", smtpAuth);
- props.put("mail.smtp.port", smtpPort);
- Session session = Session.getDefaultInstance(props,
- new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(MAIL_OFFICIAL,MAIL_OFFICIAL_PWS);
- }
- });
- try {
- Message message = new MimeMessage(session);
- message.setFrom(new InternetAddress(MAIL_NAME));
- message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(email));
- //message.setRecipients(Message.RecipientType.CC, InternetAddress.parse("chaichana.si@oneauthen.in.th", false));
- message.setSubject("Signing Document");
- MimeBodyPart messageBodyPart = new MimeBodyPart();
- Multipart multipart = new MimeMultipart();
- messageBodyPart = new MimeBodyPart();
- String file = locationfile;
- String fileName = "Document.pdf";
- DataSource source = new FileDataSource(file);
- messageBodyPart.setDataHandler(new DataHandler(source));
- messageBodyPart.setFileName(fileName);
- multipart.addBodyPart(messageBodyPart);
- message.setContent(multipart);
- System.out.println("Sending");
- Transport.send(message);
- System.out.println("Done");
- } catch (MessagingException e) {
- throw new RuntimeException(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement