code_junkie

Access gmail from Java

Nov 13th, 2011
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. import javax.mail.*;
  2. import javax.mail.internet.*;
  3.  
  4. import java.util.Properties;
  5.  
  6.  
  7. public class SimpleSSLMail {
  8.  
  9. private static final String SMTP_HOST_NAME = "smtp.gmail.com";
  10. private static final int SMTP_HOST_PORT = 465;
  11. private static final String SMTP_AUTH_USER = "[email protected]";
  12. private static final String SMTP_AUTH_PWD = "mypwd";
  13.  
  14. public static void main(String[] args) throws Exception{
  15. new SimpleSSLMail().test();
  16. }
  17.  
  18. public void test() throws Exception{
  19. Properties props = new Properties();
  20.  
  21. props.put("mail.transport.protocol", "smtps");
  22. props.put("mail.smtps.host", SMTP_HOST_NAME);
  23. props.put("mail.smtps.auth", "true");
  24. // props.put("mail.smtps.quitwait", "false");
  25.  
  26. Session mailSession = Session.getDefaultInstance(props);
  27. mailSession.setDebug(true);
  28. Transport transport = mailSession.getTransport();
  29.  
  30. MimeMessage message = new MimeMessage(mailSession);
  31. message.setSubject("Testing SMTP-SSL");
  32. message.setContent("This is a test", "text/plain");
  33.  
  34. message.addRecipient(Message.RecipientType.TO,
  35. new InternetAddress("[email protected]"));
  36.  
  37. transport.connect
  38. (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
  39.  
  40. transport.sendMessage(message,
  41. message.getRecipients(Message.RecipientType.TO));
  42. transport.close();
  43. }
  44. }
Add Comment
Please, Sign In to add comment