Advertisement
Guest User

Untitled

a guest
Sep 5th, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. import java.util.Properties;
  2. import javax.mail.*;
  3. import javax.mail.internet.*;
  4.  
  5. public class SendMailSSL {
  6. public static void main(String[] args){
  7.  
  8. String to="ganesh.datapoint@gmail.com";//change accordingly
  9.  
  10. //Get the session object
  11. Properties props = new Properties();
  12. props.put("mail.smtp.host", "smtp.gmail.com");
  13. props.put("mail.smtp.socketFactory.port", "465");
  14. props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
  15. props.put("mail.smtp.auth", "true");
  16. props.put("mail.smtp.port", "465");
  17.  
  18. Session session = Session.getDefaultInstance(props,
  19. new javax.mail.Authenticator() {
  20. protected PasswordAuthentication getPasswordAuthentication() {
  21. return new PasswordAuthentication("ganesh.datapoint@gmail.com","****");//change accordingly
  22. }
  23. });
  24.  
  25. //compose message
  26. try {
  27. MimeMessage message = new MimeMessage(session);
  28. message.setFrom(new InternetAddress("ganesh.datapoint@gmail.com"));//change accordingly
  29. message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
  30. message.setSubject("Hello");
  31. message.setText("Testing.......");
  32.  
  33. //send message
  34. Transport.send(message);
  35.  
  36. System.out.println("message sent successfully");
  37.  
  38. } catch (MessagingException e) {throw new RuntimeException(e);}
  39.  
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement