Finding SMTP host and port knowing the e-mail address using JAVA API(使用 JAVA API 查找知道电子邮件地址的 SMTP 主机和端口)
问题描述
我做了一个简单的应用程序来使用 Java API 发送电子邮件并有一个问题:
I made a simple application to send e-mails using Java API and have a question:
有什么方法可以找出知道要登录发送电子邮件的人的电子邮件地址的 SMTP 主机吗?还有港口?
例如,如果发件人的电子邮件地址是 sender@gmail.com,则 SMTP 主机是 smtp.gmail.com,端口是 465.如果发件人的电子邮件地址是 sender@yahoo.com,则 SMTP主机是 smtp.yahoomail.com 和端口 25.
For example, if the sender's e-mail address is sender@gmail.com, the SMTP host is smtp.gmail.com and the port 465. If the sender's e-mail address is sender@yahoo.com, the SMTP host is smtp.yahoomail.com and the port 25.
假设我不知道这一点,有没有办法使用 Java API 类找到这些信息?请注意,我是 java 新手 :)
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 :)
提前致谢,
安德烈亚
感谢您的回答.我已尝试执行以下操作:
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;
}
但是,无论 hostName 的值如何(例如 gmail.com、yahoo.com )Record[] records = new Lookup(hostName, Type.MX).run(); 总是返回 null.
But, regardless of the value of hostName (eg. gmail.com, yahoo.com ) Record[] records = new Lookup(hostName, Type.MX).run(); always return null.
我很确定我错过了什么,但我不知道是什么.你能帮我解决这个问题吗?你能告诉我我做错了什么吗?
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?
非常感谢,
安德烈亚
推荐答案
不幸的是,没有标准的方法来识别任意电子邮件地址的正确传出 SMTP 服务器,假设您尝试做的是让用户指定一个电子邮件地址/密码,然后使用该帐户发送邮件.
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.
这就是为什么电子邮件客户端(例如 Thunderbird、Outlook 等)通常需要用户手动配置传出 SMTP 服务器名称/端口的原因.您可以通过识别一些流行的 ISP(Google、Yahoo 等)并预先配置正确的值来协助该过程,但没有通用的方法可以自动执行此操作.
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.
这篇关于使用 JAVA API 查找知道电子邮件地址的 SMTP 主机和端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 JAVA API 查找知道电子邮件地址的 SMTP 主机和端口


- Jersey REST 客户端:发布多部分数据 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01