Guest User

Untitled

a guest
Mar 30th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. public class SendMail extends AsyncTask<Void, Void, Void> {
  2.  
  3. //Declaring Variables
  4. private Context context;
  5. private Session session;
  6.  
  7. //Information to send email
  8. private String email;
  9. private String subject;
  10. private String message;
  11.  
  12. //Progressdialog to show while sending email
  13. private ProgressDialog progressDialog;
  14.  
  15. //Class Constructor
  16. public SendMail(Context context, String email, String subject, String message) {
  17. //Initializing variables
  18. this.context = context;
  19. this.email = email;
  20. this.subject = subject;
  21. this.message = message;
  22. }
  23.  
  24. @Override
  25. protected void onPreExecute() {
  26. super.onPreExecute();
  27. //Showing progress dialog while sending email
  28. progressDialog = ProgressDialog.show(context, "Sending message", "Please wait...", false, false);
  29. }
  30.  
  31. @Override
  32. protected void onPostExecute(Void aVoid) {
  33. super.onPostExecute(aVoid);
  34. //Dismissing the progress dialog
  35. progressDialog.dismiss();
  36. //Showing a success message
  37. Toast.makeText(context, "Message Sent", Toast.LENGTH_LONG).show();
  38. }
  39.  
  40. @Override
  41. protected Void doInBackground(Void... params) {
  42.  
  43. String prot = "smtp";
  44. final String user = Config.EMAIL;
  45. String to = Config.EMAIL;
  46. final String pass = Config.PASSWORD;
  47. String mailhost = "smtp-mail.outlook.com";
  48.  
  49. Properties props = System.getProperties();
  50. props.put("mail." + prot + ".host", mailhost);
  51. props.put("mail." + prot + ".auth", "true");
  52. props.put("mail." + prot + ".starttls.enable", "true");
  53. props.put("mail." + prot + ".port", "587");
  54. props.put("mail." + prot + ".ssl.enable", "false");
  55.  
  56. Session session = Session.getDefaultInstance(props,
  57. new javax.mail.Authenticator() {
  58. protected PasswordAuthentication getPasswordAuthentication() {
  59. return new PasswordAuthentication(user, pass);
  60. }
  61. });
  62. session.setDebug(true);
  63.  
  64. try {
  65. Message msg = new MimeMessage(session);
  66. msg.setFrom(new InternetAddress(user));
  67. msg.setRecipients(Message.RecipientType.TO,
  68. InternetAddress.parse(to, false));
  69.  
  70. msg.setSubject("TESTE ENVIO EMAIL");
  71.  
  72. msg.setText("Teste de envio de email conteudo");
  73.  
  74. msg.setHeader("X-Mailer", user);
  75. msg.setSentDate(new Date());
  76.  
  77. SMTPTransport t = (SMTPTransport) session.getTransport(prot);
  78.  
  79. t.connect(mailhost, user, pass);
  80.  
  81. t.sendMessage(msg, msg.getAllRecipients());
  82. t.close();
  83.  
  84. Log.d("------------", "done");
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. }
  88. return null;
  89. }
Add Comment
Please, Sign In to add comment