Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding SMTP host and port knowing the e-mail address using JAVA API

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

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 very much,

Andreea

like image 337
Ioja Andreea Avatar asked Apr 21 '12 18:04

Ioja Andreea


People also ask

How do I find my SMTP host and port?

If you are subscribed to a hosted email relay service you can get the SMTP server hostname and port number from the support page of your email service. If you run your own SMTP server you can find the configured SMTP port number and address from the SMTP server configuration.

What is mail SMTP host in Java?

SMTP is an acronym for Simple Mail Transfer Protocol. It is an Internet standard for electronic mail (e-mail) transmission across Internet Protocol (IP) networks.


1 Answers

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.

like image 175
David Gelhar Avatar answered Oct 26 '22 17:10

David Gelhar