Hi, I am trying to execute a ready made code to send mail using Java Mail API 1.4 and got an error
Code:
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class SendMailSample {
public static void main(String[] argv) throws Exception {
String host = "smtp.gmail.com";
String from = "me@gmail.com";
String to = "you@gmail.com";
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.auth", "true");
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("Test Mail");
message.setText("Test Mail");
// Send message
Transport.send(message);
}
}
class MyAuthenticator extends Authenticator {
MyAuthenticator() { super(); }
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("me@gmail.com", "myPassword");
}
}
Error:
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;
nested exception is:
java.net.SocketException: Software caused connection abort: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
at javax.mail.Service.connect(Service.java:297)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at Pkg.Java.SendMailSample.main(SendMailSample.java:35)
Caused by: java.net.SocketException: Software caused connection abort: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
... 7 more
I know that this is one common error and tried some solutions which I got from googling
1. Disabled port 25 option in McAfee
2. Disabled Norton Antivirus
Still I am getting the same error. Can anyone give me a clear idea how to resolve this issue.
Should I need to disable port 25 option in Norton also??
Many thanks in advance
Message was edited by:
astelaveesta