Guest User

Untitled

a guest
Nov 7th, 2017
965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto","correo@gmail.com", null));
  2. emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Android APP - ");
  3. startActivity(Intent.createChooser(emailIntent, getActivity().getString(R.string.enviar_mail)));
  4.  
  5. String[] TO = {""}; //Direcciones email a enviar.
  6. String[] CC = {""}; //Direcciones email con copia.
  7.  
  8. Intent emailIntent = new Intent(Intent.ACTION_SEND);
  9.  
  10. emailIntent.setData(Uri.parse("mailto:"));
  11. emailIntent.setType("text/plain");
  12. emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
  13. emailIntent.putExtra(Intent.EXTRA_CC, CC);
  14. emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Tu Asunto...");
  15. emailIntent.putExtra(Intent.EXTRA_TEXT, "tuemail@email.com"); // * configurar email aquí!
  16.  
  17. try {
  18. startActivity(Intent.createChooser(emailIntent, "Enviar email."));
  19. Log.i("EMAIL", "Enviando email...");
  20. }
  21. catch (android.content.ActivityNotFoundException e) {
  22. Toast.makeText(this, "NO existe ningún cliente de email instalado!.", Toast.LENGTH_SHORT).show();
  23. }
  24.  
  25. repositories {
  26. jcenter()
  27. maven {
  28. url "https://maven.java.net/content/groups/public/"
  29. }
  30. }
  31.  
  32. dependencies {
  33. compile 'com.sun.mail:android-mail:1.5.5'
  34. compile 'com.sun.mail:android-activation:1.5.5'
  35. }
  36.  
  37. /**
  38. * Created by snolde on 06-04-2017.
  39. */
  40.  
  41. public class MailJob extends AsyncTask<MailJob.Mail,Void,Void>{
  42. private final String user;
  43. private final String pass;
  44.  
  45. public MailJob(String user, String pass) {
  46. super();
  47. this.user=user;
  48. this.pass=pass;
  49. }
  50.  
  51. @Override
  52. protected Void doInBackground(Mail... mails) {
  53. Properties props = new Properties();
  54. props.put("mail.smtp.auth", "true");
  55. props.put("mail.smtp.starttls.enable", "true");
  56. props.put("mail.smtp.host", "smtp.gmail.com");
  57. props.put("mail.smtp.port", "587");
  58.  
  59. Session session = Session.getInstance(props,
  60. new javax.mail.Authenticator() {
  61. protected PasswordAuthentication getPasswordAuthentication() {
  62. return new PasswordAuthentication(user, pass);
  63. }
  64. });
  65. for (Mail mail:mails) {
  66.  
  67. try {
  68.  
  69. Message message = new MimeMessage(session);
  70. message.setFrom(new InternetAddress(mail.from));
  71. message.setRecipients(Message.RecipientType.TO,
  72. InternetAddress.parse(mail.to));
  73. message.setSubject(mail.subject);
  74. message.setText(mail.content);
  75.  
  76. Transport.send(message);
  77.  
  78. } catch (MessagingException e) {
  79. Log.d("MailJob", e.getMessage());
  80. }
  81. }
  82. return null;
  83. }
  84.  
  85. public static class Mail{
  86. private final String subject;
  87. private final String content;
  88. private final String from;
  89. private final String to;
  90.  
  91. public Mail(String from, String to, String subject, String content){
  92. this.subject=subject;
  93. this.content=content;
  94. this.from=from;
  95. this.to=to;
  96. }
  97. }
  98. }
  99.  
  100. new MailJob(user, passwd).execute(
  101. new MailJob.Mail("from@gmail.com", "to@otromail.com", "subjeto", "contenido")
  102. );
Add Comment
Please, Sign In to add comment