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 Java Mail program

843834Dec 15 2007 — edited Dec 17 2007
Hi, 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??
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 14 2008
Added on Dec 15 2007
5 comments
243 views