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();
    }
}
4

4 回答 4

4

从 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
于 2019-03-21T06:04:35.457 回答
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...");

更少的代码和非常有表现力

于 2016-07-07T19:32:57.600 回答
2

From the Oracle's JAVAMAIL API FAQ (http://www.oracle.com/technetwork/java/javamail/faq/index.htm):

JavaMail does not currently support accessing mail servers through a web proxy server.

But:

If your proxy server supports the SOCKS V4 or V5 protocol, and allows anonymous connections, and you're using JDK 1.5 or newer and JavaMail 1.4.5 or newer, you can configure a SOCKS proxy on a per-session, per-protocol basis by setting the "mail.smtp.socks.host" property as described in the javadocs for the com.sun.mail.smtp package.

In order to use a SOCKS proxy, you have to set the mail.smtp.socks.host and mail.smtp.socks.port parameters for your Session object - as described here: https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html

于 2013-09-30T11:47:49.557 回答
-3

Just try the following code, Easy to work...

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);
        }
    }
}

Include java-mail.jar, Run it....

Copied from here

于 2013-09-30T11:48:11.927 回答