Whats wrong with this code.Runtime error

Posted by javacode on Stack Overflow See other posts from Stack Overflow or by javacode
Published on 2010-04-29T17:55:11Z Indexed on 2010/04/29 17:57 UTC
Read the original article Hit count: 326

Filed under:
|

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)

© Stack Overflow or respective owner

Related posts about java

Related posts about javamail