gpt4 book ai didi

java - Spring JavaMailSender : recipient set as "default"

转载 作者:太空宇宙 更新时间:2023-11-04 12:36:34 30 4
gpt4 key购买 nike

这是我的第一个问题,所以我想保持简单。我对 Spring Framework 版本 4.1.8.RELEASE 中的 JavaMailSender 有问题。对于特定的 SMTP,它似乎不会将收件人添加到邮件中。我想提一下,我们在其他项目中使用了相同的解决方案,并且它始终有效。从现在开始,收件人没有出现任何问题。

以下是我们如何设置收件人的代码示例(setupRecipients 方法):

String emailTo = entity.getEmailTo();
Preconditions.checkNotNull( !Strings.isNullOrEmpty( emailTo ) );
emailTo = emailTo.replace( ",", " " );
emailTo = emailTo.replace( ";", " " );
InternetAddress[] emails = InternetAddress.parse( emailTo );

for ( InternetAddress address : emails )
address.validate();

for ( InternetAddress address : emails )
messageHelper.setTo( address.getAddress() );

if ( !Strings.isNullOrEmpty( bcc ) )
messageHelper.addBcc( bcc );

以下是我们如何发送电子邮件的示例(其中 mailSender 的类型为 JavaMailSender):

    MimeMessage message = this.mailSender.createMimeMessage();

try {
MimeMessageHelper e = new MimeMessageHelper(message, true, "utf-8");
e.setFrom(this.from);
message.setSubject(this.createSubject(entity));
this.setupRecipients(entity, e);
this.setupBody(entity, e);
this.setupAttachments(entity, e);
this.setupInlines(entity, e);
this.mailSender.send(message);
} catch (IOException | MessagingException var4) {
Throwables.propagate(var4);
}

当我尝试使用该特殊 SMTP 发送电子邮件时,我收到以下响应:

org.springframework.mail.MailSendException: Failed messages: javax.mail.SendFailedException: Invalid Addresses; 
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 504 5.5.2 <default>: Recipient address rejected: need fully-qualified address
; message exception details (1) are:
Failed message 1:
javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 504 5.5.2 <default>: Recipient address rejected: need fully-qualified address

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1196)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:584)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:433)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
at packagename.EmailExporter.export(EmailExporter.java:57)
at packagename.ExportJobImpl$ExportCallback.onResult_aroundBody0(ExportJobImpl.java:86)
at packagename.ExportJobImpl$ExportCallback$AjcClosure1.run(ExportJobImpl.java:1)
at org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$around$org_springframework_transaction_aspectj_AbstractTransactionAspect$1$2a73e96cproceed(AbstractTransactionAspect.aj:66)
at org.springframework.transaction.aspectj.AbstractTransactionAspect$AbstractTransactionAspect$1.proceedWithInvocation(AbstractTransactionAspect.aj:72)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$around$org_springframework_transaction_aspectj_AbstractTransactionAspect$1$2a73e96c(AbstractTransactionAspect.aj:70)
at packagename.ExportJobImpl$ExportCallback.onResult(ExportJobImpl.java:81)
at packagename.ExportableEntityEnumeratorImpl.lambda$iterate$9(ExportableEntityEnumeratorImpl.java:30)
at packagename.ExportableEntityEnumeratorImpl$$Lambda$31/1744828123.processRow(Unknown Source)
at org.springframework.jdbc.core.JdbcTemplate$RowCallbackHandlerResultSetExtractor.extractData(JdbcTemplate.java:1607)
at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:708)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:644)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:695)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:727)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:752)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:767)
at packagename.ExportableEntityEnumeratorImpl.iterate_aroundBody0(ExportableEntityEnumeratorImpl.java:28)
at packagename.ExportableEntityEnumeratorImpl$AjcClosure1.run(ExportableEntityEnumeratorImpl.java:1)
at org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$around$org_springframework_transaction_aspectj_AbstractTransactionAspect$1$2a73e96cproceed(AbstractTransactionAspect.aj:66)
at org.springframework.transaction.aspectj.AbstractTransactionAspect$AbstractTransactionAspect$1.proceedWithInvocation(AbstractTransactionAspect.aj:72)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$around$org_springframework_transaction_aspectj_AbstractTransactionAspect$1$2a73e96c(AbstractTransactionAspect.aj:70)
at packagename.ExportableEntityEnumeratorImpl.iterate(ExportableEntityEnumeratorImpl.java:27)
at packagename.ExportJobImpl.run(ExportJobImpl.java:50)
at packagename.MultiExportJobImpl.run(MultiExportJobImpl.java:42)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 504 5.5.2 <default>: Recipient address rejected: need fully-qualified address

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1094)
... 38 more

这是我们尝试过的列表:

  1. 首先,我们咨询了SMTP提供商;然而,他们的答案是收件人被设置为“默认”,仅此而已。我们还请求了日志,但从那以后他们没有提供它们(这不是我们的 SMTP,但他们甚至没有向所有者提供它们)。
  2. 其次,当我尝试使用 Spring 配置 JavaMail 并使用我的开发机器而不是我们的应用程序通常运行的服务器在本地发送电子邮件时,它按预期工作。此外,当配置为 ex 时,SMTP 发送电子邮件也没有问题。雷鸟。
  3. 当我们将他们的 SMTP 与我们服务器上的 SMTP 切换后,它就可以工作了。

结论是问题一定出在服务器配置中、服务器与 SMTP 之间或 SMTP 中。但是我不知道到底是什么原因造成的。

最佳答案

我发现发送电子邮件时出现问题的原因。但我不知道为什么会发生这种情况。

当我们将密件抄送或抄送地址添加到 MessageHelper 时,消息无法发送,并且收件人为“默认”。我尝试了不同的方法addBcc、setBcc、addCc、setCc,但它们都产生相同的结果。这很奇怪,我找不到解释。

更新:

我们找到了真正的罪魁祸首。密件抄送中使用的地址得到了值默认,这当然没有被记录和验证(这样的错误)。然而最有趣的是,由于注释 @Autowired,即使设置为其他正确的地址,bcc 也会获得值 default。有趣的是,即使我们没有声明任何可以连接到的值,Spring 也没有抛出异常。

关于java - Spring JavaMailSender : recipient set as "default",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37278210/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com