Java Code Base

Code Samples Java

Send email using Gmail Mail Server

Code ResourceAuthor: iamdvr  

Difficulty Level:

Published: 6th Nov 2006   Read: 77105 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
  2. //set CLASSPATH=%CLASSPATH%;activation.jar;mail.jar
  3. import javax.mail.*;
  4. import javax.mail.internet.*;
  5. import java.util.*;
  7. public class Mail
  8. {
  9.          String  d_email = "",
  10.             d_password = "****",
  11.             d_host = "",
  12.             d_port  = "465",
  13.             m_to = "",
  14.             m_subject = "Testing",
  15.             m_text = "Hey, this is the testing email using";
  16.     public static void main(String[] args)
  17.     {
  18.                 String[] to={""};
  19.                 String[] cc={""};
  20.                 String[] bcc={""};
  21.                 //This is for google
  22.                         Mail.sendMail("","password","","465","true",
  23. "true",true,"","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.     }
  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("", 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);
  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(""));
  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.         }
  81. }

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: 


Brilliantly coded, Thanks ! . But I'm still getting exception: "Could not connect to SMTP host:, 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.

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>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 "", port 465, isSSL false
javax.mail.MessagingException: Could not connect to SMTP host:, p
ort: 465;
nested exception is: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(
at com.sun.mail.smtp.SMTPTransport.protocolConnect(
at javax.mail.Service.connect(
at javax.mail.Service.connect(
at Mail.sendMail(
at Mail.main(
Caused by: Connection timed out: connect
at Method)
at Source)
at Source)
at Source)
at Source)
at Source)
at Source)
at Source
at com.sun.mail.util.SocketFetcher.createSocket(
at com.sun.mail.util.SocketFetcher.getSocket(
at com.sun.mail.smtp.SMTPTransport.openServer(
... 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.

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 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. srinivasaraobora305814
2. iamdvr157925
3. venki_madesh41335
4. Raju38395
5. Kiran.jakkaraju30561
6. Nidhi Saxena30306
7. yuvanmytri29953
8. parmod kumar duhan22962
9. venkat_kiran18948
10. chowsys14641

Active Coders

Refined Tags