使用JAVA API查找知道电子邮件地址的SMTP主机和端口 [英] Finding SMTP host and port knowing the e-mail address using JAVA API

查看:826
本文介绍了使用JAVA API查找知道电子邮件地址的SMTP主机和端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个使用Java API发送电子邮件的简单应用程序并提出了一个问题:



有没有办法找出SMTP主机知道登录发送电子邮件的人的电子邮件地址?还有端口?



例如,如果发件人的电子邮件地址是sender@gmail.com,则SMTP主机是smtp.gmail.com如果发件人的电子邮件地址是sender@yahoo.com,则SMTP主机为smtp.yahoomail.com,端口为25。



假设我不知道这个,有没有办法使用Java API类找到这些信息?请注意我是java新手:)



提前致谢,



Andreea






感谢您的回答。我尝试过以下操作:

  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(找不到域名的MX记录+ hostName +。);
}

//返回第一个条目(不是最佳解决方案)
if(records.length> 0){
MXRecord mx =(MXRecord)记录[ 0];
returnValue = mx.getTarget()。toString();
}
} catch(TextParseException e){
抛出新的RuntimeException(e);
}

System.out.println(return value =+ returnValue);
返回returnValue;
}

但是,无论hostName的值如何(例如.gmail.com,yahoo) .com)
记录[]记录=新查找(hostName,Type.MX).run(); 始终返回null。



<我很确定我错过了什么,但我不知道是什么。
请你帮帮我吗?你能告诉我我做错了吗?



非常感谢你,



Andreea

解决方案

不幸的是,没有标准方法可以为任意电子邮件地址识别正确的外发SMTP服务器,假设您要做的是让用户指定一个电子邮件地址/密码,然后使用该帐户发送邮件。



这就是电子邮件客户端(例如Thunderbird,Outlook等)通常需要用户的原因手动配置传出SMTP服务器名称/端口。你可以通过识别一些流行的ISP(谷歌,雅虎等)并预先配置正确的值来协助这个过程,但是没有通用的方法可以自动完成。


I made a simple application to send e-mails using Java API and have a question:

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?

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.

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 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.

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

解决方案

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.

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主机和端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆