Simple Java Mail program
843834Dec 15 2007 — edited Dec 17 2007Hi, i have written a simple code to send mail thru java... but it doesnt work... control never comes back after stepping in to the send function.. no exceptions.. no mails sent.. nothin.. cud someone hel me out??
sometimes i get this exception after a long time:
javax.mail.MessagingException: Exception reading response;
nested exception is:
java.net.SocketException: Connection reset
at com.sun.mail.smtp.SMTPTransport.readServerResponse(Unknown Source)
at com.sun.mail.smtp.SMTPTransport.openServer(Unknown Source)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(Unknown Source)
at javax.mail.Service.connect(Unknown Source)
-------
here is the code snippet:
public void mySend() throws Throwable
{
Properties p = System.getProperties();
p.put("mail.transport.protocol", "smtp");
p.put("mail.smtp.host", "smtp.gmail.com");
p.put("mail.smtp.port", "465");
p.put("mail.smtp.starttls.enable", "true");
p.put("mail.smtp.auth", "true");
Authenticator authenticator = new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("vicky.bhandari", "password");
}
};
Session session = Session.getInstance(p, authenticator);
SMTPMessage message = new SMTPMessage(session);
InternetAddress fromAddress = new InternetAddress("vicky.bhandari@gmail.com");
InternetAddress toAddress = new InternetAddress("vicky.bhandari@gmail.com");
message.setSubject("Test");
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setFrom(fromAddress);
SMTPTransport.send(message);
}
Could someone please help me out??