ballchaichana

onemail

Oct 17th, 2018
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 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. public class OneMailServlet {
  20.     public static void SendToOneMail(String locationfile, String email) {
  21.         Properties props = new Properties();
  22.         props.put("mail.smtp.host", "smtp.inetmail.cloud");
  23.         props.put("mail.smtp.socketFactory.port", "25");
  24.         props.put("mail.smtp.socketFactory.class",
  25.                 "javax.net.ssl.SSLSocketFactory");
  26.         props.put("mail.smtp.auth", "true");
  27.         props.put("mail.smtp.port", "25");
  28.  
  29.         Session session = Session.getDefaultInstance(props,
  30.             new javax.mail.Authenticator() {
  31.                 protected PasswordAuthentication getPasswordAuthentication() {
  32.                     return new PasswordAuthentication("[email protected]","Chaichana2538");
  33.                 }
  34.             });
  35.  
  36.         try {
  37.  
  38.             Message message = new MimeMessage(session);
  39.             message.setFrom(new InternetAddress("[email protected]"));
  40.             message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(email));
  41.             message.setRecipients(Message.RecipientType.CC, InternetAddress.parse("[email protected]", false));
  42.             message.setSubject("Signing Document");
  43.  
  44.            
  45.            
  46.             MimeBodyPart messageBodyPart = new MimeBodyPart();
  47.             Multipart multipart = new MimeMultipart();
  48.             messageBodyPart = new MimeBodyPart();
  49.             String file = locationfile;
  50.             String fileName = "Document.pdf";
  51.             DataSource source = new FileDataSource(file);
  52.             messageBodyPart.setDataHandler(new DataHandler(source));
  53.             messageBodyPart.setFileName(fileName);
  54.             multipart.addBodyPart(messageBodyPart);
  55.             message.setContent(multipart);
  56.             System.out.println("Sending");
  57.             Transport.send(message);
  58.             System.out.println("Done");
  59.  
  60.  
  61.         } catch (MessagingException e) {
  62.             throw new RuntimeException(e);
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment