通过SMTP(Gmail)在Android上发送电子邮件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过SMTP(Gmail)在Android上发送电子邮件相关的知识,希望对你有一定的参考价值。

  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. }

以上是关于通过SMTP(Gmail)在Android上发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

通过Python中的smtp.gmail.com在GAE上发送电子邮件

使用 codeigniter 通过 gmail smtp 服务器发送电子邮件

PHPMailer 使用 SMTP 身份验证发送电子邮件,但 Gmail 仍然声称它无法验证发件人

在 django 中发送邮件 - gmail smtp 问题

通过 GoDaddy 上的 gmail SMTP 发送电子邮件 [关闭]

通过 smtp.gmail.com 发送 php 邮件连接