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
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();
}