Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How to send email using gmail smtp

Zulfi KhanMay 12 2016 — edited Jun 3 2016

Hi,

I want to send email using gmail smtp. I downloded a program from internet but its producing lots of errors. The code is given below along with errors:

import javax.mail.*;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

import java.util.Properties;

/**

10

* Created by anirudh on 28/10/14.

11

*/

public class EmailExample {

    public static void main(String args[]) {

          final String username = "yourmail@gmail.com";

          final String password = "yourpassword";

        Properties props = new Properties();

        props.put("mail.smtp.auth", "true");

        props.put("mail.smtp.starttls.enable", "true");

        props.put("mail.smtp.host", "smtp.gmail.com");

        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,

                new javax.mail.Authenticator() {

                    protected PasswordAuthentication getPasswordAuthentication() {

                        return new PasswordAuthentication(username, password);

                    }

                });

        try {

            Message message = new MimeMessage(session);

            message.setFrom(new InternetAddress("yourmail@gmail.com"));

            message.setRecipients(Message.RecipientType.TO,

                    InternetAddress.parse("test@gmail.com"));

            message.setSubject("Test JCG Example");

            message.setText("Hi," +

                    "This is a Test mail for JCG Example!");

            Transport.send(message);

          System.out.println("Mail sent succesfully!");

        } catch (MessagingException e) {

            throw new RuntimeException(e);

        }

    }

I am getting following errors:

import javax.mail.*;

^

import javax.mail.internet.MimeMessage;

                          ^

EmailExample.java:48: error: cannot find symbol

        Session session = Session.getInstance(props,

        ^

  symbol:   class Session

  location: class EmailExample

EmailExample.java:50: error: package javax.mail does not exist

                new javax.mail.Authenticator() {

                              ^

EmailExample.java:48: error: cannot find symbol

        Session session = Session.getInstance(props,

                          ^

  symbol:   variable Session

  location: class EmailExample

EmailExample.java:66: error: cannot find symbol

            Message message = new MimeMessage(session);

            ^

  symbol:   class Message

  location: class EmailExample

EmailExample.java:66: error: cannot find symbol

            Message message = new MimeMessage(session);

                                  ^

  symbol:   class MimeMessage

  location: class EmailExample

EmailExample.java:68: error: cannot find symbol

            message.setFrom(new InternetAddress("yourmail@gmail.com"));

                                ^

  symbol:   class InternetAddress

  location: class EmailExample

EmailExample.java:70: error: package Message does not exist

            message.setRecipients(Message.RecipientType.TO,

                                         ^

EmailExample.java:72: error: cannot find symbol

                    InternetAddress.parse("test@gmail.com"));

                    ^

  symbol:   variable InternetAddress

  location: class EmailExample

EmailExample.java:81: error: cannot find symbol

            Transport.send(message);

            ^

  symbol:   variable Transport

  location: class EmailExample

EmailExample.java:89: error: cannot find symbol

        } catch (MessagingException e) {

                 ^

  symbol:   class MessagingException

  location: class EmailExample

13 errors

l>

Some body please guide me about a better email program.

Zulfi.

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 1 2016
Added on May 12 2016
5 comments
2,281 views