java 使用JAVA API查找知道电子邮件地址的SMTP主机和端口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10261995/
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
Finding SMTP host and port knowing the e-mail address using JAVA API
提问by Ioja Andreea
I made a simple application to send e-mails using Java API and have a question:
我制作了一个简单的应用程序来使用 Java API 发送电子邮件并有一个问题:
Is there any way to find out the SMTP host knowing the e-mail address of the one who will login to send an e-mail? And also the port?
有没有办法找出知道将登录发送电子邮件的人的电子邮件地址的 SMTP 主机?还有港口?
For example, if the sender's e-mail address is [email protected], the SMTP host is smtp.gmail.com and the port 465. If the sender's e-mail address is [email protected], the SMTP host is smtp.yahoomail.com and the port 25.
例如,如果发件人的电子邮件地址为[email protected],则SMTP 主机为smtp.gmail.com,端口为465。如果发件人的电子邮件地址为[email protected],则SMTP 主机为smtp .yahoomail.com 和端口 25。
Supposing I don't know this, is there any way to find this information using Java API classes? Please note that I'm new to java :)
假设我不知道这一点,有没有办法使用 Java API 类找到这些信息?请注意,我是 Java 新手 :)
Thanks in advance,
提前致谢,
Andreea
安德烈亚
Thanks for your answers. I've tried to do the following:
感谢您的回答。我尝试执行以下操作:
public static String getMXRecordsForEmailAddress(String eMailAddress) {
String returnValue = null;
try {
String hostName = getHostNameFromEmailAddress(eMailAddress);
Record[] records = new Lookup(hostName, Type.MX).run();
if (records == null) {
throw new RuntimeException("No MX records found for domain " + hostName + ".");
}
// return first entry (not the best solution)
if (records.length > 0) {
MXRecord mx = (MXRecord) records[0];
returnValue = mx.getTarget().toString();
}
} catch (TextParseException e) {
throw new RuntimeException(e);
}
System.out.println("return value = "+returnValue);
return returnValue;
}
But, regardless of the value of hostName (eg. gmail.com, yahoo.com ) Record[] records = new Lookup(hostName, Type.MX).run();always return null.
但是,不管 hostName 的值(例如 gmail.com, yahoo.com ) Record[] records = new Lookup(hostName, Type.MX).run(); 总是返回空值。
I'm pretty sure that I missed something, but I don't know what. Will you please help me with this? Can you tell me what I'm doing wrong?
我很确定我错过了一些东西,但我不知道是什么。你能帮我解决这个问题吗?你能告诉我我做错了什么吗?
Thank you verry much,
非常感谢你,
Andreea
安德烈亚
回答by David Gelhar
Unfortunately, there's no standard way to identify the correct outgoing SMTP server for an arbitrary email address, assuming what you're trying to do is let the user specify an email address/password and then send the mail using that account.
不幸的是,没有标准方法可以为任意电子邮件地址识别正确的外发 SMTP 服务器,假设您尝试做的是让用户指定电子邮件地址/密码,然后使用该帐户发送邮件。
That's why email clients (e.g. Thunderbird, Outlook, etc.) generally require the user to configure the outgoing SMTP server name/port manually. You could assist in that process by recognizing a few popular ISPs (Google, Yahoo, etc.) and pre-configuring the proper values, but there's no general-purpose way to do that automatically.
这就是电子邮件客户端(例如 Thunderbird、Outlook 等)通常要求用户手动配置外发 SMTP 服务器名称/端口的原因。您可以通过识别一些流行的 ISP(Google、Yahoo 等)并预先配置正确的值来协助该过程,但没有通用的方法可以自动执行此操作。
回答by Eric Darchis
Something is not really clear in your question. You are either trying to find out the SMTP to send email to some address, so send it directly to their server. That's done through the MX record as explained above.
你的问题有些不太清楚。您要么试图找出 SMTP 以将电子邮件发送到某个地址,因此将其直接发送到他们的服务器。这是通过 MX 记录完成的,如上所述。
If, as I suspect, you are trying to find out for your current user (in the from field) which SMTP server to use to send his emails to the world. This is a different story. It cannot be determined safely. The MX record gives you the address for incoming email of that domain, not outgoing. Most of the time, it will work but no guarantee. GMail for example has in its MX record:
如果,正如我怀疑的那样,您正试图为当前用户(在 from 字段中)找出使用哪个 SMTP 服务器向全世界发送他的电子邮件。这是一个不同的故事。不能安全地确定。MX 记录为您提供该域的传入电子邮件地址,而不是传出电子邮件的地址。大多数情况下,它会起作用,但不能保证。例如,GMail 在其 MX 记录中有:
alt1.gmail-smtp-in.l.google.com internet address = 173.194.70.27
alt2.gmail-smtp-in.l.google.com internet address = 173.194.69.27
alt4.gmail-smtp-in.l.google.com internet address = 173.194.79.27
While the smtp.gmail.com (outgoing) is:
而 smtp.gmail.com(传出)是:
Name: gmail-smtp-msa.l.google.com
Address: 173.194.67.108
Or a company foobar.com might have smtp.foobar.com but only accept outgoing mail as internalmail.foobar.loc via their VPN.
或者公司 foobar.com 可能有 smtp.foobar.com 但只接受通过他们的 VPN 作为 internalmail.foobar.loc 的外发邮件。
You can see this guessing game in thunderbird setup, they try to find out the servers automatically but ask you for confirmation.
您可以在 Thunderbird 设置中看到这个猜谜游戏,他们会尝试自动查找服务器,但会要求您确认。
回答by Paulo Künzel
It seems you are trying to let the user type only the email and password to connect. If so, we had this same issue and the best way we've found was to get the domain name and:
您似乎试图让用户只输入电子邮件和密码进行连接。如果是这样,我们遇到了同样的问题,我们发现的最好方法是获取域名,并且:
If it is public like Gmail, Yahoo or Outlook then try their specific configuration for them.
If it is privte domain or something like it. Loop through outgoing servers smtp.domain.com and mail.domain.com using ports 587, 465 and 25. You'll probably have to check for TLS and authentication.
如果它像 Gmail、Yahoo 或 Outlook 一样是公开的,那么请尝试为它们进行特定的配置。
如果它是私有域或类似的东西。使用端口 587、465 和 25 循环传出服务器 smtp.domain.com 和 mail.domain.com。您可能需要检查 TLS 和身份验证。
The process is a bit long, but if you have a couple of public emails and a dozend private ones you should be able to test most of the scenarios.
这个过程有点长,但如果你有几个公共电子邮件和十几个私人电子邮件,你应该能够测试大多数场景。
回答by twk
You typically talk to an smtp server you own and it handles routing mail to the yahoo Gmail some random isp to server.
您通常与您拥有的 smtp 服务器交谈,它处理将邮件路由到 yahoo Gmail 的一些随机 isp 到服务器。
The normal API to use is http://javamail.kenai.com/nonav/javadocs/javamail.
要使用的常规 API 是http://javamail.kenai.com/nonav/javadocs/javamail。
If you were writing your own smtp server: 1 please don't 2 the smtp info is stored in the DNS mxrecord http://en.m.wikipedia.org/wiki/MX_record
如果您正在编写自己的 smtp 服务器:1 请不要 2 smtp 信息存储在 DNS mxrecord http://en.m.wikipedia.org/wiki/MX_record