I am trying to send a mail using java but i keep reciving the following erro
java.net.SocketException: Connection reset
javax.mail.MessagingException: Exception reading response;
here is my code
public void sendMail(String from, String to, String cc, String bcc, String subject, String body)
{
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Authenticator auth = new SMTPAuthenticator();
Session mailSession = Session.getDefaultInstance(props, auth);
mailSession.setDebug(true);
Transport transport;
try
{
transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject(subject);
message.setText(body);
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
if(!cc.equals(""))
{
message.addRecipient(Message.RecipientType.CC,
new InternetAddress(cc));
}
if(!bcc.equals(""))
{
message.addRecipient(Message.RecipientType.BCC,
new InternetAddress(bcc));
}
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
catch (NoSuchProviderException e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
}
catch (MessagingException e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
e.printStackTrace();
}
}
private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
private static final String SMTP_HOST_NAME = "smtp.gmail.com";
I research online with no success
This code is mainly chunck of code i found online
So if anybody can tell me why i keep having that exception and what to do to get rid of it
tx
for smt