Guest User

Untitled

a guest
Feb 14th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. import java.util.Properties;
  2.  
  3. import javax.activation.DataHandler;
  4. import javax.activation.DataSource;
  5. import javax.activation.FileDataSource;
  6. import javax.mail.Message;
  7. import javax.mail.MessagingException;
  8. import javax.mail.Multipart;
  9. import javax.mail.PasswordAuthentication;
  10. import javax.mail.Session;
  11. import javax.mail.Transport;
  12. import javax.mail.internet.InternetAddress;
  13. import javax.mail.internet.MimeBodyPart;
  14. import javax.mail.internet.MimeMessage;
  15. import javax.mail.internet.MimeMultipart;
  16.  
  17. public class SendMessage implements Runnable {
  18.  
  19. private final String line, alphanumeric;
  20.  
  21. public SendMessage(String line, String alphanumeric) {
  22. this.line = line;
  23. this.alphanumeric = alphanumeric;
  24. }
  25.  
  26. @Override
  27. public void run() {
  28. System.out.println(line);
  29. String HOST = line.split( "," )[ 0 ];
  30. final String USER = line.split( "," )[ 1 ];
  31. final String PASS = line.split( "," )[ 2 ];
  32. String RECPT = line.split( "," )[ 3 ];
  33. String FIRST = line.split( "," )[ 4 ];
  34. String LAST = line.split( "," )[ 5 ];
  35. String DOB = line.split( "," )[ 6 ];
  36. String PASSPORT = line.split( "," )[ 7 ];
  37. String PURPOSE = line.split( "," )[ 8 ];
  38. String PHONE = line.split( "," )[ 9 ];
  39. String EMAIL = line.split( "," )[ 10 ];
  40. String FILE1 = line.split( "," )[ 11 ];
  41. String FILE2 = line.split( "," )[ 12 ];
  42.  
  43. String MSG = "\r\n" + "Dear Sir/Madam" + "\r\n";
  44. MSG += "\r\n" + "Please check attached file below: " + " \r\n";
  45. MSG += "\r\n" + "FIRST AND SUR NAME: " + FIRST + " " + LAST + "\r\n";
  46. MSG += "DATE OF BIRTH: " + DOB + "\r\n";
  47. MSG += "PASSPORT NUMBER: " + PASSPORT + "\r\n";
  48. MSG += "PURPOSE OF TRAVEL: " + PURPOSE + "\r\n";
  49. MSG += "PHONE: " + PHONE + "\r\n";
  50. MSG += "EMAIL: " + EMAIL + "\r\n";
  51. MSG += "\r\n" + "Thanks";
  52. MSG += "\r\n" + FIRST + " " + LAST + "\r\n \r\n \r\n";
  53.  
  54. System.out.println( MSG );
  55.  
  56. try
  57. {
  58. Properties props = new Properties();
  59. props.put("mail.smtp.host", HOST );
  60. props.put("mail.smtp.socketFactory.port", "465" );
  61. props.put("mail.smtp.socketFactory.class",
  62. "javax.net.ssl.SSLSocketFactory" );
  63. props.put("mail.smtp.auth", "true" );
  64. props.put("mail.smtp.port", "587" );
  65.  
  66. Session session = Session.getDefaultInstance(props,
  67. new javax.mail.Authenticator() {
  68. protected PasswordAuthentication getPasswordAuthentication() {
  69. return new PasswordAuthentication( USER, PASS );
  70. }
  71. });
  72.  
  73. try {
  74.  
  75. Message message = new MimeMessage(session);
  76. message.setFrom( new InternetAddress( EMAIL ) );
  77. message.setRecipients( Message.RecipientType.TO,
  78. InternetAddress.parse( RECPT ) );
  79. message.setSubject( alphanumeric );
  80. message.setText( "" );
  81.  
  82. MimeBodyPart messageBodyPart = new MimeBodyPart();
  83.  
  84. Multipart multipart = new MimeMultipart();
  85.  
  86. MimeBodyPart textPart = new MimeBodyPart();
  87. textPart.setText( MSG, "utf-8" );
  88. multipart.addBodyPart(textPart);
  89.  
  90. message.setContent(multipart);
  91.  
  92. messageBodyPart = new MimeBodyPart();
  93. String file = FILE1;
  94. String fileName = FILE1;
  95. DataSource source = new FileDataSource(file);
  96. messageBodyPart.setDataHandler(new DataHandler(source));
  97. messageBodyPart.setFileName(fileName);
  98. multipart.addBodyPart(messageBodyPart);
  99.  
  100. MimeBodyPart messageBodyPart2 = new MimeBodyPart();
  101.  
  102. String file2 = FILE2;
  103. String fileName2 = FILE2;
  104. DataSource source2 = new FileDataSource(file2);
  105. messageBodyPart2.setDataHandler(new DataHandler(source2));
  106. messageBodyPart2.setFileName(fileName2);
  107. multipart.addBodyPart(messageBodyPart2);
  108.  
  109. Transport.send(message);
  110.  
  111. System.out.println("Done");
  112.  
  113. } catch (MessagingException e) {
  114. throw new RuntimeException(e);
  115. }
  116. }
  117. catch( Exception e )
  118. {
  119. e.printStackTrace();
  120. }
  121. }
  122.  
  123. }
Add Comment
Please, Sign In to add comment