Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to Send emails from Google Mail using JavaMail API

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Smile How to Send emails from Google Mail using JavaMail API

    With this code you can send emails through your Google Mail account using the JavaMail API.

    Firstly, if you don't already have the JavaMail API, visit JavaMail API and download it.

    If any of you need help importing the API, please post.

    Here is the code. Update the code with your Google Mail account details.

    import javax.mail.*;
    import javax.mail.internet.*;
    import java.util.Properties;
     
    public class SimpleGoogleEmail {
     
        private static final String SMTP_HOST_NAME = "smtp.gmail.com";
        private static final int SMTP_HOST_PORT = 465;
        private static final String SMTP_AUTH_USER = "username@gmail.com";
        private static final String SMTP_AUTH_PWD  = "yourPassword";
     
        public static void main(String[] args) throws Exception{
           new SimpleGoogleEmail().test();
        }
     
        public void test() throws Exception{
            Properties props = new Properties();
     
            props.put("mail.transport.protocol", "smtps");
            props.put("mail.smtps.host", SMTP_HOST_NAME);
            props.put("mail.smtps.auth", "true");
            // props.put("mail.smtps.quitwait", "false");
     
            Session mailSession = Session.getDefaultInstance(props);
            mailSession.setDebug(true);
            Transport transport = mailSession.getTransport();
     
            MimeMessage message = new MimeMessage(mailSession);
            // message subject
            message.setSubject("Java Programming Forums");
            // message body
            message.setContent("Hey! Visit http://www.JavaProgrammingForums.com", "text/plain");
     
            message.addRecipient(Message.RecipientType.TO,
                 new InternetAddress("from@emailaddress.com"));
     
            transport.connect
              (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
     
            transport.sendMessage(message,
                message.getRecipients(Message.RecipientType.TO));
            transport.close();
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  2. The Following 2 Users Say Thank You to JavaPF For This Useful Post:

    Time (November 14th, 2010), xivshin (May 8th, 2011)


  3. #2
    Member
    Join Date
    May 2010
    Posts
    39
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: How to Send emails from Google Mail using JavaMail API

    Just showing some love for this post I've always messed around with the mail api, but never could figure out the connect method

    Thanks

    EDIT:

    For people using the newer version of Mail api use:
    		MimeBodyPart mbp = new MimeBodyPart();
    		mbp.setText(body);
    		Multipart mp = new MimeMultipart();
    		mp.addBodyPart(mbp);
    		message.setContent(mp);

    To set the body/message

  4. The Following User Says Thank You to Time For This Useful Post:

    JavaPF (February 21st, 2011)

  5. #3
    Member
    Join Date
    Nov 2011
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to Send emails from Google Mail using JavaMail API

    Quote Originally Posted by Time View Post

    For people using the newer version of Mail api use:
    		MimeBodyPart mbp = new MimeBodyPart();
    		mbp.setText(body);
    		Multipart mp = new MimeMultipart();
    		mp.addBodyPart(mbp);
    		message.setContent(mp);

    To set the body/message
    The old way still works. There is no need to use MultiPart if the email is plain text and does not contain attachments or inlines.

    Spring 3
    Last edited by cafeteria84; January 22nd, 2012 at 04:06 PM.

  6. The Following User Says Thank You to cafeteria84 For This Useful Post:

    JavaPF (March 3rd, 2012)

  7. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Send emails from Google Mail using JavaMail API

    Thank you all for your answers. I've now figured it out.

Similar Threads

  1. JavaMail and Gmail
    By khushbu in forum Java Servlet
    Replies: 2
    Last Post: May 21st, 2010, 01:11 AM
  2. google search
    By nasi in forum Java Theory & Questions
    Replies: 7
    Last Post: April 2nd, 2010, 03:13 AM
  3. Problem with JavaMail... HELP! :(
    By danmcm88 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 18th, 2010, 04:11 PM
  4. Javamail attachment filenames
    By johniem in forum Java Theory & Questions
    Replies: 0
    Last Post: February 3rd, 2010, 07:51 AM
  5. Javamail
    By johniem in forum Java Theory & Questions
    Replies: 1
    Last Post: February 3rd, 2010, 07:42 AM