Code.GeekInterview.com
  I am new, Sign me up!
 
Code Samples Java
 

Send email using Gmail SMTPS


Code ResourceAuthor: iamdvr  

Difficulty Level:

Published: 22nd Aug 2007   Read: 3337 times  

Filed in: Java
Add Comment


 


Gmail updated their smtp way of sending mail...
Connection time out error occurs for normal way of sending mails...
1)Where activation.jar and mail.jar should be in the same folder of java and class files

2) Go to the directory in command prompt or shell */ javac -d . -classpath activation.jar;mail.jar SimpleSSLMail.java java -classpath ./classes;;./lib/activation.jar;./lib/mail.jar;./lib/rowset.jar SimpleSSLMail

 

 


Sample Code
  1.  
  2. import javax.mail.*;
  3. import javax.mail.internet.*;
  4. import java.util.Properties;
  5. public class SimpleSSLMail {
  6.  
  7.     private static final String SMTP_HOST_NAME = "smtp.gmail.com";
  8.     private static final int SMTP_HOST_PORT = 465;
  9.     private static final String SMTP_AUTH_USER = "myaccount@gmail.com";
  10.     private static final String SMTP_AUTH_PWD  = "mypwd";
  11.  
  12.     public static void main(String[] args) throws Exception{
  13.        new SimpleSSLMail().test();
  14.     }
  15.  
  16.     public void test() throws Exception{
  17.         Properties props = new Properties();
  18.  
  19.         props.put("mail.transport.protocol", "smtps");
  20.         props.put("mail.smtps.host", SMTP_HOST_NAME);
  21.         props.put("mail.smtps.auth", "true");
  22.         props.put("mail.smtps.quitwait", "false");
  23.  
  24.         Session mailSession = Session.getDefaultInstance(props);
  25.         mailSession.setDebug(true);
  26.         Transport transport = mailSession.getTransport();
  27.  
  28.         MimeMessage message = new MimeMessage(mailSession);
  29.         message.setSubject("Testing SMTP-SSL");
  30.         message.setContent("This is a test", "text/plain");
  31.  
  32.         message.addRecipient(Message.RecipientType.TO,
  33.              new InternetAddress("elvis@presley.org"));
  34.  
  35.         transport.connect
  36.           (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
  37.  
  38.         transport.sendMessage(message,
  39.             message.getRecipients(Message.RecipientType.TO));
  40.         transport.close();
  41.     }
  42. }
  43.  
Copyright GeekInterview.com


Next Article: Java Newsticker


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Comment


Members Please Login

Name:  Email: (Optional. Used for Notification)

Title:
 
Comment:
Validation Code: <=>  (Enter this code in text box)





Popular Coders

# Coder NameHits
1. iamdvr35751
2. venki_madesh30853
3. srinivasaraobora22477
4. bora_srinivasarao11728
5. Kiran.jakkaraju4609
6. parmod kumar duhan4502
7. chowsys3970
8. Venkateswara Rao3713
9. Raju3390
10. Vamshidhar Matam2290

Active Coders

Refined Tags

 

Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact  

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape