JavaMail - 设置端口、代理和防火墙

DoT*_*nes 3 java proxy firewall jakarta-mail connectexception

我正在尝试制作一个非常简单的电子邮件应用程序,并且我已经编写了几行基本代码。我不断收到的一个例外是com.sun.mail.util.MailConnectException. 有没有一种简单的方法可以通过代理或防火墙进行编码,而不会扰乱发送计算机的连接设置?

到目前为止我的代码:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class SendHTMLMail {
public static void main(String[] args) {
    // Recipient ID needs to be set
    String to = "test@test.com";

    // Senders ID needs to be set
    String from = "mytest@test.com";

    // Assuming localhost
    String host = "localhost";

    // System properties
    Properties properties = System.getProperties();

    // Setup mail server
    properties.setProperty("mail.smtp.host", host);

       //Get default session object
    Session session = Session.getDefaultInstance(properties);

    try {
        // Default MimeMessage object
        MimeMessage mMessage = new MimeMessage(session);

        // Set from
        mMessage.setFrom(new InternetAddress(from));

        // Set to
        mMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

        // Set subject
        mMessage.setSubject("This is the subject line");

        // Set the actual message
        mMessage.setContent("<h1>This is the actual message</h1>", "text/html");

        // SEND MESSAGE
        Transport.send(mMessage);
        System.out.println("Message sent...");
    }catch (MessagingException mex) {
        mex.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

从 JavaMail 1.6.2 开始,您可以为 Session 对象设置代理身份验证属性以发送电子邮件。

请参阅以下文档链接。https://javaee.github.io/javamail/docs/api/

以下属性是新引入的,并且可以与代理身份验证(基本)配合使用。

mail.smtp.proxy.host

mail.smtp.proxy.port

mail.smtp.proxy.user

mail.smtp.proxy.password
Run Code Online (Sandbox Code Playgroud)