Search host and port SMTP, knowing the email address using the JAVA API

I made a simple email sending application using the Java API and asked a question:

Is there a way to find out the SMTP host by knowing the email address of the person who will be logging in to send the email? And also the port?

For example, if the sender’s sender@gmail.com address is sender@gmail.com , the SMTP host is smtp.gmail.com and port 465. If the sender’s email address is sender@yahoo.com , the SMTP host is smtp.yahoomail.com and port 25.

Suppose I don't know this, is there a way to find this information using the Java API classes? Please note that I am new to java :)

Thanks in advance,

Andrea


Thank you for your responses. I 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 hostName value (for example, gmail.com, yahoo.com) Record [] records = new Lookup (hostName, Type.MX) .run (); always returns null.

I'm pretty sure I missed something, but I don't know what. could you help me? Can you tell me what I'm doing wrong?

Thanks a lot,

Andrea

+6
source share
4 answers

Unfortunately, there is no standard way to identify the correct outgoing SMTP server for an arbitrary email address, assuming what you are trying to do is allow the user to specify an email address / password, and then send mail using this account.

For this reason, email clients (for example, Thunderbird, Outlook, etc.) usually require that the user manually configure the name / port of the outgoing SMTP server. You could help in this process by recognizing several popular Internet providers (Google, Yahoo, etc.) and pre-setting the correct values, but there is no general purpose way to do this automatically.

+2
source

Something is not entirely clear in your question. You are either trying to find SMTP to send email to any address, so send it directly to your server. This is done through an MX record, as described above.

If, as I suspect, you are trying to find out your current user (in the field), which SMTP server to use to send your emails to the world. This is a completely different story. This cannot be determined safely. The MX record gives you an incoming email address for this domain, not an outgoing one. Most of the time it will work, but it does not guarantee. For example, GMail has an 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 

So far smtp.gmail.com (outbound):

 Name: gmail-smtp-msa.l.google.com Address: 173.194.67.108 

Or foobar.com may have smtp.foobar.com, but only accepts outgoing mail as internalmail.foobar.loc through its VPNs.

You can see this guessing game in the thunderbird setup, they will try to figure out the servers automatically, but they will ask you to confirm.

+1
source

Typically, you are talking to the smtp server that you own, and it processes the mail in the yahoo mailbox for any random ISP server.

The normal API to use is http://javamail.kenai.com/nonav/javadocs/ javamail.

If you write your own SMTP server: 1 request not 2 smtp information is stored in DNS mxrecord http://en.m.wikipedia.org/wiki/MX_record

0
source

It seems you are trying to allow the user to enter only the email address and password for the connection. If so, we had the same problem, and the best way we found was to get the domain name and:

  • If it's publicly available, like Gmail, Yahoo or Outlook, try using them for them.

  • If it's a private domain or something like that. Go through the outgoing servers smtp.domain.com and mail.domain.com using ports 587, 465 and 25. You may have to check for TLS and authentication.

The process is a bit long, but if you have several public emails and private private applications, you should check most of the scenarios.

0
source

Source: https://habr.com/ru/post/913805/


All Articles