Sending Emails in Java using GMail ID

Thought of sharing following code with you all. It is a small code snippet that uses SMTP in Java to login into GMail and send email using ones GMail account.

String host = "smtp.gmail.com";
    String from = "username";
    String pass = "password";
    Properties props = System.getProperties();
    props.put("mail.smtp.starttls.enable", "true"); // added this line
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");

    String[] to = {"to@gmail.com"}; // added this line

    Session session = Session.getDefaultInstance(props, null);
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));

    InternetAddress[] toAddress = new InternetAddress[to.length];

    // To get the array of addresses
    for( int i=0; i < to.length; i++ ) { // changed from a while loop
        toAddress[i] = new InternetAddress(to[i]);
    }
    System.out.println(Message.RecipientType.TO);

    for( int i=0; i < toAddress.length; i++) { // changed from a while loop
        message.addRecipient(Message.RecipientType.TO, toAddress[i]);
    }
    message.setSubject("sending in a group");
    message.setText("Welcome to JavaMail");
    Transport transport = session.getTransport("smtp");
    transport.connect(host, from, pass);
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();

The code is pretty much self explanatory. Add your GMail username and password in line 7 and 8.

props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);

And also add the recepient email address in to array in line 12.

String[] to = {"to@gmail.com"}; // added this line


11 Comments

  • manoj wrote on 4 August, 2009, 10:53

    hello

  • manoj wrote on 4 August, 2009, 10:54

    hay i am patel manoj

  • Sivasubramaniam Arunachalam wrote on 30 November, 2009, 6:05

    Can you give us the associated JARs or the complete import statements?

  • suesi Tran wrote on 29 December, 2009, 14:58

    Please send the jar file to my mail too: embedzit@gmail.com

  • Hitesh wrote on 5 November, 2010, 23:53

    hey thanks for the Code dear.. its Totally workin nd onw thin is that I was lookin for this thisn since a long time but here i got the Perfect code that is Workin so that thanks a lot.. ! Take care Bye Bye

  • Abhi wrote on 25 May, 2011, 0:26

    Thanks a lot for the snippet, works great. Was looking something like that for my project, you saved my time.

  • Mohit wrote on 4 September, 2011, 19:34

    I’m trying to run this snippet but got the following Exception
    Please help me:-

    javax.servlet.ServletException: javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. u2sm14732770pbq.9

    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:852)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
    org.apache.jsp.test_005fmail.send_jsp._jspService(send_jsp.java:133)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    com.vtr.globarena.URLCheckFilter.doFilter(URLCheckFilter.java:175)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

  • MuthuKumar wrote on 19 October, 2011, 12:53

    how can i run this program using commandprompt?
    how to set paths for jar files?
    pls help.

  • Nagaraj wrote on 14 November, 2011, 11:27

    Provide me the jar files used

  • dinesh wrote on 14 December, 2011, 12:14

    search for mail.jar and activation.jar

  • DURGESH wrote on 27 January, 2012, 11:01

    THANKS A LOT !!!!!!!!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Copyright © 2012 ViralPatel.net. All rights reserved.