我们正在使用 Spring Mail 从 java 应用程序发送电子邮件
org.springframework.mail.javamail.JavaMailSenderImpl
Spring电子邮件配置是
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl" autowire-candidate="default">
<property name="host" value="${test.email.host}" />
<property name="port" value="${test.email.port}" />
<property name="username" value="${test.email.username}" />
<property name="password" value="${test.email.password}" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>
Java代码快照是
@Autowired(required = true)
@Qualifier("errorMessageMailSender")
JavaMailSenderImpl mailSender;
..............
..............
try {
MimeMessage mailMessage = buildEmailMimeMessage(properties,mimeMultipart);
logger.info(String.format("Built MimeMessage object is <%s>",mailMessage));
if (mailMessage != null) {
mailSender.send(mailMessage);
logger.info("Mail sent Successfully");
}else{
logger.info("Mail send failed as Mail message object construction failed.");
}
result=true;
} catch (Exception e) {
logger.error("An exception occurred while sending mail :: " + e.getMessage());
}
财产文件
test.email.host=mail.mydomain.net
test.email.port=2525
test.[email protected] /cdn-cgi/l/email-protection
test.email.password=mypassword
但我们遇到异常,并且未发送电子邮件
An exception occurred while sending mail :: Failed messages: javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 <[email protected] /cdn-cgi/l/email-protection>... Relaying denied
您尝试向其发送邮件的 smtp 服务器拒绝中继。如果您自己的本地服务器没有问题,您可以更改配置以中继来自 mydomain.net 的邮件。但如果是外部服务器(例如gmail),则必须使用真实的注册域名。
要针对邮件服务器测试您的代码,我建议您设置一个 dockerized Apache James 服务器,在其上创建一些测试用户并发送和接收电子邮件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)