How to email using gmail smtp?

  • Java
  • Thread starter zak100
  • Start date
  • Tags
    Email
In summary, the conversation is about someone trying to write a program to send emails using Gmail's SMTP server. They have found a code example online and are encountering multiple errors while trying to make it work. They are seeking urgent help and have provided a link to the code example they are using.
  • #1
zak100
462
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
  • #2
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?
 

1. How do I set up a Gmail SMTP server?

To set up a Gmail SMTP server, you first need to log into your Gmail account and go to the "Settings" section. Then, click on the "Accounts and Import" tab and scroll down to the "Send mail as" section. Click on "Add another email address" and enter the email address you want to use. Click "Next" and then select "Send through Gmail (easier to set up)" as the SMTP server. Follow the prompts to complete the setup process.

2. What is the SMTP server address for Gmail?

The SMTP server address for Gmail is "smtp.gmail.com". This is the server that will handle the outgoing emails from your Gmail account. Make sure to use this address when setting up your email client or application.

3. How do I enable SMTP authentication for Gmail?

To enable SMTP authentication for Gmail, you need to go to the "Settings" section in your Gmail account. Then, click on the "Accounts and Import" tab and scroll down to the "Send mail as" section. Click on "Edit info" next to the email address you want to use and check the box next to "Treat as an alias". Then, click on "Next Step" and select "Send through Gmail (easier to set up)" as the SMTP server. Check the box next to "Use SMTP authentication" and enter your Gmail username and password. Click "Save Changes" to enable SMTP authentication.

4. What port should I use for Gmail SMTP?

The recommended port for Gmail SMTP is 587. This is the default port that is used for secure outgoing email with TLS encryption. However, you can also use port 465 if you want to use SSL encryption. Make sure to select the appropriate encryption option in your email client or application.

5. How do I test my Gmail SMTP connection?

To test your Gmail SMTP connection, you can use a tool like Telnet or a third-party email testing service. With Telnet, you can connect to the Gmail SMTP server and send a test email. If the email is successfully sent, then your connection is working. Alternatively, you can use an email testing service that will simulate sending an email from your Gmail account and provide a report on the delivery status.

Similar threads

  • Programming and Computer Science
Replies
1
Views
805
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
Replies
2
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
4K
  • Programming and Computer Science
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top