Finding SMTP host and port knowing the e-mail addr

2020-02-14 02:32发布

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

4条回答
迷人小祖宗
2楼-- · 2020-02-14 02:39

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.

查看更多
地球回转人心会变
3楼-- · 2020-02-14 02:55

You typically talk to an smtp server you own and it handles routing mail to the yahoo Gmail some random isp to server.

The normal API to use is 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

查看更多
再贱就再见
4楼-- · 2020-02-14 03:00

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.

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:

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:

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.

You can see this guessing game in thunderbird setup, they try to find out the servers automatically but ask you for confirmation.

查看更多
\"骚年 ilove
5楼-- · 2020-02-14 03:00

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:

  1. If it is public like Gmail, Yahoo or Outlook then try their specific configuration for them.

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

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.

查看更多
登录 后发表回答