Advertisement
thieumao

Send email in Android

Jan 24th, 2017
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. package com.thieumao.mail;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Toast;
  7.  
  8. import android.app.Activity;
  9. import android.app.ProgressDialog;
  10. import android.content.Context;
  11. import android.os.AsyncTask;
  12. import android.os.Bundle;
  13. import android.view.View;
  14. import android.view.View.OnClickListener;
  15. import android.widget.Button;
  16. import android.widget.EditText;
  17. import android.widget.Toast;
  18.  
  19. import java.util.Properties;
  20.  
  21. import javax.mail.Authenticator;
  22. import javax.mail.Message;
  23. import javax.mail.MessagingException;
  24. import javax.mail.PasswordAuthentication;
  25. import javax.mail.Session;
  26. import javax.mail.Transport;
  27. import javax.mail.internet.InternetAddress;
  28. import javax.mail.internet.MimeMessage;
  29.  
  30. public class MainActivity extends AppCompatActivity {
  31.  
  32. Session session;
  33. ProgressDialog pdialog;
  34. Context context;
  35. EditText reciep, sub, msg;
  36. String rec, subject, textMessage;
  37.  
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. setContentView(R.layout.activity_main);
  42.  
  43. context = this;
  44. rec = "thieumao@gmail.com";
  45.  
  46. // Button login = (Button) findViewById(R.id.btn_submit);
  47. // reciep = (EditText) findViewById(R.id.et_to);
  48. // sub = (EditText) findViewById(R.id.et_sub);
  49. // msg = (EditText) findViewById(R.id.et_text);
  50. }
  51.  
  52. /** Called when the user touches the button */
  53. public void sendMessage(View view) {
  54. // Do something in response to button click
  55. // rec = reciep.getText().toString();
  56. // subject = sub.getText().toString();
  57. // textMessage = msg.getText().toString();
  58.  
  59. Properties props = new Properties();
  60. props.put("mail.smtp.host", "smtp.gmail.com");
  61. props.put("mail.smtp.socketFactory.port", "465");
  62. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  63. props.put("mail.smtp.auth", "true");
  64.  
  65. session = Session.getDefaultInstance(props, new Authenticator() {
  66. protected PasswordAuthentication getPasswordAuthentication() {
  67. return new PasswordAuthentication("tengido@gmail.com", "thieumao");
  68. }
  69. });
  70.  
  71. pdialog = ProgressDialog.show(context, "", "Sending Mail...", true);
  72.  
  73. RetreiveFeedTask task = new RetreiveFeedTask();
  74. task.execute();
  75. }
  76.  
  77. class RetreiveFeedTask extends AsyncTask<String, Void, String> {
  78. @Override
  79. protected String doInBackground(String... params) {
  80.  
  81. try{
  82. Message message = new MimeMessage(session);
  83. message.setFrom(new InternetAddress("leoski94@gmail.com"));
  84. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(rec));
  85. message.setSubject(subject);
  86. message.setContent(textMessage, "text/html; charset=utf-8");
  87. Transport.send(message);
  88. } catch(MessagingException e) {
  89. e.printStackTrace();
  90. } catch(Exception e) {
  91. e.printStackTrace();
  92. }
  93. return null;
  94. }
  95.  
  96. @Override
  97. protected void onPostExecute(String s) {
  98. // pdialog.dismiss();
  99. // reciep.setText("");
  100. // msg.setText("");
  101. // sub.setText("");
  102. // Toast.makeText(getApplicationContext(), "Message sent", Toast.LENGTH_LONG).show();
  103. }
  104. }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement