JavaMail SMTP主机错误 [英] JavaMail SMTP-host error
问题描述
我有一个发送电子邮件的Grails应用程序。邮件服务器没有SMTP认证,所以它需要SMTP之前的POP,这意味着我需要在通过SMTP发送之前对POP帐户进行认证。大多数情况下它可以正常工作,但偶尔会发生,邮件服务器不能从属性中获取,而是尝试连接到localhost。这里是属性:
属性props = new Properties();
props.setProperty(mail.store.protocol,pop3)
props.setProperty(mail.pop3.host,mail.xxxxx.com)
props.setProperty(mail.pop3.port,110)
props.setProperty(mail.smtp.host,mail.xxxxx.com)
props.setProperty( mail.smtp.port,25)
props.setProperty(mail.smtp.sendpartial,true)
props.setProperty(mail.pop3.socketFactory.port, 110)
props.setProperty(mail.pop3.socketFactory.class,javax.net.SocketFactory)
props.setProperty(mail.pop3.socketFactory.fallback,false )
Transport t = null
def store
try {
URLName url = new URLName(pop3,mail.xxxxxxx.com,110,
INBOX,用户名,密码);
Session session = Session.getDefaultInstance(props,null)
store = session.getStore(url)
store.connect(mail.xxxxxx.com ,用户名,密码)
留言信息=新的MimeMessage(会话);
message.setFrom(new InternetAddress(xxxx@xxxxxx.com));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toAddress,true));
message.setSubject(mailTitle);
message.setContent(messageBody,text / html);
message.setSentDate(new Date());
$ bt = session.getTransport(smtp)
t.connect()
t.send(消息)
返回true
}
catch(AddressException e){
e.printStackTrace()
返回false
}
catch(MessagingException e){
e.printStackTrace()
返回false
}
finally {
store?.close()
}
通常情况下,这是有效的。但是,当一个Quartz Job正在发送时,来自Properties的邮件服务器条目没有兑现,它使用localhost代替,然后无法发送电子邮件。
我可以连接Telnet并使用提及的属性发送。
这可能是超时问题吗?根据我在文档中读到的内容,超时是默认的无限,所以应该不是问题。
这可能是性能问题吗?我创建了一个虚拟应用程序,它或多或少地具有相同的功能,但不会从Grails服务发出,而是直接从控制器发出。那个人一直都在工作,但那个应用总是处于闲置状态。
我没有使用Mail Plugin for Grails,因为我看不到它可以处理pop before smtp-paradigm。
预先感谢。解决方案:看起来好像解决方案是放置mail.smtp.localhost - 值与mail.smtp.host的值相同。自从我把这个属性放进去以后,没有一封邮件失败了。我认为这不是一个明显的属性,并且从我的角度来看是无知的,但是,我希望这将在未来帮助其他人。
所以,我的属性如下:
属性props = new Properties( );
props.setProperty(mail.store.protocol,pop3)
props.setProperty(mail.pop3.host,mail.xxxxxxx.com)
props.setProperty(mail.pop3.port,110)
props.setProperty(mail.smtp.localhost,mail.xxxxxxx.com)
props.setProperty( mail.smtp.host,mail.xxxxxxx.com)
props.setProperty(mail.smtp.port,25)
props.setProperty(mail.smtp.sendpartial ,true)
props.setProperty(mail.pop3.socketFactory.port,110)
props.setProperty(mail.pop3.socketFactory.class,javax.net .SocketFactory)
props.setProperty(mail.pop3.socketFactory.fallback,false)
将mail.smtp.host的值设置为属性mail.smtp.localhost,不仅使错误消失,整个程序变得快得多。
感谢您的努力!
I have a Grails-application that sends emails. The mailserver has no SMTP-authentication, so it requires "POP before SMTP", which means that I need to authenticate against the POP-account before sending through SMTP. Most often it works, but then once in a while, the mailserver is not picked up from the properties, and it tries to connect to "localhost" instead. Here is the properties:
Properties props = new Properties();
props.setProperty("mail.store.protocol", "pop3")
props.setProperty("mail.pop3.host", "mail.xxxxx.com")
props.setProperty("mail.pop3.port", "110")
props.setProperty("mail.smtp.host", "mail.xxxxx.com")
props.setProperty("mail.smtp.port", "25")
props.setProperty("mail.smtp.sendpartial", "true")
props.setProperty("mail.pop3.socketFactory.port", "110")
props.setProperty("mail.pop3.socketFactory.class","javax.net.SocketFactory")
props.setProperty("mail.pop3.socketFactory.fallback", "false")
Transport t = null
def store
try {
URLName url = new URLName("pop3", "mail.xxxxxxx.com", 110,
"INBOX", "username", "password");
Session session = Session.getDefaultInstance(props, null)
store = session.getStore(url)
store.connect("mail.xxxxxx.com", "username", "password")
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("xxxx@xxxxxx.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toAddress, true));
message.setSubject(mailTitle);
message.setContent(messageBody, "text/html");
message.setSentDate(new Date());
t = session.getTransport("smtp")
t.connect()
t.send(message)
return true
}
catch (AddressException e) {
e.printStackTrace()
return false
}
catch (MessagingException e) {
e.printStackTrace()
return false
}
finally {
store?.close()
}
More often than not, this works. But when a Quartz Job is doing the sending, the mailserver entry from the Properties is not honored and it uses "localhost" instead and then fails to send the emails.
I can connect with Telnet and send with the attributes mentioned.
Could it be a timeout issue? According to what I've read in docs, the timeouts are "infinite" as default, so that "should" not be the problem.
Could it be a performance issue? I've created a "dummy app", which - more or less - does the same, but does not issue the sending from a Grails service, but directly from a controller. That one works all the time, but that app is always idleing.
I'm not using the Mail Plugin for Grails, since I couldn't see that it could handle the "pop before smtp"-paradigm.
Thanks in advance.
Solution: It looks as though the solution is to put the "mail.smtp.localhost"-value to the same value as for "mail.smtp.host". Not one single mail has failed since I put that property in. I don't think it was an obvious property to set and an ignorance from my point of view, nevertheless, I hope this will help someone else in the future.
So, my properties are as follows:
Properties props = new Properties();
props.setProperty("mail.store.protocol", "pop3")
props.setProperty("mail.pop3.host", "mail.xxxxxxx.com")
props.setProperty("mail.pop3.port", "110")
props.setProperty("mail.smtp.localhost", "mail.xxxxxxx.com")
props.setProperty("mail.smtp.host", "mail.xxxxxxx.com")
props.setProperty("mail.smtp.port", "25")
props.setProperty("mail.smtp.sendpartial", "true")
props.setProperty("mail.pop3.socketFactory.port", "110")
props.setProperty("mail.pop3.socketFactory.class","javax.net.SocketFactory")
props.setProperty("mail.pop3.socketFactory.fallback", "false")
Setting the value of "mail.smtp.host" to the property "mail.smtp.localhost", not only made the errors go away, the entire routine became a lot faster.
Thank you for your efforts!
这篇关于JavaMail SMTP主机错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!