Regarding Java Mail API
hi,
i am vijay. I have used the below program. i am facing an error of
Exception in thread "main" javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first z15sm2857307pod
at javax.mail.Transport.send0(Transport.java:219)
at javax.mail.Transport.send(Transport.java:81)
at SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java:79)
at SendMailUsingAuthentication.main(SendMailUsingAuthentication.java:37)
This is my program...
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* @author gaucho
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class SendMailUsingAuthentication {
private static final String SMTP_HOST_NAME = "smtp.gmail.com";
private static final String SMTP_AUTH_USER = "mygmail@gmail.com";
private static final String SMTP_AUTH_PWD = "password";
private static final String emailMsgTxt = "Please visit my project at ";
private static final String emailSubjectTxt = "Order Confirmation Subject";
private static final String emailFromAddress = "fromaddress@gmail.com";
// Add List of Email address to who email needs to be sent to
private static final String[] emailList = { "toaddress@gmail.com" };
public static void main(String args[]) throws Exception {
SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();
smtpMailSender.postMail(emailList, emailSubjectTxt, emailMsgTxt,
emailFromAddress);
System.out.println("Sucessfully Sent mail to All Users");
}
public void postMail(String recipients[], String subject, String message,
String from) throws MessagingException {
boolean debug = false;
java.security.Security
.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
//Set the host smtp address
Properties props = System.getProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", true);
//props.put("mail.smtp.port", "587");
props.put("mail.smtp.STARTTLS.enable",true);
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo = new InternetAddress(recipients);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
System.out.println("ENtered");
Transport.send(msg);
}
/**
* SimpleAuthenticator is used to do simple authentication when the SMTP
* server requires it.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
}
Please help me to get ride of the error.
Thanks and Regards,
Vijay

