Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Simple SendMail program

843830Mar 23 2006 — edited Dec 15 2007
I have the following simple code to send mail through my GMail ac using JavaMail:

import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;

import javax.mail.*;
import javax.mail.internet.*;

public class SendMail {

public static void main(String[] argv) throws Exception{

String host = "smtp.gmail.com";
String from = "vedi@gmail.com";
String to = "jitendra@gmail.com";

// Get system properties
Properties props = System.getProperties();

// Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.smtp.starttls.enable", "true");

// Get session
Authenticator auth = new MyAuthenticator();
Session session = Session.getDefaultInstance(props, auth);

// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("Hello JavaMail");
message.setText("Welcome to JavaMail");

// Send message
com.sun.mail.smtp.SMTPSSLTransport.send(message);
}
}

class MyAuthenticator extends Authenticator
{
MyAuthenticator() { super(); }

protected PasswordAuthentication getPasswordAuthentication()
{ return new PasswordAuthentication("vedijitendra", "pw"); }
}

I am getting SMTPSendFailedException: Authentication Required.

I have specified the correct server settings, usr id & pw.

Pls help. Thx for ur time.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 12 2008
Added on Mar 23 2006
12 comments
339 views