Advertisement
Guest User

Email

a guest
Dec 16th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1.  
  2. package emailtest;
  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.BodyPart;
  9. import javax.mail.Message;
  10. import javax.mail.MessagingException;
  11. import javax.mail.Multipart;
  12. import javax.mail.PasswordAuthentication;
  13. import javax.mail.Session;
  14. import javax.mail.Transport;
  15. import javax.mail.internet.InternetAddress;
  16. import javax.mail.internet.MimeBodyPart;
  17. import javax.mail.internet.MimeMessage;
  18. import javax.mail.internet.MimeMultipart;
  19.  
  20. public class EmailTest {
  21. public static void main(String[] args) {
  22. // Recipient's email ID needs to be mentioned.
  23. String to = "bcarter@stu.tcehs.org";
  24.  
  25. // Sender's email ID needs to be mentioned
  26. String from = "DetentionTest@gmail.com";
  27.  
  28. final String username = "DetentionTest@gmail.com";//change accordingly
  29. final String password = "D3tent1onTest";//change accordingly
  30.  
  31. // Assuming you are sending email through relay.jangosmtp.net
  32. String host = "relay.jangosmtp.net";
  33.  
  34. Properties props = new Properties();
  35. props.put("mail.smtp.starttls.enable", "true");
  36. props.put("mail.smtp.auth", "true");
  37. props.put("mail.smtp.host", "smtp.gmail.com");
  38. props.put("mail.smtp.port", "587");
  39.  
  40. // Get the Session object.
  41. Session session = Session.getInstance(props,
  42. new javax.mail.Authenticator() {
  43. protected PasswordAuthentication getPasswordAuthentication() {
  44. return new PasswordAuthentication(username, password);
  45. }
  46. });
  47.  
  48. try {
  49. // Create a default MimeMessage object.
  50. Message message = new MimeMessage(session);
  51.  
  52. // Set From: header field of the header.
  53. message.setFrom(new InternetAddress(from));
  54.  
  55. // Set To: header field of the header.
  56. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
  57.  
  58. // Set Subject: header field
  59. message.setSubject("Testing Subject");
  60.  
  61. // Create the message part
  62. BodyPart messageBodyPart = new MimeBodyPart();
  63.  
  64. // Now set the actual message
  65. messageBodyPart.setText("This is message body");
  66.  
  67. // Create a multipar message
  68. Multipart multipart = new MimeMultipart();
  69.  
  70. // Set text message part
  71. multipart.addBodyPart(messageBodyPart);
  72.  
  73. // Part two is attachment
  74. messageBodyPart = new MimeBodyPart();
  75. String filename = "H:\\Documents\\My Pictures\\Piggy oink.jpg";
  76. DataSource source = new FileDataSource(filename);
  77. messageBodyPart.setDataHandler(new DataHandler(source));
  78. messageBodyPart.setFileName("test");
  79. multipart.addBodyPart(messageBodyPart);
  80.  
  81. // Send the complete message parts
  82. message.setContent(multipart);
  83.  
  84. // Send message
  85. Transport.send(message);
  86.  
  87. System.out.println("Sent message successfully....");
  88.  
  89. } catch (MessagingException e) {
  90. throw new RuntimeException(e);
  91. }
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement