views:

8137

answers:

4

I am trying to use Bill the Lizard's code to send an email using Google Apps. I am getting this error:

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. f3sm9277120nfh.74

    at javax.mail.Transport.send0(Transport.java:219)
    at javax.mail.Transport.send(Transport.java:81)
    at SendMailUsingAuthentication.postMail(SendMailUsingAuthentication.java:81)
    at SendMailUsingAuthentication.main(SendMailUsingAuthentication.java:44)

Bill's code contains the next line, which seems related to the error:

   props.put("mail.smtp.starttls.enable","true");

However, it does not help.

These are my import statements:

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;

Does anyone know about this error?

A: 

I think it's got to do with using SMTPS instead of SMTP for mail transport. Here's a different version, modeled after the JavaMail FAQ on accessing Gmail. Note that I have left out all of the finer level exception handling for clarity.

private static void send(
  final String username,
  final String password,
  final String recipients,
  final String subject,
  final String body)
  throws Exception
{
 final Session session = Session.getInstance(System.getProperties(), null);
 final Message msg = new MimeMessage(session);
 final String senderEmail = username.contains("@") ? username : (username + "@gmail.com");
 msg.setFrom(new InternetAddress(senderEmail));

 final Address[] recipientAddresses = InternetAddress.parse(recipients);
 msg.setRecipients(Message.RecipientType.TO, recipientAddresses);

 msg.setSentDate(new Date());
 msg.setSubject(subject);
 msg.setText(body);

 final Transport transport = session.getTransport("smtps");
 transport.connect(GMAIL_SMTP_HOST, GMAIL_SMTP_PORT, username, password);
 transport.sendMessage(msg, recipientAddresses);
 transport.close();
}
Zach Scrivena
I get the next exception while running your code: Exception in thread "main" javax.mail.NoSuchProviderException: No provider for smtps
Sergio del Amo
Seems like the JavaMail resource files are missing or corrupted (see http://java.sun.com/products/javamail/javadocs/javax/mail/Session.html). There are default copies of the files inside mail.jar/META-INF.
Zach Scrivena
Thanks, i worked it out.
Sergio del Amo
+1  A: 

I found the problem. Previously i was using j2ee.jar to import javax.mail.

I removed j2ee.jar from the classpath and downloaded JavaMail 1.4.1 and put into my classpath two jars, smtp.jar and mailapi.jar. I use now smtps instead smtp

Transport transport = session.getTransport("smtps");

Now Bill the Lizard's code works.

Sergio del Amo
A: 

It is the version of java mail API. I was facing this issue and I just updated the java mail API to 1.4.3 It works fine for me!

Thanks!

Cleber
A: 

im trying to send mail through java APi but is showes en excaption plzzz guide mee...

msgjavax.mail.internet.MimeMessage@f72617 javax.mail.SendFailedException: Sending failed; nested exception is: class javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS com mand first. n29sm18571449wae.4

sandeep