Guest User

Untitled

a guest
Aug 6th, 2018
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. How to send the email using Android Code [closed]
  2. Intent i = new Intent(Intent.ACTION_SEND);
  3. i.setType("text/plain");
  4. i.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient@example.com"});
  5. i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
  6. i.putExtra(Intent.EXTRA_TEXT , "body of email");
  7. try {
  8. startActivity(Intent.createChooser(i, "Send mail..."));
  9. } catch (android.content.ActivityNotFoundException ex) {
  10. Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
  11. }
  12.  
  13. Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
  14.  
  15. String[] recipients = new String[]{"my@email.com", "",};
  16.  
  17. emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
  18.  
  19. emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test");
  20.  
  21. emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is email's message");
  22.  
  23. emailIntent.setType("text/plain");
  24.  
  25. startActivity(Intent.createChooser(emailIntent, "Send mail..."));
  26.  
  27. String to = "abcd@gmail.com";
  28.  
  29. String from = "web@gmail.com";
  30.  
  31. Properties properties = System.getProperties();
  32.  
  33. properties.setProperty("mail.smtp.host", SMPT_HOSTNAME);
  34.  
  35. Session session = Session.getInstance(properties, new Authenticator() {
  36. @Override
  37. protected PasswordAuthentication getPasswordAuthentication() {
  38. return new PasswordAuthentication(USERNAME, PASSWORD);
  39. }
  40. });
  41.  
  42. try {
  43. MimeMessage message = new MimeMessage(session);
  44.  
  45. message.setFrom(new InternetAddress(from));
  46.  
  47. message.addRecipient(Message.RecipientType.TO, new InternetAddress(
  48. to));
  49.  
  50. message.setSubject("This is the Subject Line!");
  51.  
  52. message.setText("This is actual message");
  53.  
  54. Transport.send(message);
  55. System.out.println("Sent message successfully....");
  56. } catch (MessagingException mex) {
  57. mex.printStackTrace();
  58. }
Add Comment
Please, Sign In to add comment