Search code examples
javajakarta-mailexchange-servergodaddy-apisimple-java-mail

Which SMTP sever to send Java Mail to when the server is cloud-based?


Java 8 here, although this really is more of a generic Exchange/SMTP question I suppose.

I purchased a domain name and Office 365 support from GoDaddy, and as a result, have several email addresses through them. Let's say one of these is [email protected].

I am writing a Java 8 app that will allow users to send emails to this support address (through the app). I will likely use the excellent Simple Java Mail library, which wraps the Java Mail API. And so somewhere in my app there will be some code that looks something like:

Email email = EmailBuilder.startingBlank()
  .to("MyApp Help Desk", "[email protected]")
  .withSubject("Support Ticket #12345")
  .withPlainText("Something went wrong inside the MyApp!")
  .buildEmail();

Mailer mailer = MailerBuilder
  .withSMTPServer("smtp.host.com", 587, "[email protected]", "password")
  .withTransportStrategy(TransportStrategy.SMTP_TLS)
  .withProperty("mail.smtp.sendpartial", true)
  .buildMailer();

mailer.sendMail(email);

Pretty straight-forward stuff, right? Except I'm confused about one simple thing: which SMTP server am I using here? Would I use the Microsoft Exchange server that my GoDaddy account provides me, or would I choose something else?


Solution

  • That depends a little bit on what you want to do.

    If you just want to make one attempt to send the mail, you connect to the mail server that accepts mail for [email protected]. It sounds like this is an office365 server owned by Microsoft that accepts mail for myapp.example.com. There should be public DNS records for that domain. I haven't used the Simple Java Mail library, but from your pseudocode, I assume it does the DNS lookups for you.

    It sometimes happens that an application encounters errors when trying to send an email. For example, somebody could restart your app server in the middle of the smtp conversation, or reboot your router, or something else. It's pretty common to use a Mail Transfer Agent (MTA) such as Exchange to help make this process more robust. If the MTA fails on the first delivery attempt, it will retry delivery for some configurable period of time. In your case, you could send the email to your Exchange server, and it would route the email to myapp.example.com.

    (In this case, Exchange is sort of acting as a Mail Submission Agent (MSA), but that's kind of a nitpick.)

    Hope this helps.