Advertisement
Guest User

Untitled

a guest
May 3rd, 2018
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. package com.rodosaenz.smtp.client;
  2.  
  3. import java.util.Properties;
  4. import javax.mail.Message;
  5. import javax.mail.MessagingException;
  6. import javax.mail.PasswordAuthentication;
  7. import javax.mail.Session;
  8. import javax.mail.Transport;
  9. import javax.mail.internet.InternetAddress;
  10. import javax.mail.internet.MimeMessage;
  11.  
  12. /**
  13. *
  14. * @author Rodolfo
  15. */
  16. public class SmtpClientTextExample {
  17.  
  18. public static void main(String[] args) {
  19.  
  20. // Recipient's email ID needs to be mentioned.
  21. String to = "to@gmail.com";
  22.  
  23. // Sender's email ID needs to be mentioned
  24. String from = "from@gmail.com";
  25. final String username = "<YOUR-ACCOUNT>";//change accordingly
  26. final String password = "*******";//change accordingly
  27.  
  28. // Assuming you are sending email through relay.jangosmtp.net
  29. String host = "smtp.gmail.com";
  30.  
  31. Properties props = new Properties();
  32. props.put("mail.smtp.auth", "true");
  33. props.put("mail.smtp.starttls.enable", "true");
  34. props.put("mail.smtp.host", host);
  35. props.put("mail.smtp.port", "587");
  36.  
  37. Session session = Session.getInstance(props,
  38. new javax.mail.Authenticator() {
  39. protected PasswordAuthentication getPasswordAuthentication() {
  40. return new PasswordAuthentication(username, password);
  41. }
  42. });
  43.  
  44. try {
  45.  
  46. // Create a default MimeMessage object.
  47. Message message = new MimeMessage(session);
  48.  
  49. // Set From: header field of the header.
  50. message.setFrom(new InternetAddress(from));
  51.  
  52. // Set To: header field of the header.
  53. message.setRecipients(Message.RecipientType.TO,
  54. InternetAddress.parse(to));
  55.  
  56. // Set Subject: header field
  57. message.setSubject("Testing Java Text Email");
  58.  
  59. message.setText("Simple Text Email");
  60.  
  61. // Send message
  62. Transport.send(message);
  63.  
  64. System.out.println("Sent message successfully....");
  65.  
  66. } catch (MessagingException e) {
  67. throw new RuntimeException(e);
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement