1

I am trying to send email using Mailgun. According to mailgun, I am using the following code :

public static void TestMail() throws Exception
{

            Properties props = System.getProperties();
            props.put("mail.smtps.host","smtp.mailgun.org");
            props.put("mail.debug", "true");
            props.put("mail.verbose", "true");
            props.put("mail.smtps.auth","true");
            Session session = Session.getInstance(props, null);
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("fromaddress@gmail.com"));
            msg.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("toaddress@gmail.com", false));
            msg.setSubject("Hello");
            msg.setText("Testing some Mailgun awesomness");
            msg.setSentDate(new Date());
            SMTPTransport t =
                    (SMTPTransport)session.getTransport("smtps");
            t.setStartTLS(true);
            t.connect("smtp.mailgun.com", "postmaster@sandbox86837*********.mailgun.org", "b071d**********");
            t.sendMessage(msg, msg.getAllRecipients());
            System.out.println("Response: " + t.getLastServerResponse());
            t.close();


}

I am using the javamail api downloaded from : https://code.google.com/p/javamail-android/downloads/list

NOW IN THE DEBUG FOLLOWING INFORMATION IS DISPLAYED :

DEBUG: JavaMail version 1.4.1
DEBUG: not loading file: /system/lib/javamail.providers
DEBUG: java.io.FileNotFoundException: /system/lib/javamail.providers: open failed: ENOENT (No such file or directory)
DEBUG: not loading resource: /META-INF/javamail.providers
 DEBUG: not loading resource: /META-INF/javamail.default.providers
DEBUG: failed to load any providers, using defaults

DEBUG: not loading resource: /META-INF/javamail.default.address.map
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.address.map
DEBUG: not loading file: /system/lib/javamail.address.map
 DEBUG: java.io.FileNotFoundException: /system/lib/javamail.address.map: open failed: ENOENT (No such file or directory)
DEBUG: failed to load address map, using defaults
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc.,1.4.1]
DEBUG SMTP: useEhlo true, useAuth true


DEBUG SMTP: trying to connect to host "smtp.mailgun.com", port 465, isSSL true
220 ak47 ESMTP ready
DEBUG SMTP: connected to host "smtp.mailgun.com", port: 465
EHLO
501 5.5.4 Invalid argument
 HELO
501 5.5.4 Invalid argument

I have no idea what this error means ? When I googled no one pointed the exact cause of the error or the reason. It could be ssl or my system is missing some java library ?? Or the java lib that I downloaded is not correct.

But as you see in the debug, it was able to connect successfully to smtp.mailgun.com. Then why this error ? Please please help me.

Thanks

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
intellignt_idiot
  • 1,962
  • 2
  • 16
  • 23

3 Answers3

0

The server to which you are connecting is requiring that the argument to the HELO or EHLO be either a hostname or an IP address.

Your code does not seem to be sending either.

The RFC (RFC2821 page 29) seems to be somewhat ambiguous...

...the client SHOULD send an IP address if no hostname is available

where the word MUST would have been used if this was a requirement. But the syntax description of HELO/EHLO seems to indicate the argument is required.

Also in section 3.6 (on Domains):

  • The domain name given in the EHLO command MUST BE either a primary host name (a domain name that resolves to an A RR) or, if the host has no name, an address literal as described in section 4.1.1.1.
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • 1
    If JavaMail is not sending the host address, it's becaue the JDK, er, Android non-Java runtime, can't figure out the name of your host. You can set the [mail.smtp.localhost property](https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html) to the host name you want to use in the HELO/EHLO command. Also, you might want to try the newer [JavaMail for Android](https://java.net/projects/javamail/pages/Home#JavaMail_for_Android). – Bill Shannon Dec 10 '15 at 03:54
  • @BillShannon Is there any reason why OP shouldn't be using a current release? And is that JAR at Google Code the real thing or somebody's substitute? – user207421 Dec 10 '15 at 04:21
  • @Jim So, if I set the mail.smtp.localhost propery, it would suffice the requirement of a hostname ? I mean , how to use the command helo/ehlo. Or simply put, what do I need to do here ? – intellignt_idiot Dec 10 '15 at 05:00
  • Do whatever JavaMail needs in order to send a host name or IP address wit the EHLO. – Jim Garrison Dec 10 '15 at 06:22
  • Okay. But what should be the host name ? My own or of the server (Mailgun) which I am using to send the email ? props.put("mail.smtp.ehlo", WHAT SHOULD i WRITE HERE ?); – intellignt_idiot Dec 10 '15 at 15:15
  • @Jim I have tried with all ip addresses , but is not working. Still the same error. – intellignt_idiot Dec 10 '15 at 15:34
  • It needs to be the host name of the machine you're sending the mail from. If it's not working, update your post with the details of what you did, including the debug output. – Bill Shannon Dec 10 '15 at 21:42
  • 1
    @EJP I strongly recommend using the current JavaMail version. It's not a final release yet, but it will be very soon. The Google Code version is a fork of a much older version that isn't being updated by the person who created the fork. – Bill Shannon Dec 10 '15 at 21:44
  • @Bill I did use my ip address both local as well my ip but still it gave me the same error. So I decided to drop the use of Javamail and used RETROFIT with Mailgun. And EPJ, Thanks for your suggestion. Yes thats why I did not use the newer version as it was not final. – intellignt_idiot Dec 12 '15 at 11:37
  • What did the debug output show when you set mail.smtps.localhost? (Since you're explicitly using the "smtps" transport instead of the "smtp" transport, the property name needs to use "smtps" as well.) – Bill Shannon Dec 12 '15 at 22:29
0

So, finally I was able to send mail using MAILGUN. But I had to drop the idea of using JAVAMAIL for sending as I could not solve this issue of invalid argument.

Retrofit provides a class for sending email using MAILGUN.I am posting the code I used here for anyone facing problem with Mailgun :

public interface SendMailApi {
@Headers({ACCEPT_JSON_HEADER})
@FormUrlEncoded
@POST("/messages")
void authUser(
        @Header("Authorization") String authorizationHeader,
        @Field("from") String from,
        @Field("to") String to,
        @Field("subject") String subject,
        @Field("text") String text,
        Callback<MailGunResponse> cb
);

}

public void sendMail(String to, String subject, String msg,      Callback<MailGunResponse> cb){
String from = "test <test@address.com>";
String clientIdAndSecret = "api" + ":" + "key-*******";
String authorizationHeader = BASIC + " " +     Base64.encodeToString(clientIdAndSecret.getBytes(), Base64.NO_WRAP);
sendMailApi.authUser(authorizationHeader,from, to, subject, msg, cb);
}

public MailGun() {
RestAdapter restAdapter = getAuthAdapter();
sendMailApi = restAdapter.create(SendMailApi.class);
}

private RestAdapter getAuthAdapter(){
RestAdapter.LogLevel logLevel = RestAdapter.LogLevel.FULL;
if(DEBUG)logLevel = RestAdapter.LogLevel.FULL;
return new RestAdapter.Builder()
        .setEndpoint(ENDPOINT)
        .setConverter(new GsonConverter(new Gson()))
        .setLogLevel(logLevel)
        .build();
}
intellignt_idiot
  • 1,962
  • 2
  • 16
  • 23
0

I suggest you to use the RESTful API instead of the SMTP legacy backend. You can use the Jersey library and the official documentation of Mailgun which provides accurate examples.

Also, and a big disclaimer, I've recently published a Java Mailgun library to ease with that. See this answer.

Community
  • 1
  • 1
sargue
  • 5,695
  • 3
  • 28
  • 43