Simple SendMail program
843830Mar 23 2006 — edited Dec 15 2007I 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.