1

The following is the program, which I am trying to send e-mail. The code is error free and I don't get any run time exception. But the code is unable to send e-mail. I have revised this code a lot but can't get what is actually wrong. The sender and the receiver both have GMail accounts. The sender has 2-step verification process disabled. (I don't think it matters for the receiver. Does it?)

The code :

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

class tester {
   public static void main(String args[]) {
      Properties props = new Properties();
      props.put("mail.smtp.host" , "smtp.gmail.com");
      props.put("mail.stmp.user" , "username"); // username or complete address ! Have tried both
      Session session  = Session.getDefaultInstance( props , null);
      String to = "me@gmail.com";
      String from = "from@gmail.com";
      String subject = "Testing...";
      Message msg = new MimeMessage(session);
         try {
           msg.setFrom(new InternetAddress(from));
           msg.setRecipient(Message.RecipientType.TO , new InternetAddress(to));
           msg.setSubject(subject);
           msg.setText("Working fine..!");
           System.out.println("fine!!??");
         }  catch(Exception exc) {
                System.out.println(exc);
            }
    }
}
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328

2 Answers2

4

Well, your code doesn't actually attempt to send the message. Take a look at Transport.send.

Here are some examples:

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • yes. After `Transport.send()` i get this exception during runtime `com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. c5sm1092784p bh.94` – Suhail Gupta Jul 07 '11 at 10:57
1

First of all, you forgot to call Transport.send() to send your MimeMessage.

Secondly, GMail needs to be configured to use TLS or SSL connection. The following needs to be added to your Properties (props):

//To use TLS
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");

//To use SSL
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");

To connect to GMail SMTP, use the Transport.connect() method. I see you are not using any Transport at all in your code, so add this:

Transport transport = session.getTransport();

//Connect to GMail
transport.connect("smtp.gmail.com", 465, "USERNAME_HERE", "PASSWORD_HERE");
transport.send(msg);

Alternatively, you can create a Session by including a javax.mail.Authenticator as a parameter.

Example:

Session session = Session.getDefaultInstance(props,
    new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("USERNAME_HERE", "PASSWORD_HERE");
        }
    });

I hope this helps you.

Resources:

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • after adding these and adding the statement `props.put("mail.smtp.password", "password")`; i get the following exception **javax.mail.AuthenticationFailedException: failed to connect, no password specified?** why do i get this ? – Suhail Gupta Jul 07 '11 at 11:24
  • Specify username and password in transport.connect() call: – maximdim Jul 07 '11 at 12:36
  • @ maximdim that produces the same exception. I did like this `Transport transport = session.getTransport("smtp"); transport.connect("smtp.gmail.com", "username", "password"); Transport.send(msg);` – Suhail Gupta Jul 07 '11 at 12:50
  • @ The Elite Gentleman I get the same exception . EDIT your post to include `"smtp"` in the arguments of getTransport(). – Suhail Gupta Jul 07 '11 at 12:59
  • @Suhail Gupta, I don't have to, since (if I configure my properties correctly, I can do [`session.getTransport()`](http://javamail.kenai.com/nonav/javadocs/javax/mail/Session.html#getTransport%28%29). – Buhake Sindi Jul 07 '11 at 13:05
  • @ The Elite Gentleman Does it make a difference if i pass `full email address` or just the `username` in the username spot – Suhail Gupta Jul 07 '11 at 13:14
  • @Suhail Gupta, try both ways and see which one works for you. – Buhake Sindi Jul 07 '11 at 13:15
  • Update your post with full source code and exception you're getting. – Buhake Sindi Jul 07 '11 at 13:31
  • @ The Elite Gentleman please visit http://stackoverflow.com/questions/6610572/javax-mail-authenticationfailedexception-failed-to-connect-no-password-specifie – Suhail Gupta Jul 07 '11 at 13:36
  • This answer though complete was still incomplete because Authentication was required.The complete answer is at http://stackoverflow.com/questions/6610572/javax-mail-authenticationfailedexception-failed-to-connect-no-password-specifie – Suhail Gupta Jul 07 '11 at 16:32
  • @Suhail Gupta, what I wrote was missing pieces to your code and I did include `javax.mail.Authenticator`. – Buhake Sindi Jul 07 '11 at 20:06
  • @ The Elite Gentleman The last snippet of your answer , i don't understand it . When is the method `getPasswordAuthentication()` called ? – Suhail Gupta Jul 08 '11 at 04:59
  • It's called by the Session for authentication. – Buhake Sindi Jul 08 '11 at 05:17
  • @ The Elite Gentleman but we are sending the password and username using this statement also `transport.send( "smtp.gmail.com " , port ,"username" , "password");` Then why do we require the authentication method ? – Suhail Gupta Jul 08 '11 at 05:55
  • @SuhailGupta let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1255/discussion-between-the-elite-gentleman-and-suhail-gupta) – Buhake Sindi Jul 08 '11 at 06:09