Advertisement
Guest User

Untitled

a guest
Feb 27th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. import javax.mail.Message;
  2. import javax.mail.MessagingException;
  3. import javax.mail.Session;
  4. import javax.mail.Transport;
  5. import javax.mail.internet.InternetAddress;
  6. import javax.mail.internet.MimeMessage;
  7. import java.util.Properties;
  8.  
  9. public class SendEmail {
  10.  
  11. public static void main(String... args) {
  12. SendEmail sendEmail=new SendEmail();
  13. sendEmail.send();
  14. }
  15.  
  16. public void send() {
  17. // Recipient's email ID needs to be mentioned.
  18. String to = "talehsmail@gmail.com";
  19.  
  20. // Sender's email ID needs to be mentioned
  21. String from = "nurlanagayev307@gmail.com";
  22.  
  23. // Assuming you are sending email from localhost
  24. String host = "smtp.gmail.com";
  25.  
  26. // Get system properties
  27. Properties properties = System.getProperties();
  28.  
  29. // Setup mail server
  30. properties.setProperty("mail.smtp.host", host);
  31. properties.put("mail.smtp.port", "587");
  32.  
  33. properties.put("mail.smtp.starttls.enable","true");
  34. properties.put("mail.smtp.auth", "true"); // If you need to authenticate
  35. properties.put("mail.smtp.socketFactory.port", 587);
  36. properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  37. properties.put("mail.smtp.socketFactory.fallback", "false");
  38.  
  39.  
  40. // Get the default Session object.
  41. Session session = Session.getDefaultInstance(properties);
  42.  
  43. try {
  44. // Create a default MimeMessage object.
  45. MimeMessage message = new MimeMessage(session);
  46.  
  47. // Set From: header field of the header.
  48. message.setFrom(new InternetAddress(from));
  49.  
  50. // Set To: header field of the header.
  51. message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  52.  
  53. // Set Subject: header field
  54. message.setSubject("This is the Subject Line!");
  55.  
  56. // Now set the actual message
  57. message.setText("This is actual message");
  58.  
  59. // Send message
  60. Transport t = session.getTransport("smtps");
  61.  
  62. t.connect(host, "nurlanagayev307@gmail.com", "kn-8r0th3r5");
  63. System.out.println("Sent message successfully....");
  64. } catch (MessagingException mex) {
  65. mex.printStackTrace();
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement