How to Fix Errors in Gmail SMTP Java Code?

  • Context: Java 
  • Thread starter Thread starter zak100
  • Start date Start date
  • Tags Tags
    Email
Click For Summary
SUMMARY

The discussion focuses on resolving compilation errors in a Java program that utilizes Gmail's SMTP server for sending emails. The primary issues stem from missing imports for the JavaMail API, specifically the javax.mail package. To fix the errors, users must ensure they have the JavaMail library included in their project dependencies, either by downloading the JAR files or by using a build tool like Maven or Gradle to manage dependencies. The correct setup will eliminate the "cannot find symbol" errors related to classes such as Session, Message, and Transport.

PREREQUISITES
  • Understanding of Java programming and syntax
  • Familiarity with JavaMail API for email functionality
  • Knowledge of dependency management in Java (Maven or Gradle)
  • Basic concepts of SMTP and email protocols
NEXT STEPS
  • Learn how to add JavaMail API as a dependency using Maven or Gradle
  • Explore JavaMail API documentation for detailed usage and examples
  • Investigate common SMTP configuration settings for Java applications
  • Review error handling best practices in Java for messaging applications
USEFUL FOR

Java developers, software engineers, and anyone looking to implement email functionality in Java applications using Gmail's SMTP service.

zak100
Messages
462
Reaction score
11
Hi,
I want to write a program to email using gmail smtp. I got following code from internet.

Java:
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 = "[EMAIL]yourmail@gmail.com[/EMAIL]";

          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("[EMAIL]yourmail@gmail.com[/EMAIL]"));

            message.setRecipients(Message.RecipientType.TO,

                    InternetAddress.parse("[EMAIL]test@gmail.com[/EMAIL]"));

            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);

        }

    }
}
Its giving me following errors:

Java:
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("[EMAIL]yourmail@gmail.com[/EMAIL]"));
                                ^
  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("[EMAIL]test@gmail.com[/EMAIL]"));
                    ^
  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

>

Some body please guide me how to fix these errors.
Urgent please.
Zulfi.
 
Last edited by a moderator:
Technology news on Phys.org
It would be easier for people to help if you used the code blocks and removed all of the extra line spaces.
When I googled "Created by anirudh on 28/10/14" I got this page -
Send Email with Gmail in Java Example
Is this the example that you're trying to make work?
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 2 ·
Replies
2
Views
12K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 6 ·
Replies
6
Views
30K
  • · Replies 3 ·
Replies
3
Views
13K