Sending Email On Android By SMTP (Gmail)


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package org.apache.android.mail;
  2.  
  3. import javax.activation.DataHandler;
  4. import javax.activation.DataSource;
  5. import javax.mail.Message;
  6. import javax.mail.PasswordAuthentication;
  7. import javax.mail.Session;
  8. import javax.mail.Transport;
  9. import javax.mail.internet.InternetAddress;
  10. import javax.mail.internet.MimeMessage;
  11. import java.io.ByteArrayInputStream;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.io.OutputStream;
  15. import java.security.Security;
  16. import java.util.Properties;
  17.  
  18. public class GMailSender extends javax.mail.Authenticator {
  19. private String mailhost = "smtp.gmail.com";
  20. private String user;
  21. private String password;
  22. private Session session;
  23.  
  24. static {
  25. Security.addProvider(new org.apache.harmony.xnet.provider.jsse.JSSEProvider());
  26. }
  27.  
  28. public GMailSender(String user, String password) {
  29. this.user = user;
  30. this.password = password;
  31.  
  32. Properties props = new Properties();
  33. props.setProperty("mail.transport.protocol", "smtp");
  34. props.setProperty("mail.host", mailhost);
  35. props.put("mail.smtp.auth", "true");
  36. props.put("mail.smtp.port", "465");
  37. props.put("mail.smtp.socketFactory.port", "465");
  38. props.put("mail.smtp.socketFactory.class",
  39. "javax.net.ssl.SSLSocketFactory");
  40. props.put("mail.smtp.socketFactory.fallback", "false");
  41. props.setProperty("mail.smtp.quitwait", "false");
  42.  
  43. session = Session.getDefaultInstance(props, this);
  44. }
  45.  
  46. protected PasswordAuthentication getPasswordAuthentication() {
  47. return new PasswordAuthentication(user, password);
  48. }
  49.  
  50. public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
  51. MimeMessage message = new MimeMessage(session);
  52. DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
  53. message.setSender(new InternetAddress(sender));
  54. message.setSubject(subject);
  55. message.setDataHandler(handler);
  56. if (recipients.indexOf(',') > 0)
  57. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
  58. else
  59. message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
  60. Transport.send(message);
  61. }
  62.  
  63. public class ByteArrayDataSource implements DataSource {
  64. private byte[] data;
  65. private String type;
  66.  
  67. public ByteArrayDataSource(byte[] data, String type) {
  68. super();
  69. this.data = data;
  70. this.type = type;
  71. }
  72.  
  73. public ByteArrayDataSource(byte[] data) {
  74. super();
  75. this.data = data;
  76. }
  77.  
  78. public void setType(String type) {
  79. this.type = type;
  80. }
  81.  
  82. public String getContentType() {
  83. if (type == null)
  84. return "application/octet-stream";
  85. else
  86. return type;
  87. }
  88.  
  89. public InputStream getInputStream() throws IOException {
  90. return new ByteArrayInputStream(data);
  91. }
  92.  
  93. public String getName() {
  94. return "ByteArrayDataSource";
  95. }
  96.  
  97. public OutputStream getOutputStream() throws IOException {
  98. throw new IOException("Not Supported");
  99. }
  100. }
  101. }

URL: http://davanum.wordpress.com/2007/12/22/android-send-email-via-gmail-actually-via-smtp/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.