Hi All,

I am using javax mail api in android to send email to different users.I have created my own userId and password using gmail, I passing the using this new UserId and Password in code to authorize and send mail. it worked fine. But suddenly the mails are not getting send, and gives "javax.mail.AuthenticationFailedException". But once i sign in to the account from my console mail gets start working again. I am getting why this problem as I am passing userid and password, there should not be any issue of token expiration, or is there?. I am really stuck can anyone help me out?

Following is hoe am sending mail:

private ResultCode sendMail(Context context, String imagePath,
  String subject, String headerText, String footerText) {
  ResultCode resultCode;
  String excludedText = "hi";
 
  Properties props = new Properties();
  // Default user name for SMTP
  props.put("mail." + PROTOCOL + ".user", mUserId);
  // The SMTP server to connect to
  props.put("mail." + PROTOCOL + ".host", HOST);
  // The SMTP server port to connect to, if the connect() method doesn't
  // explicitly specify one. Defaults to 25.
  props.put("mail." + PROTOCOL + ".port", PORT);
  // If true, enables the use of the STARTTLS command (if supported by the
  // server) to switch the connection to a TLS-protected connection before
  // issuing any login commands. Defaults to false.
  props.put("mail." + PROTOCOL + ".starttls.enable", "true");
  // If set to true, use SSL to connect and use the SSL port by default.
  // Defaults to false for the "smtp" protocol and true for the "smtps"
  // protocol
  // modified to false from true..
  props.put("mail." + PROTOCOL + ".ssl.enable", "false");
  // If true, attempt to authenticate the user using the AUTH command.
  // Defaults to false.
  props.put("mail." + PROTOCOL + ".auth", "true");
  // Specifies the port to connect to when using the specified socket
  // factory. If not set, the default port will be used.
  props.put("mail." + PROTOCOL + ".socketFactory.port", PORT);
  // If set, specifies the name of a class that implements the
  // javax.net.SocketFactory interface. This class will be used to create
  // SMTP sockets.
  props.put("mail." + PROTOCOL + ".socketFactory.class",
  "javax.net.ssl.SSLSocketFactory");
  // If set to true, failure to create a socket using the specified socket
  // factory class will cause the socket to be created using the
  // java.net.Socket class. Defaults to true.
  // modified to true from false..
  props.put("mail." + PROTOCOL + ".socketFactory.fallback", "true");
  try {
  Authenticator auth = new SMTPAuthenticator();
  Session session = Session.getInstance(props, auth);
  MimeMessage msg = new MimeMessage(session);
  Multipart multipart = new MimeMultipart();
 
  // attach image if imagepath is present
  if (!TextUtils.isEmpty(imagePath)
  && FileUtils.isFileExist(imagePath)) {
 messageBodyPart = new MimeBodyPart();
 
  String compressedFileName = BitmapHelper
  .compressImage(imagePath);
  File imageFile = new File(compressedFileName);
 
  DataSource source = new FileDataSource(imageFile);
  messageBodyPart.setDataHandler(new DataHandler(source));
 
  String imageName = ImageUtils.getImageName(compressedFileName);
  messageBodyPart.setFileName(imageName);
 
  messageBodyPart.setDisposition(MimeBodyPart.INLINE);
  messageBodyPart.setHeader("Content-ID", "<krumbs>");
  multipart.addBodyPart(messageBodyPart);
 
  messageBodyPart = new MimeBodyPart();
 
  String imageHtml = "<html><body>"
  + "<div style='margin-left: auto;  margin-right: auto;  "
  + " position: relative;' align='center'>"
  + "<img src=\"cid:krumbs\"></div></body></html>";
 
  messageBodyPart.setContent(imageHtml, HTML_TYPE);
  multipart.addBodyPart(messageBodyPart);
  }
 
  if (!TextUtils.isEmpty(hashExcludedText)) {
  messageBodyPart = new MimeBodyPart();
 
  String footerTextPart = "<h3 style='width:100%;color:gray;text-align:left;font-family:sans-serif;text-align: center;'>"
  + excludedText + "</h3>";
 
  messageBodyPart.setContent(footerTextPart, HTML_TYPE);
  multipart.addBodyPart(messageBodyPart);
  }
 
  // Set the complete message parts
  msg.setContent(multipart);
 
  msg.setReplyTo(new javax.mail.Address[] { new javax.mail.internet.InternetAddress(
  mSenderId) });
 
  msg.setSubject(subject);
  msg.setFrom(new InternetAddress(mSenderId, getUsername(context,
  mSenderId)));
 
  if (mRecipientIds.indexOf(',') > 0)
  msg.setRecipients(Message.RecipientType.TO,
  InternetAddress.parse(mRecipientIds));
  else
  msg.setRecipient(Message.RecipientType.TO, new InternetAddress(
  mRecipientIds));
 
  Transport.send(msg);
 
  resultCode = ResultCode.SUCCESS;
  } catch (Exception e) {
  e.printStackTrace();
  String error_message = e.getMessage();
  KrumbsAnalytics.getInstance(context).trackBug(context,
  "Send_Email: error =  " + error_message);
  resultCode = ResultCode.ERROR;
  } finally {
  Log.i("check", "mail sent");
  }
  return resultCode;
  }