How can I send email using core java code via outlook account running in unicode mode ? I am using javax.mail jar files

292 views Asked by At

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("[email protected]"));
        message.setSubject("Can you see this mail ?");
        message.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
        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?

0

There are 0 answers