Home > OS >  Android: Amazon AWS SES won't send emails in certain Chinese mobiles
Android: Amazon AWS SES won't send emails in certain Chinese mobiles

Time:03-24

My app sends an activation code to email during user's registration process, but for an unknown -to me- reason in some Chinese devices is not working and I'm not being able to get a log of the Exception.

This is where I send the email:

 private void getAwsResult_getAwsUser(String strResult)
 {
     try{
         CommunityUserDTO communityUser = CommunityUserService.deserializeUser(strResult);

         if(communityUser==null){ //email don't exist in database
             registerResult.setValue(new Pair<>(null, Enum.RegisterResult.SENDINGEMAIL));
             CreateUserUseCase.setActivationCode(this.user);
             sendConfirmationEmail(this.user.getEmail(), this.user.getActivationCode());
         }else{
             registerResult.setValue(new Pair<>(null, Enum.RegisterResult.EMAILALREADYREGISTERED));
         }
     }catch(Exception ex)
     {
         ExceptionHandler.logException(ex);
     }
 }

 private void sendConfirmationEmail(String recipient, int activationCode)
 {
     String jsonEmail = SendEmailUseCase.
             generateJsonConfirmationEmail(recipient, activationCode);

     TaskRunner taskRunner = new TaskRunner();
     taskRunner.executeAsync(new SendEmailTask(jsonEmail), this::getAwsResult_sendEmail);
 } 

public class SendEmailTask implements Callable<String>
{
    private final String jsonEmail;

    public SendEmailTask(String jsonEmail)
    {
        this.jsonEmail = jsonEmail;
    }

    @Override
    public String call()
    {
        return TMEmail.send(jsonEmail);
    }
}

This is a jsonEmail input parameter for sendEmail method example:

{
   "subject":"Your activation code.",
   "message":"Your activation code is: xxxxxx",
   "send_to":"[email protected]",
   "send_from":"[email protected]",
   "smtp_server":"xxx.amazonaws.com",
   "smtp_user":"xxx",
   "smtp_pass":"xxx",
   "smtp_useauth":"true",
   "smtp_port":"25",
   "center_text":"true"
}

public static String send(String jsonEmail)
{
   String result = Enum.Result.OK;

   JSONObject oJsonEmail = TMJson.str2JSON(jsonEmail);

   try {
      String subject = oJsonEmail.getString(Enum.Email.SUBJECT);
      String message = oJsonEmail.getString(Enum.Email.MESSAGE);
      String sendFrom = oJsonEmail.getString(Enum.Email.SEND_FROM);
      String sendTo = oJsonEmail.getString(Enum.Email.SEND_TO);

      String smtpServer = oJsonEmail.getString(Enum.Email.SMTP_SERVER);
      String smtpPort = oJsonEmail.getString(Enum.Email.SMTP_PORT);
      String smtpUseAuth = oJsonEmail.getString(Enum.Email.SMTP_USEAUTH);
      boolean centerBodyText = Boolean.parseBoolean(oJsonEmail.getString(Enum.Email.CENTER_TEXT));

      final String smtpUser = oJsonEmail.getString(Enum.Email.SMTP_USER);
      final String smtpPass = oJsonEmail.getString(Enum.Email.SMTP_PASS);

      Properties props = new Properties();
      props.put("mail.smtp.host", smtpServer);
      props.put("mail.smtp.port", smtpPort);
      props.put("mail.smtp.auth", smtpUseAuth);
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.from.alias", "My App Name");

      Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
               protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication(smtpUser, smtpPass);
               }
            });

      message = TMJson.decodeEmail(message);
      message = format(message, centerBodyText);

      Message oMessage = new MimeMessage(session);
      oMessage.setFrom(new InternetAddress(sendFrom, session.getProperty("mail.from.alias"), "UTF-8"));
      oMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(sendTo));
      oMessage.setSubject(subject);
      oMessage.setContent(message, "text/html; charset=iso-8859-2");

      Transport.send(oMessage);
   }catch (MessagingException | UnsupportedEncodingException | JSONException e) {
      result = Enum.Result.KO;
      ExceptionHandler.logException(e);
   }

   return result;
}

I've tested this in my Android mobile and tablet, and in many other devices and it's working just fine, the email is sent and the user receives the activation code, but in China, where we are still testing the beta version the tester is not receiving the email.

I know the app is crashing before sending the email, but cannot figure out why, and if you notice, I hace a try/catch with this ExceptionHandler.logException(e); that sends a report of the crash to my email, but it's also not working, so I don't know what to do.

I contacted AWS support team to check if there is any possibility to see any SES logs, but has no response yet.

Any ideas on how could I get closer to the real exception? Still cannot even see it in the Google Play Developer Console crashes report section.

CodePudding user response:

Ok, luckily I found the issue myself. Nothing to do with Chinese devices, but with Android version.

I was testing my app in Android 10 devices (the only ones I have) and they were testing in an Android 11 device.

This link solved my problem:

Android Studio VerifyError rejecting class text_plain from JavaMail API

And this is the solution: Add this in build.gradle

android { ... packagingOptions { exclude 'META-INF/NOTICE.md' exclude 'META-INF/LICENSE.md' } }

dependencies { implementation 'com.sun.mail:android-mail:1.6.6' implementation 'com.sun.mail:android-activation:1.6.6' ... }

Hope I'm helping anyone else having problems sending emails.

  • Related