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



我正在尝试制作一个非常简单的电子邮件应用程序,并且我已经编写了几行基本代码。我不断得到的一个例外是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();
    }
}

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

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

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

mail.smtp.proxy.host
mail.smtp.proxy.port
mail.smtp.proxy.user
mail.smtp.proxy.password

您需要以正确的组合正确设置一堆属性,以便代理在 JavaMail 中工作。JavaMail只支持匿名SOCKS代理。

但是,Simple Java Mail会为您处理这些属性,并在此基础上添加经过身份验证的代理支持。它是开源的,并且仍在积极开发。

以下是使用Simple Java Mail的代码的外观:

Mailer mailer = new Mailer(// note: from 5.0.0 on use MailerBuilder instead
        new ServerConfig("localhost", thePort, theUser, thePasswordd),
        TransportStrategy.SMTP_PLAIN,
        new ProxyConfig(proxyHost, proxyPort /*, proxyUser, proxyPassword */)
);
mailer.sendMail(new EmailBuilder()
        .from("mytest", "mytest@test.com")
        .to("test", "test@test.com")
        .subject("This is the subject line")
        .textHTML("<h1>This is the actual message</h1>")
        .build());
System.out.println("Message sent...");

代码少了很多,表现力很强。

来自Oracle的JAVAMAIL API FAQ (http://www.oracle.com/technetwork/java/javamail/faq/index.htm):

JavaMail 目前不支持通过 网络代理服务器。

但:

如果您的代理服务器支持 SOCKS V4 或 V5 协议,并且允许 匿名连接,并且您使用的是 JDK 1.5 或更高版本和 JavaMail 1.4.5 或更高版本,您可以通过将"mail.smtp.socks.host"属性设置为 在 com.sun.mail.smtp 包的 javadocs 中描述。

要使用 SOCKS 代理,您必须为 Session 对象设置mail.smtp.socks.hostmail.smtp.socks.port参数 - 如下所述:https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html

只需尝试以下代码,易于工作...

public class SendMail{
    public static void main(String[] args) {
        final String username = "from@gmail.com";
        final String password = "password";
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
          });
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("to@gmail.com"));
            message.setSubject("Testing Subject");
            message.setText("Dear Mail Crawler,"
                + "nn No spam to my email, please!");
            Transport.send(message);
            System.out.println("Done");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

包括java-mail.jar,运行它。

从这里复制

最新更新