如何配置环境以使用JavaMail?

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

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

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

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

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  denied (authentication required) 

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

UPD2。 这是代码:

 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 ("

Hello world

", "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配合使用。

或者,您甚至可以使用任何“标准”邮件提供商的基础架构。 例如,我使用Google Apps帐户和Google的SMTP基础结构从我们的Java应用程序发送邮件。 如果您不想设置自己的SMTP服务器以便简单地试用JavaMail,那么使用Gmail帐户是一个很好的起点。

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

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

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

编辑:

以下是一些代码,可以帮助您开始使用authenticationg(适用于Gmail帐户,但也可以用于您自己的服务器)

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

结合上述答案的工作示例,使用activation-1.1.jarmail-1.4.1.jar以及SMTP主机是Gmail

  1. user@gmail.comuser_pw替换为return new PasswordAuthentication("user@gmail.com", "user_pw");

  2. 此外,您希望将myRecipientAddress@gmail.com替换为您要接收电子邮件的电子邮件地址。

     package com.test.sendEmail; import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class sendEmailTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub sendEmailTest emailer = new sendEmailTest(); //the domains of these email addresses should be valid, //or the example will fail: emailer.sendEmail(); } /** * Send a single email. */ public void sendEmail(){ Session mailSession = createSmtpSession(); mailSession.setDebug (true); try { Transport transport = mailSession.getTransport (); MimeMessage message = new MimeMessage (mailSession); message.setSubject ("HTML mail with images"); message.setFrom (new InternetAddress ("myJavaEmailSender@gmail.com")); message.setContent ("

    Hello world

    ", "text/html"); message.addRecipient (Message.RecipientType.TO, new InternetAddress ("myRecipientAddress@gmail.com")); transport.connect (); transport.sendMessage (message, message.getRecipients (Message.RecipientType.TO)); } catch (MessagingException e) { System.err.println("Cannot Send email"); e.printStackTrace(); } } private Session createSmtpSession() { final Properties props = new Properties(); props.setProperty ("mail.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.transport.protocol", "smtp"); // props.setProperty("mail.debug", "true"); return Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("user@gmail.com", "user_pw"); } }); } }

我可以看到你的部分问题。 它在错误消息中得到了充分的解释。

您发送邮件的SMTP服务器(即您在JavaMail配置中配置的地址之一)拒绝将邮件转发到mymail@company.com 。 看起来像是SMTP服务器中的配置问题。 正如sfussenegger所说,它与javamail无关。

因此,您不是同时在所有方面进行调试,尝试从已知正常工作的SMTP客户端寻址SMTP服务器可能是个好主意。 例如,雷鸟会做得很好。 如果你可以从Thunderbird发送邮件,那么JavaMail应该没什么问题。


更新:

Google SMTP服务器的正确地址是: smtp.gmail.com 。 这是您在JavaMail中配置的服务器吗? 你能告诉我们匹配的错误信息吗?

这应该工作:

 import java.text.MessageFormat; import java.util.List; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class Emailer { public static void main(String[] args) { String hostname = args[0]; final String userName = args[1]; final String passWord = args[2]; String toEmail = args[3]; String fromEmail = args[4]; String subject = args[5]; String body = ""; // add rest of args as one body text for convenience for (int i = 6; i < args.length; i++) { body += args[i] + " "; } Properties props = System.getProperties(); props.put("mail.smtp.host", hostname); Session session = Session.getInstance(props, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, passWord); } }); MimeMessage message = new MimeMessage(session); try { message.setFrom(new InternetAddress(fromEmail)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail)); message.setSubject(subject); message.setText(body); Transport.send(message); } catch (MessagingException e) { System.out.println("Cannot send email " + e); } } } 

您需要将JavaMail mail.jar放在类路径中以获取javax.mail依赖项。 我不确定Google是否允许您按照自己的意愿发送电子邮件。 如何尝试其他电子邮件提供商,如您的ISP?