1

I'm new in Java world and trying to create a small app to send email to my gmail account. But I am getting some error which I tried to solve, but failed. My full code is the following:

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class SendMyEmail {

    public static void main(String[] args) throws UnsupportedEncodingException {
        System.out.println("Hello");


        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBody = "...";

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("tinyNina@gmail.com", "Example.com Admin"));
            msg.addRecipient(Message.RecipientType.TO,
                             new InternetAddress("tinyNina@gmail.com", "Mr. User"));
            msg.setSubject("Your Example.com account has been activated");
            msg.setText(msgBody);
            Transport.send(msg);

            System.out.println("Done");

        } catch (AddressException e) {

            System.out.println(e);
            // ...
        } catch (MessagingException e) {

            System.out.println(e);
            // ...
        }

And I am getting the following error:

Hello
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection refused: connect


        }

    }

Can anybody tell me where I am doing some mistake?

Note: I tried lots of ways and then I found a google link https://cloud.google.com/appengine/docs/java/mail/ and then I tried and getting that error.

nina
  • 11
  • 1

0 Answers0