Java 为我们的 SMTP 服务器配置 Spring 的 MailSender 时出现问题(但 GMail 有效)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/854493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problem configuring Spring's MailSender for our SMTP-server (but GMail works)
提问by
I have some problems sending mails through SMTP using Spring's MailSenderinterface and the concrete implementation JavaMailSenderImpl. I'm able to send mail through GMail, but not through our company SMTP-server (Postfix).
我在使用 Spring 的MailSender接口和具体实现JavaMailSenderImpl通过 SMTP 发送邮件时遇到了一些问题。我可以通过 GMail 发送邮件,但不能通过我们公司的 SMTP 服务器(Postfix)。
Correct configurations
正确的配置
To see that I have the correct configuration I used the excellent mail sender ssmtp. It's a simple utility (which is able to emulate Sendmail) and is used solely for sending mail through SMTP.
为了查看我的配置是否正确,我使用了出色的邮件发件人ssmtp。它是一个简单的实用程序(能够模拟 Sendmail)并且仅用于通过 SMTP 发送邮件。
Below are the two commands I used to send mail. The first one is for GMail and the second one for our company SMTP-server. Both mails arrived as they should and thus the configuration files that follow are correct.
下面是我用来发送邮件的两个命令。第一个用于 GMail,第二个用于我们公司的 SMTP 服务器。两封邮件都按预期到达,因此后面的配置文件是正确的。
$ ssmtp -C gmail-smtp.conf [email protected] < gmail-message.txt
$ ssmtp -C other-smtp.conf [email protected] < other-message.txt
The contents of the ssmtp configuration files and the message files are listed below. How the configuration file is structured can be seen at: http://linux.die.net/man/5/ssmtp.conf:
下面列出了 ssmtp 配置文件和消息文件的内容。配置文件的结构可以在:http: //linux.die.net/man/5/ssmtp.conf看到:
gmail-message.txt:
gmail-message.txt:
To: [email protected]
From: [email protected]
Subject: Sent using the SMTP-server of GMail
Some content.
gmail-smtp.conf:
gmail-smtp.conf:
mailhub=smtp.gmail.com:587
UseSTARTTLS=yes
[email protected]
AuthPass=john_password
other-message.txt:
其他消息.txt:
To: [email protected]
From: [email protected]
Subject: Sent using the SMTP-server of TheCompany
Some content.
other-smtp.conf:
其他 smtp.conf:
# No username or password = no authentication
hostname=thecompany.net
mailhub=mail.thecompany.net:25
MailSender configuration which works against GMail
适用于 GMail 的 MailSender 配置
I'm sucessful in sending mail through GMail using this Spring MailSender configuration:
我成功地使用这个 Spring MailSender 配置通过 GMail 发送邮件:
...
<bean id="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="[email protected]" />
<property name="password" value="john_password" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
...
The problem (sending through the company SMTP-server)
问题(通过公司 SMTP 服务器发送)
With this MailSender configuration:
使用此 MailSender 配置:
...
<bean id="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
<property name="host" value="mail.thecompany.net" />
<property name="port" value="25" />
</bean>
...
I get this exception:
我得到这个例外:
org.springframework.mail.MailSendException; nested exceptions (1) are:
Failed message 1: javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 504 5.5.2 <rat>: Helo command rejected: need fully-qualified hostname
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:422)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:308)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:297)
... (The rest are methods I've created, which are irrelevant)
I alsoget 504 5.5.2 : Helo command rejected: need fully-qualified hostnameif I remove hostname=thecompany.netfrom other-smtp.confusing ssmtp. I guess I have to provide the hostname somehow. My computers name is ratbut it seems like it wants thecompany.net.
我也得到504 5.5.2 : Helo command denied : need full-qualified hostnameif I remove hostname= thecompany.netfrom other-smtp.confusing ssmtp。我想我必须以某种方式提供主机名。我的电脑名字是老鼠,但它似乎想要thecompany.net。
Any and all help appreciated!
任何和所有的帮助表示赞赏!
采纳答案by Karimchik
Try to add the property "mail.smtp.localhost" with correct value (thecompany.net).
尝试使用正确的值(thecompany.net)添加属性“mail.smtp.localhost” 。
回答by MahdeTo
I think its an address resolution problem in your case. make sure the mail server is accessible using the given name from the machine you've deployed this code on. if not just add an entry to your the hosts file.
我认为在您的情况下是地址解析问题。确保可以使用您部署此代码的机器上的给定名称访问邮件服务器。如果不只是在您的主机文件中添加一个条目。
回答by matt b
Can you pass in the extra properties (hostname) you need to set with your SMTP server in the javaMailProperties
property?
您能否在属性中传入您需要使用 SMTP 服务器设置的额外属性(主机名)javaMailProperties
?
<bean id="mailSender" class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
<property name="host" value="mail.thecompany.net" />
<property name="port" value="25" />
<!-- addition: -->
<property name="javaMailProperties">
<props>
<prop key="hostname">thecompany.net</prop>
</props>
</property>
</bean>
Not quite sure what the correct key name is for hostname
in the JavaMail API.
不太确定hostname
JavaMail API 中正确的密钥名称是什么。
回答by sinarf
回答by desmondev
SMTPTransport mailFrom() sets the sender's address in the following order:
SMTPTransport mailFrom() 按以下顺序设置发件人的地址:
SMTPMessage.getEnvelopeFrom()
mail.smtp.from property
From: header in the message
System username using the InternetAddress.getLocalAddress() method
You just need to add property from
in javaMailProperies
to your email address
您只需要from
在javaMailProperies
您的电子邮件地址中添加属性