Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One possible solution: get the MX record on your own and use <a href="http://java.sun.com/products/javamail/" rel="noreferrer">JavaMail API</a>.</p> <p>You can get the MX record using the <a href="http://www.dnsjava.org/" rel="noreferrer">dnsjava</a> project:</p> <p>Maven2 dependency:</p> <pre><code>&lt;dependency&gt; &lt;groupId&gt;dnsjava&lt;/groupId&gt; &lt;artifactId&gt;dnsjava&lt;/artifactId&gt; &lt;version&gt;2.0.1&lt;/version&gt; &lt;/dependency&gt; </code></pre> <p>Method for MX record retrieval:</p> <pre><code>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 + "."); } if (log.isTraceEnabled()) { // log found entries for debugging purposes for (int i = 0; i &lt; records.length; i++) { MXRecord mx = (MXRecord) records[i]; String targetString = mx.getTarget().toString(); log.trace("MX-Record for '" + hostName + "':" + targetString); } } // return first entry (not the best solution) if (records.length &gt; 0) { MXRecord mx = (MXRecord) records[0]; returnValue = mx.getTarget().toString(); } } catch (TextParseException e) { throw new RuntimeException(e); } if (log.isTraceEnabled()) { log.trace("Using: " + returnValue); } return returnValue; } private static String getHostNameFromEmailAddress(String mailAddress) throws TextParseException { String parts[] = mailAddress.split("@"); if (parts.length != 2) throw new TextParseException("Cannot parse E-Mail-Address: '" + mailAddress + "'"); return parts[1]; } </code></pre> <p>Sending mail via JavaMail code:</p> <pre><code>public static void sendMail(String toAddress, String fromAddress, String subject, String body) throws AddressException, MessagingException { String smtpServer = getMXRecordsForEmailAddress(toAddress); // create session Properties props = new Properties(); props.put("mail.smtp.host", smtpServer); Session session = Session.getDefaultInstance(props); // create message Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress(fromAddress)); msg.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress)); msg.setSubject(subject); msg.setText(body); // send message Transport.send(msg); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload