Very good: I am developing an app that should send an email to a contact account. I tried to do as I put it on the internet but I can not get it to work, besides when I run it, the app does not fail me but it does not do what I need. The code is as follows:
StrictMode.ThreadPolicy policy= new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
// Recipient's email ID needs to be mentioned.
String to = "[email protected]";
// Sender's email ID needs to be mentioned
String from = "[email protected]";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.starttls.required", "true");
properties.put("mail.smtp.auth", "true");
properties.put("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.host", "smtp.gmail.com");
properties.setProperty("mail.user", "mi_user");
properties.setProperty("mail.password", "mi_pass");
// Get the default Session object.
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("mi_user", "mi_pass");
}
});
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
message.setText("This is actual message");
// Send message
Transport.send(message);
Log.d("exito","exito");
} catch (MessagingException mex) {
mex.printStackTrace();
}