Forum Stats

  • 3,734,333 Users
  • 2,246,951 Discussions
  • 7,857,228 Comments

Discussions

How to send email using gmail smtp

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Jun 3, 2016 2:11AM in New To Java

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 = "[email protected]";

          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 protected]"));

            message.setRecipients(Message.RecipientType.TO,

                    InternetAddress.parse("[email protected]"));

            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("[email protected]"));

                                ^

  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 protected]"));

                    ^

  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.

eudriscabrera-JavaNet

Answers

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited May 12, 2016 5:46AM
  • Unknown
    edited May 12, 2016 1:46PM
    Some body please guide me about a better email program.
    

    The problem is NOT the 'email program'.

    The problem in this thread, as with your other thread, is that you are trying to use Java when you haven't learned HOW to use it.

    I suggest you STOP trying to write code before you know the language and how it works.

    The Java Tutorials has trails on how to use ALL of the basic functionality.

    https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

    Adding Classes to the JAR File's Classpath

    You may need to reference classes in other JAR files from within a JAR file.
    

    If you want to reference a class in your code the class MUST BE on the classpath or the jar that contains the class must be in the classpath.

    That is one of the FIRST THINGS you learn when you take any basic Java class.

    You will continue to have problems if you don't learn the language BEFORE you try to write code.

    There are NO SHORTCUTS.

    eudriscabrera-JavaNet
  • 836548
    836548 Member Posts: 286
    edited May 23, 2016 6:38AM

    I would suggest first write simple programs, and learn the basics of java, for example how to add the classpath etc..

    One more thing don't just try to run the downloaded programs, without understanding what that does, and reading the readMe file (pre-requisite to run the program) to compile and execute it .

  • Ashish Awasthi
    Ashish Awasthi Member Posts: 3,388 Bronze Crown
    edited Jun 3, 2016 2:11AM

    Have you added required jar files in project class path ?

    Check this code-

    1. import java.util.Properties; 
    2. import javax.mail.*; 
    3. import javax.mail.internet.*; 
    4.  
    5. public class SendMailSSL { 
    6. public static void main(String[] args) { 
    7.  
    8. String to="[email protected]";//change accordingly 
    9.  
    10.   //Get the session object 
    11.   Properties props = new Properties(); 
    12.   props.put("mail.smtp.host", "smtp.gmail.com"); 
    13.   props.put("mail.smtp.socketFactory.port", "465"); 
    14.   props.put("mail.smtp.socketFactory.class", 
    15.             "javax.net.ssl.SSLSocketFactory"); 
    16.   props.put("mail.smtp.auth", "true"); 
    17.   props.put("mail.smtp.port", "465"); 
    18.   
    19.   Session session = Session.getDefaultInstance(props, 
    20.    new javax.mail.Authenticator() { 
    21.    protected PasswordAuthentication getPasswordAuthentication() { 
    22.    return new PasswordAuthentication("[email protected]","password");//change accordingly 
    23.    } 
    24.   }); 
    25.   
    26.   //compose message 
    27.   try { 
    28.    MimeMessage message = new MimeMessage(session); 
    29.    message.setFrom(new InternetAddress("[email protected]"));//change accordingly 
    30.    message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); 
    31.    message.setSubject("Hello"); 
    32.    message.setText("Testing......."); 
    33.     
    34.    //send message 
    35.    Transport.send(message); 
    36.  
    37.    System.out.println("message sent successfully"); 
    38.   
    39.   } catch (MessagingException e) {throw new RuntimeException(e);} 
    40.   

    and go through this -  http://www.javatpoint.com/example-of-sending-email-using-java-mail-api-through-gmail-server


    Ashish

This discussion has been closed.