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

3
我将尝试做一个非常简单的电子邮件应用程序,已经写了几行基本代码。一个异常我一直在遇到是 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();
    }
}

密码字段在哪里? - kark
我正在努力中 ;) 我可以添加它,但在我真正获得连接之前,我认为这并不是必要的。此外,我不认为那是问题所在。或者,它是吗...? - DoTheGenes
对于查看代码来说,这已经足够了,但是要运行代码,你需要提供...你可以使用发布的答案..这比你绘制的要简单得多。 - kark
4个回答

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

3

要使代理在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...");

代码量更少,表达力更强。

这种方法是否也可以用于接收电子邮件,而不仅仅是发送? - Dreamspace President
1
@DreamspacePresident 不,接收邮件是完全不同的协议,不由Simple Java Mail处理。 - Benny Bottema

2
根据Oracle的JAVAMAIL API FAQ (http://www.oracle.com/technetwork/java/javamail/faq/index.htm):
JavaMail目前不支持通过Web代理服务器访问邮件服务器。
但是:
如果您的代理服务器支持SOCKS V4或V5协议,并允许匿名连接,并且您正在使用JDK 1.5或更新版本和JavaMail 1.4.5或更新版本,则可以通过为com.sun.mail.smtp包的javadocs中描述的设置“mail.smtp.socks.host”属性,在每个会话、每个协议的基础上配置SOCKS代理。
要使用SOCKS代理,必须为您的Session对象设置mail.smtp.socks.host和mail.smtp.socks.port参数,如此处所述:https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html

-3

尝试一下以下代码,易于操作...

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,"
                + "\n\n No spam to my email, please!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

包含 java-mail.jar,运行它....

这里复制


5
当然,你错过了问题的主要点,即如何通过代理服务器或防火墙连接。@RAlex说得对。还可以参考JavaMail FAQ调试提示 - Bill Shannon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接