1

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[]{"xyz@yahoo.com"},"hi","hello","abc@gmail.com");
    }
    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)
user327136
  • 19
  • 3
  • 5
  • To send email to Gmail, from Java: http://stackoverflow.com/questions/46663/how-do-you-send-email-from-a-java-app-using-gmail – leonbloy Apr 29 '10 at 17:58

3 Answers3

1

Actually, you also have to set the smtps properties when using smtps.

Simply set the mail properties to include:

mail.smtps.host
mail.smtps.auth
Flexo
  • 87,323
  • 22
  • 191
  • 272
John P.
  • 11
  • 1
0

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
  • 8,876
  • 8
  • 41
  • 75
  • He is, but he's also given the javamail property that should promote the connection to TLS after connecting. – Geoff Apr 29 '10 at 18:03
-1

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
  • 3,129
  • 23
  • 11
  • by changing the port I got this error com.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 – user327136 Apr 29 '10 at 18:06
  • I've ammended my answer, obviously GMail is not an open relay, you do need to perform authentication. – Geoff Apr 29 '10 at 18:08