tags:

views:

51

answers:

2

im trying to send a mail using javamail api using the below code:when i compiled the class file im getting the below error which says 'must issue starttls command first' i have mentioned the error below. and also getProvider() function error i think so...i dont know what the errors mean.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.event.*;
import javax.mail.Authenticator;
import java.net.*;
import java.util.Properties;
public class mailexample 
    {
  public static void main (String args[]) throws Exception {

    String from = args[0];
    String to = args[1];
try
{
Properties props=new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host","smtp.gmail.com");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth", "true");
javax.mail.Authenticator authenticator = new javax.mail.Authenticator()
    {
    protected javax.mail.PasswordAuthentication getPasswordAuthentication() 
        {
        return new javax.mail.PasswordAuthentication("[email protected]", "pass");
    }
};
Session sess=Session.getDefaultInstance(props,authenticator);
sess.setDebug (true);
Transport transport =sess.getTransport ("smtp");
Message msg=new MimeMessage(sess);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject("Hello JavaMail");
msg.setText("Welcome to JavaMail");
transport.connect();
transport.send(msg);

}
catch(Exception e)
{
System.out.println("err"+e);
}
}
}

error:

C:\Users\bobby\Desktop>java mailexample [email protected] abc@gmail.
com

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s
mtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true

DEBUG: SMTPTransport trying to connect to host "smtp.gmail.com", port 25

DEBUG SMTP RCVD: 220 mx.google.com ESMTP q10sm12956046rvp.20

DEBUG: SMTPTransport connected to host "smtp.gmail.com", port: 25

DEBUG SMTP SENT: EHLO bobby-PC
DEBUG SMTP RCVD: 250-mx.google.com at your service, [60.243.184.29]
250-SIZE 35651584
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES


DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s
mtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true

DEBUG: SMTPTransport trying to connect to host "smtp.gmail.com", port 25

DEBUG SMTP RCVD: 220 mx.google.com ESMTP l29sm12930755rvb.16

DEBUG: SMTPTransport connected to host "smtp.gmail.com", port: 25

DEBUG SMTP SENT: EHLO bobby-PC
DEBUG SMTP RCVD: 250-mx.google.com at your service, [60.243.184.29]
250-SIZE 35651584
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES

DEBUG SMTP SENT: MAIL FROM:
DEBUG SMTP RCVD: 530 5.7.0 Must issue a STARTTLS command first. l29sm12930755rvb
.16

DEBUG SMTP SENT: QUIT
errjavax.mail.SendFailedException: Sending failed;
  nested exception is:
        javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command f
irst. l29sm12930755rvb.16
+1  A: 

Probably, you need to put props.put("mail.smtp.starttls.enable","true"); before you authenticate.

S.Mark
hhey im still having the same problem...same error..
bobby
A: 

See Must issue a STARTTLS command first. There is working code for connecting to Gmail. You need to enable starttls.

Matthew Flaschen