<p>I'd like to send mail without bothering with the SMTP-Server which is used for delivery.</p>

<p>So <a href="http://java.sun.com/products/javamail/" rel="noreferrer">JavaMail API</a> doesn't work for me because I have to specify a SMTP server to connect to.</p>

<p>I'd like the library to find out on its own which SMTP server is responsible for which email address by querying the MX record of the mail address domain.</p>

<p>I'm looking for something like <a href="https://aspirin.dev.java.net/" rel="noreferrer">Aspirin</a>. Unfortunately I can't use Aspirin itself because the development stopped 2004 and the library fails to communicate with modern spam hardened servers correctly.</p>

<p>An embeddable version of <a href="http://james.apache.org/" rel="noreferrer">James</a> would do the task. But I haven't found documentation concerning whether this is possible.</p>

<p>Or does anyone know about other libraries I could use?</p>

One possible solution: get the MX record on your own and use
JavaMail API
.
You can get the MX record using the
dnsjava
project:
Maven2 dependency:
```
```
<dependency>
    <groupId>dnsjava</groupId>
    <artifactId>dnsjava</artifactId>
    <version>2.0.1</version>
</dependency>
```
```
Method for MX record retrieval:
```
```
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 < 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 > 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];
}
```
```
Sending mail via JavaMail 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);
}
```
```