1

I am using the following program for sending email through Java. When I use this program, I am not getting any error. At that same time I am not getting out it, means the mail is being not sent. I am trying to send message to Gmail. Are these smtp correct or not?

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

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

package mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class mail4 {

    private String from;
    private String to;
    private String subject;
    private String text;

    public mail4(String from, String to, String subject, String text){

        this.from = from;
        this.to = to;
        this.subject = subject;
        this.text = text;
    }

    public void send(){

        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "465");

        Session mailSession = Session.getDefaultInstance(props);
        Message simpleMessage = new MimeMessage(mailSession);

        InternetAddress fromAddress = null;
        InternetAddress toAddress = null;
        try {

            fromAddress = new InternetAddress(from);
            toAddress = new InternetAddress(to);
        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {

            simpleMessage.setFrom(fromAddress);
            simpleMessage.setRecipient(RecipientType.TO, toAddress);
            simpleMessage.setSubject(subject);
            simpleMessage.setText(text);
                        Transport.send(simpleMessage);


        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}




main class
***********

package mail;
public class mail5 {
public static void main(String[] args) {

        String from = "xxx@gmail.com";
        String to = "yyy@gmail.com";
        String subject = "Test";
        String message = "A test message";
        mail4 sendMail = new mail4(from, to, subject, message);
        sendMail.send();
    }
}
gprathour
  • 14,813
  • 5
  • 66
  • 90
fernandas
  • 33
  • 3
  • 10
  • ya both are correct but what error you are getting ? – Hemant Metalia Jan 24 '12 at 09:56
  • Am not getting any error.At that same time the mail was not reached "TO" address.when i type one print statement before Transport.send(simpleMessage);it will print .I try to print that same statement after Transport.send(simpleMessage); it will not printed. – fernandas Jan 24 '12 at 10:01
  • write catch (Exception e) { e.printStackTrace(); } and see the printstacktrace i hade come across such exception – Hemant Metalia Jan 24 '12 at 10:04

3 Answers3

0

try adding the following property:

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

If this doesn't work, check the following links:

http://www.velocityreviews.com/forums/t141237-send-smtp-mail-using-javamail-with-gmail-account.html

http://www.javaworld.com/javatips/jw-javatip115.html

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
  • When I use this line I have an following error javax.mail.AuthenticationFailedException at javax.mail.Service.connect(Service.java:267) at javax.mail.Service.connect(Service.java:137) at javax.mail.Service.connect(Service.java:86) at javax.mail.Transport.send0(Transport.java:150) at javax.mail.Transport.send(Transport.java:80) at mail.mail4.send(mail4.java:60) at mail.mail5.main(mail5.java:15) – fernandas Jan 24 '12 at 10:03
  • @fernandas In the first link I have sent you, check out the last answer it has a complete code with authentication – Adel Boutros Jan 24 '12 at 10:23
0

I think the port is 587 unless you're using TLS (for which 465) is correct. You're not providing any authentication data: Try this:

Session mailSession = Session.getDefaultInstance(props, 
  new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication("xxxxxx", "xxxxxx");
    }
  }
 );
Peter Svensson
  • 6,105
  • 1
  • 31
  • 31
0

EDIT: I misread the code. You need to use port 587 since it's the GMail's SMTP port. Also you can edit yoru props as follows:

String host = "smtp.gmail.com";
int port = 587;
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Korhan Ozturk
  • 11,148
  • 6
  • 36
  • 49
  • I got this error javax.mail.AuthenticationFailedException at javax.mail.Service.connect(Service.java:267) at javax.mail.Service.connect(Service.java:137) at javax.mail.Service.connect(Service.java:86) at javax.mail.Transport.send0(Transport.java:150) at javax.mail.Transport.send(Transport.java:80) at mail.mail4.send(mail4.java:65) at mail.mail5.main(mail5.java:15) – fernandas Jan 24 '12 at 10:07
  • Try once more with username and password given to the Transport object `String username = "username";` `String password = "password";` ... `Transport transport = session.getTransport("smtp");` `transport.connect(host, port, username, password);` `Transport.send(message);` – Korhan Ozturk Jan 24 '12 at 12:01