0

I want my program to send emails from my corporate outlook account. This is the code I am using. Although I am using the correct SMTP host name and port number it still is throwing an exception.

package Prac;

import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import java.util.Properties;

public class CodeForMail {

    private static final String SMTP_HOST_NAME = "host name here";
    private static final String SMTP_AUTH_USER = "userid";
    private static final String SMTP_AUTH_PWD = "password";

    public static void main(String[] args) throws Exception {
        new CodeForMail().test();
    }

    public void test() throws Exception {
        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.port", "port no");
        props.put("mail.smtp.auth", "true");

        Authenticator auth = new SMTPAuthenticator();
        Session mailSession = Session.getInstance(props, auth);
        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);
        Multipart multipart = new MimeMultipart("alternative");

        BodyPart part1 = new MimeBodyPart();
        part1.setText("Para 1 ");
        BodyPart part2 = new MimeBodyPart();
        part2.setContent("Para 2", "text/html");

        multipart.addBodyPart(part1);
        multipart.addBodyPart(part2);

        message.setContent(multipart);
        message.setFrom(new InternetAddress("xxx@yyy.com"));
        message.setSubject("Can you see this mail ?");
        message.addRecipient(Message.RecipientType.TO, new InternetAddress("aaa@yyy.com"));
        transport.connect();
        Transport.send(message, message.getRecipients(Message.RecipientType.TO));
        transport.close();
        System.out.println("MSG SEND");
    }

    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);
        }
    }
}

And this is the exception which I am getting:

Exception in thread "main" javax.mail.MessagingException: Exception reading response;
  nested exception is:
    java.net.SocketException: Connection reset
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2227)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1962)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:656)
    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 Prac.CodeForMail.test(CodeForMail.java:53)
    at Prac.CodeForMail.main(CodeForMail.java:16)
Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:124)
    at java.io.BufferedInputStream.fill(Unknown Source)
    at java.io.BufferedInputStream.read(Unknown Source)
    at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:89)
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2207)
    ... 7 more

I am using a cooperate mail account for sending mail so what else do I need to check or add among the properties?

Meyer
  • 1,662
  • 7
  • 21
  • 20
  • 1
    Have you checked here: http://stackoverflow.com/questions/62929/java-net-socketexception-connection-reset – Azodious Dec 20 '16 at 10:16
  • I believe it can due to anti-virus or firewall that's preventing my application from connecting to the mail server so what are the properties which I need configure according to the network requirements. – Rohit Bhardwaj Dec 20 '16 at 11:57
  • What does the [JavaMail debug output](http://www.oracle.com/technetwork/java/javamail/faq/index.html#debug) show? If it's a firewall that's blocking the connection, you'll need to reconfigure the firewall to allow it, or configure the application to use a proxy to get through the firewall, as described in the [JavaMail FAQ](http://www.oracle.com/technetwork/java/javamail/faq/index.html#proxy). – Bill Shannon Dec 20 '16 at 20:34
  • Debug output shows:- DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jdk1.8.0_121\jre\lib\javamail.address.map (The system cannot find the file specified) DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] DEBUG SMTP: need username and password for authentication DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "ctsinchnvcasa.cts.com", port 443, isSSL true DEBUG SMTP: EOF: [EOF] DEBUG SMTP: could not connect to host "ctsinchnvcasa.cts.com", port: 443, response: -1 – Rohit Bhardwaj Jan 30 '17 at 06:08
  • Continuation of the above comment : What could be the problem ? As it's throwing java.io.FileNotFoundException in debug output. – Rohit Bhardwaj Jan 30 '17 at 06:13

0 Answers0