com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;

ips picture ips · Sep 21, 2016 · Viewed 30.1k times · Source
package jmail;

import java.util.Date; import java.util.Properties; 
import javax.mail.Authenticator; import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
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 HtmlJavaSend {

public void sendHtmlEmail(String host, String port,
        final String userName, final String password, String toAddress,
        String subject, String message) throws AddressException,
        MessagingException {

    // sets SMTP server properties
    Properties properties = new Properties();
    properties.put("mail.man.com", host);
    properties.put("mail.25", port);
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.ssl.trust","mail.man.com");

    // creates a new session with an authenticator
    Authenticator auth = new Authenticator() {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password);
        }
    };

    Session session = Session.getInstance(properties, auth);

    // creates a new e-mail message
    Message msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress(userName));
    InternetAddress[] toAddresses = {new InternetAddress(toAddress)};
    msg.setRecipients(Message.RecipientType.TO, toAddresses);
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    // set plain text message
    msg.setContent(message, "text/html");

    // sends the e-mail
    Transport.send(msg);

}

public static void main(String[] args) {
    // SMTP server information
    String host = "mail.man.com";
    String port = "25";
    String mailFrom = "[email protected]";
    String password = "Man";

    // outgoing message information
    String mailTo = "[email protected]";
    String subject = "Hello my friend";

    // message contains HTML markups
    String message = "<i>Greetings!</i><br>";
    message += "<b>Wish you a nice day!</b><br>";
    message += "<font color=red>Duke</font>";

    HtmlJavaSend mailer = new HtmlJavaSend();

    try {
        mailer.sendHtmlEmail(host, port, mailFrom, password, mailTo,
                subject, message);
        System.out.println("Email sent.");
    } catch (Exception ex) {
        System.out.println("Failed to sent email.");
        ex.printStackTrace();
    }
} }

The error is:

eror run: Failed to sent email. com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1; nested exception is: java.net.ConnectException: Connection refused: connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699) at javax.mail.Service.connect(Service.java:388) at javax.mail.Service.connect(Service.java:246) at javax.mail.Service.connect(Service.java:195) at javax.mail.Transport.send0(Transport.java:254) at javax.mail.Transport.send(Transport.java:124) at jmail.HtmlJavaSend.sendHtmlEmail(HtmlJavaSend.java:62) at jmail.HtmlJavaSend.main(HtmlJavaSend.java:85) Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:579) at java.net.Socket.connect(Socket.java:528) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:331) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2066) ... 8 more BUILD SUCCESSFUL (total time: 1 second)

Answer

porgo picture porgo · Sep 21, 2016

You have error here:

properties.put("mail.man.com", host);
properties.put("25", port);

It should be:

properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);