Code.GeekInterview.com
 
 

Java Code Base

 
Code Samples Java
 

Send email using Gmail Mail Server


Code ResourceAuthor: iamdvr  

Difficulty Level:

Published: 6th Nov 2006   Read: 71165 times  

Filed in: Java
Add Comment


 

 

Sponsored Links


 

 

This is very good example ..just copy and paste and just change username and password in sendMail function call ... Add as much of to ,cc, bcc addresses.. Try now

 


Sample Code
  1.  
  2. //set CLASSPATH=%CLASSPATH%;activation.jar;mail.jar
  3. import javax.mail.*;
  4. import javax.mail.internet.*;
  5. import java.util.*;
  6.  
  7. public class Mail
  8. {
  9.          String  d_email = "iamdvr@gmail.com",
  10.             d_password = "****",
  11.             d_host = "smtp.gmail.com",
  12.             d_port  = "465",
  13.             m_to = "iamdvr@yahoo.com",
  14.             m_subject = "Testing",
  15.             m_text = "Hey, this is the testing email using smtp.gmail.com.";
  16.     public static void main(String[] args)
  17.     {
  18.                 String[] to={"XXX@yahoo.com"};
  19.                 String[] cc={"XXX@yahoo.com"};
  20.                 String[] bcc={"XXX@yahoo.com"};
  21.                 //This is for google
  22.                         Mail.sendMail("venkatesh@dfdf.com","password","smtp.gmail.com","465","true",
  23. "true",true,"javax.net.ssl.SSLSocketFactory","false",to,cc,bcc,
  24. "hi baba don't send virus mails..","This is my style...of reply..
  25. If u send virus mails..");             
  26.     }
  27.  
  28.         public synchronized static boolean sendMail(String userName,String passWord,String host,String port,String starttls,String auth,boolean debug,String socketFactoryClass,String fallback,String[] to,String[] cc,String[] bcc,String subject,String text){
  29.                 Properties props = new Properties();
  30.                 //Properties props=System.getProperties();
  31.         props.put("mail.smtp.user", userName);
  32.         props.put("mail.smtp.host", host);
  33.                 if(!"".equals(port))
  34.         props.put("mail.smtp.port", port);
  35.                 if(!"".equals(starttls))
  36.         props.put("mail.smtp.starttls.enable",starttls);
  37.         props.put("mail.smtp.auth", auth);
  38.                 if(debug){
  39.                 props.put("mail.smtp.debug", "true");
  40.                 }else{
  41.                 props.put("mail.smtp.debug", "false");         
  42.                 }
  43.                 if(!"".equals(port))
  44.         props.put("mail.smtp.socketFactory.port", port);
  45.                 if(!"".equals(socketFactoryClass))
  46.         props.put("mail.smtp.socketFactory.class",socketFactoryClass);
  47.                 if(!"".equals(fallback))
  48.         props.put("mail.smtp.socketFactory.fallback", fallback);
  49.  
  50.         try
  51.         {
  52.                         Session session = Session.getDefaultInstance(props, null);
  53.             session.setDebug(debug);
  54.             MimeMessage msg = new MimeMessage(session);
  55.             msg.setText(text);
  56.             msg.setSubject(subject);
  57.             msg.setFrom(new InternetAddress("p_sambasivarao@sutyam.com"));
  58.                         for(int i=0;i<to.length;i++){
  59.             msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));
  60.                         }
  61.                         for(int i=0;i<cc.length;i++){
  62.             msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
  63.                         }
  64.                         for(int i=0;i<bcc.length;i++){
  65.             msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));
  66.                         }
  67.             msg.saveChanges();
  68.                         Transport transport = session.getTransport("smtp");
  69.                         transport.connect(host, userName, passWord);
  70.                         transport.sendMessage(msg, msg.getAllRecipients());
  71.                         transport.close();
  72.                         return true;
  73.         }
  74.         catch (Exception mex)
  75.         {
  76.             mex.printStackTrace();
  77.                         return false;
  78.         }
  79.         }
  80.  
  81. }
  82.  
Copyright GeekInterview.com


Next Article: Find your day from your DOB


 

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    



Comments

Brilliantly coded, Thanks ! . But I'm still getting exception: "Could not connect to SMTP host: smtp.gmail.com, port: 25;"
Comment posted by: Gopal on 2007-07-12T09:10:25
I have run this code without error, If you can post full exception trace here, I may help then.

Regards.
Comment posted by: Ejaz Azeem on 2007-08-24T06:18:20
Excellent. works great, Thanks!
Comment posted by: Ashraf on 2007-10-01T13:34:15
Hi dude,
thanks for sending..........


Comment posted by: ashwin on 2007-11-19T13:45:37
Here is the stacktrace of the error i'm getting...

C:javamail>javac Mail.java

C:javamail>java Mail
DEBUG: setDebug: JavaMail version 1.4.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s
mtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, p
ort: 465;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:41
2)
at javax.mail.Service.connect(Service.java:288)
at javax.mail.Service.connect(Service.java:169)
at Mail.sendMail(Mail.java:72)
at Mail.main(Mail.java:20)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(Unknown Source)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(Unknown Source
)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:163)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
... 5 more


Please help back to rectify the error
Comment posted by: rahul on 2007-12-06T06:09:50
First piece of code that actually works out of the box. - Great Job
Comment posted by: Newb on 2008-03-01T00:47:57
Excellent, usseful and very informative,

Thanks lot.

RAMU,C.V.
Comment posted by: RAMU CHENGALPATTU VENUGOPAL on 2010-05-04T21:44:59
really good post..
it worked for me..
Thanks a lot...
Comment posted by: Prabhat on 2010-06-23T13:43:50
Please send me the Jasperreport code sample..Thanks for advance.
Comment posted by: sivakumar on 2010-08-27T08:05:56
Hello writer u are really a great.. I tried to send code for five days i'm so much tired to get answer.. really this code is great it works thanks a lot.. and one more obligation can u send me the code to send mail using java.net package pls
Comment posted by: Kannan on 2010-08-01T11:26:12
This is great!! have been looking all day for codes that works.. this one works right off the back! thanks!!!!
Comment posted by: nghia on 2011-06-29T14:48:21

Popular Coders

# Coder NameHits
1. srinivasaraobora241830
2. iamdvr138742
3. venki_madesh38621
4. Raju31394
5. Nidhi Saxena25937
6. Kiran.jakkaraju23438
7. parmod kumar duhan20823
8. yuvanmytri16723
9. venkat_kiran12882
10. chowsys11296

Active Coders

Refined Tags