java - com.sun.mail.util.MailConnectException : Couldn't connect to host, 端口 : localhost, 25;超时-1;

标签 java ssl jakarta-mail exchange-server starttls

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 = "admin@man.com";
    String password = "Man";

    // outgoing message information
    String mailTo = "ji@man.com";
    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();
    }
} }

错误是:

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)

最佳答案

这里有错误:

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

应该是:

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

关于java - com.sun.mail.util.MailConnectException : Couldn't connect to host, 端口 : localhost, 25;超时-1;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39613406/

相关文章:

java - 如何判断我的客户端应用程序允许哪些 SSL/TLS 协议(protocol)?

java - 在 Java Web 应用程序中发送电子邮件

java - 从 Java 无状态 EJB (Java EE-6) 发送电子邮件

java - 寻找不需要任何安装的良好且简单的数据库替代方案

java - 登录部署为 WAR 的 Web 应用程序

ssl - 无法设置远程应答 sdp : Failed to push down transport description: Failed to set SSL role for the channel

Spring 电子邮件 : Must issue a STARTTLS command first

java - 单击屏幕时更改 TextView 值

java - 带有特殊字符和空格的正则表达式模式问题

html - 站点在 HTTP 上加载,但不在 HTTPS 上加载