I've read the question linked here. I've tried this method to send email programmatily when, for example, user press a button.
It does works on 2.3.7 version of Android.
The issue is when trying to launch the app on more recent version of Android (like 4.1.2) it does nothin. On those devices the toast appears but email is not send.
So, i try this code, but it does not work neither.
...
sendButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//Read EditText
EditText text = (EditText)findViewById(R.id.editText1);
String my_mail = text.getText().toString().toLowerCase();
if (my_mail.matches("")){
Toast toast = Toast.makeText(context, "Please insert a valid email address!", Toast.LENGTH_LONG);
toast.show();
}
//send mail
try {
WebSendMail(my_mail);
Toast toast = Toast.makeText(context, "Email sent!", Toast.LENGTH_LONG);
toast.show();
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
}
});
public static boolean WebSendMail(String mail){
//email sender parameter
String smtpHost = "smtp.gmail.com";
String addressFrom = "account@gmail.com";
String login = addressFrom;
String password = "password";
String subject = "Subject!";
String message = "html code";
try {
Properties props = new Properties();
props.setProperty("mail.host", smtpHost);
props.setProperty("mail.smtp.port", "587");
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.starttls.enable", "true");
Authenticator auth = new SMTPAuthenticator(login, password);
Session session = Session.getInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
msg.setText(message);
msg.setSubject(subject);
msg.setContent(message, "text/html");
msg.setFrom(new InternetAddress(addressFrom));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(mail));
Transport.send(msg);
return true;
} catch (Exception ex) {
return false;
}
}
Any help?