如何配置环境以使用JavaMail? [英] How to configure environment to use JavaMail?

查看:230
本文介绍了如何配置环境以使用JavaMail?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用JavaMail发送简单的html消息。当我试图在互联网上找到一些有解释的好例子时,每个下一个例子都让我更生气和愤怒。

I need to send simple html-message with JavaMail. And when I tried to find some nice examples with explanations in the Internet, each next example made me more angry and angry.

所有这些愚蠢的例子都包含复制和粘贴的Java代码,这些代码只有注释和一个不错的免责声明,首先应该配置你的smtp和pop3服务器。

All those silly examples contain copied and pasted Java code which differs only in comments and a nice disclaimer that first you should config your smtp and pop3 server.

我知道没有人想为某些具体产品做广告,但配置服务器是最困难的部分。那么,任何人都可以给我一些关于配置具体服务器(例如,或其他任何一个)的非常有用的信息(没有java代码)吗?

I understand that nobody wants to make an advertise for some concrete products but configuring the server is imho the hardest part. So, can anyone give me some really useful information (without java code) about configuring concrete server (Kerio, for example, or any other one)?

我现在拥有什么是下一个例外:

What I have now is the next exception:

250 2.0.0 Reset state
javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Relaying to <mymail@mycompany.com> denied (authentication required)

UPD。所有以前的文本的简单重新构造是:想象你有Windows,jdk,没有别的。你想制作java程序并在你的机器上运行它。而这个程序应该发送Hello world!到你的Gmail帐户。列出你的步骤。

UPD. Simple reformulation of all previous text is: imagine that you have Windows, jdk, and nothing else. And you want to make java program and run it on your machine. And this program should send "Hello world!" to your gmail account. List your steps.

UPD2。以下是代码:

UPD2. Here is the code:

Properties props = new Properties ();
props.setProperty ("mail.transport.protocol", "smtp");
props.setProperty ("mail.host", "smtp.gmail.com");
props.setProperty ("mail.user", "my_real_address_1@gmail.com");
props.setProperty ("mail.password", "password_from_email_above"); 

Session mailSession = Session.getDefaultInstance (props, null);
mailSession.setDebug (true);
Transport transport = mailSession.getTransport ();

MimeMessage message = new MimeMessage (mailSession);
message.setSubject ("HTML  mail with images");
message.setFrom (new InternetAddress ("my_real_address_1@gmail.com"));
message.setContent ("<h1>Hello world</h1>", "text/html");
message.addRecipient (Message.RecipientType.TO,
        new InternetAddress ("my_real_address_2@gmail.com"));

transport.connect ();
transport.sendMessage (message,
        message.getRecipients (Message.RecipientType.TO));

例外情况是:

RSET
250 2.1.5 Flushed 3sm23455365fge.10
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. 3sm23455365fge.10
    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829)
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1368)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:886)
    at com.teamdev.imgmail.MailSender.main(MailSender.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    ...


推荐答案

如果您正在寻找配置SMTP服务器的教程,那么您不应该寻找JavaMail。只需在您选择的服务器上查找教程( Kerio ,例如......或 Exim SendMail Apache James Postfix )或询问 Serverfault 。任何符合SMTP的服务器都可以很好地与JavaMail配合使用。

If you're looking for a tutorial to configure an SMTP server, you shouldn't be looking for JavaMail. Simply look for a tutorial on your server of choice (Kerio, for example ... or Exim, SendMail, Apache James, Postfix) or ask on Serverfault. Any SMTP-compliant server will play nicely with JavaMail.

或者,您甚至可以使用任何标准邮件提供商的基础设施。例如,我使用 Google Apps 帐户以及Google的SMTP基础结构从我们的Java应用程序发送邮件。如果您不想要,使用Gmail帐户无论如何都是一个很好的起点设置你自己的SMTP服务器,以简单地测试驱动JavaMail。

Alternatively, you may even use any "standard" mail provider's infrastructure. For example, I use a Google Apps account along with Google's SMTP infrastructure to send mail from our Java applications. Using a Gmail account is a good starting point anyway if you don't want to setup your own SMTP server in order to simply testdrive JavaMail.

作为最后一个选项,你甚至可以查找 MX记录,并将邮件直接发送到收件人的SMTP服务器。有一些常见的问题需要解决。

As a last option, you might even lookup the MX Records for a domain and deliver your mails directly to the SMTP server of the recipient. There are some common gotchas to workaround tough.

作为最后一点,你必须研究如何避免将你的邮件作为垃圾邮件过滤 - 这是一个巨大的话题本身。这有助于依赖标准提供商来处理托管您自己的服务器时可能遇到的一些问题。

As a last point, you'll have to look into how to avoid that your mails be filtered as spam - which is a huge topic itself. Here it helps to rely on standard providers that will deal with some of the issues you might encounter when hosting your own server.

Btw:关于您发布的错误消息: SMTP服务器拒绝中继邮件。如果您的SMTP服务器(认为它)在example.com上运行,并且您要将其作为bob@example.net发送到alice@example.org,那么您要求SMTP服务器充当中继。这是几年前的常见做法,直到它 - 你猜对了 - 被垃圾邮件发送者滥用。从那时起,鼓励邮政局长否认继电器。您有两种选择:在发送邮件之前进行身份验证或仅发送到您服务器上托管的帐户(例如,在example.com上,例如alice@example.com)。

Btw: Regarding the error message you posted: the SMTP server is denying relaying of messages. This is if your SMTP server (thinks that it) is running on example.com and you're sending as bob@example.net to alice@example.org, you're asking the SMTP server to act as a relay. This was common practice several years ago, until it was - you guessed it - abused by spammers. Since those days, postmasters are encouraged to deny relaying. You have two choices: authenticate before sending mail or send to accounts hosted at your server only (i.e. on example.com, e.g. alice@example.com).

编辑:

以下是一些代码,可帮助您开始使用身份验证功能(适用于Gmail帐户,但应该执行此操作)对于你自己的服务器也是如此)

Here is some code to get you started with authenticationg (works with Gmail accounts but should do for your own server as well)

private Session createSmtpSession() {
  final Properties props = new Properties();
  props.setProperty("mail.smtp.host", "smtp.gmail.com");
  props.setProperty("mail.smtp.auth", "true");
  props.setProperty("mail.smtp.port", "" + 587);
  props.setProperty("mail.smtp.starttls.enable", "true");
  // props.setProperty("mail.debug", "true");

  return Session.getDefaultInstance(props, new javax.mail.Authenticator() {

    protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication("john.doe@gmail.com", "mypassword");
    }
  });
}

这篇关于如何配置环境以使用JavaMail?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆