tags:

views:

152

answers:

2

Hi I am writing this application in eclipse I added all the jar files.I am pasting the code and error.Please let me know what changes I should make to run the application properly.

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendMail
{
  public static void main(String [] args)
  {
    SendMail sm=new SendMail();
    try{
     sm.postMail(new String[]{"[email protected]"},"hi","hello","[email protected]");
    }
    catch(MessagingException e)
    {
        e.printStackTrace();
    }
    }


public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
{
    boolean debug = false;

     //Set the host smtp address
     Properties props = new Properties();
     props.put("mail.smtp.starttls.enable","true");
     props.put("mail.smtp.host", "smtp.gmail.com");
        props.setProperty("mail.smtp.port", "25");

    // create some properties and get the default Session
    Session session = Session.getDefaultInstance(props, null);
    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[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);


    // Optional : You can also set your custom headers in the Email if you Want
    msg.addHeader("MyHeaderName", "myHeaderValue");

    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    Transport.send(msg);
}
}

Error:

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. 13sm646598ewy.13

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1368)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:886)
    at javax.mail.Transport.send0(Transport.java:191)
    at javax.mail.Transport.send(Transport.java:120)
    at SendMail.postMail(SendMail.java:54)
    at SendMail.main(SendMail.java:10)
A: 

You're trying to send mail over a connection that expects an encrypted connection (TLS in this case).

This sun forum has several solutions that you might try: http://forums.sun.com/thread.jspa?threadID=617974

Kylar
He is, but he's also given the javamail property that should promote the connection to TLS after connecting.
Geoff
A: 

For javamail to GMail SMTP I use the following properties:

mail.smtp.host="smtp.gmail.com"
mail.smtp.port="587"
mail.smtp.auth="true"
mail.smtp.starttls.enable="true"

I wasn't aware that GMail accepted mail on port 25. Perhaps that is the source of your problem.

Obviously you still need to perform authentication, usually via mail.smtp.user and mail.smtp.password .

Geoff
by changing the port I got this errorcom.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://mail.google.com/support/bin/answer.py?answer=14257 i35sm2744098gve.26
I've ammended my answer, obviously GMail is not an open relay, you do need to perform authentication.
Geoff