Code.GeekInterview.com
 
 

Java Code Base

 
Code Samples Java
 

Send email using Gmail SMTPS


Code ResourceAuthor: iamdvr  

Difficulty Level:

Published: 22nd Aug 2007   Read: 9553 times  

Filed in: Java
Add Comment


 

 

Sponsored Links


 

 

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 Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. srinivasaraobora130744
2. iamdvr97694
3. venki_madesh35134
4. Raju18488
5. parmod kumar duhan15756
6. Kiran.jakkaraju14058
7. chowsys7457
8. Venkateswara Rao6476
9. ashish.cns6348
10. Vamshidhar Matam5428

Active Coders

Refined Tags