Android编程 – 发送邮件

前端之家收集整理的这篇文章主要介绍了Android编程 – 发送邮件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在 Android中使用以下代码发送邮件
  1. Intent emailIntent = new Intent(Intent.ACTION_SEND);
  2. emailIntent.setType("text/html");
  3. emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,sendTo );
  4.  
  5. emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"test" );
  6. emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"msg" );

当编译和运行代码时,它会询问我选择“GMail”,“BlueTooth”等应用程序的选项.但我希望在没有用户干预的情况下发送邮件.即使通过彩信发送也会对我有好处.有人可以建议我怎么做吗?

解决方法

您需要在项目中包含java邮件库和依赖项,然后编写一个类似下面的帮助程序类.
  1. public class GMailSender extends javax.mail.Authenticator {
  2. private String mailhost = "smtp.gmail.com";
  3. private String user;
  4. private String password;
  5. private Session session;
  6.  
  7. static {
  8. Security.addProvider(new JSSEProvider());
  9. }
  10.  
  11. public GMailSender(String user,String password) {
  12. this.user = user;
  13. this.password = password;
  14.  
  15. Properties props = new Properties();
  16. props.setProperty("mail.transport.protocol","smtp");
  17. props.setProperty("mail.host",mailhost);
  18. props.put("mail.smtp.auth","true");
  19. props.put("mail.smtp.port","465");
  20. props.put("mail.smtp.socketFactory.port","465");
  21. props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
  22. props.put("mail.smtp.socketFactory.fallback","false");
  23. props.setProperty("mail.smtp.quitwait","false");
  24.  
  25. session = Session.getDefaultInstance(props,this);
  26. }
  27.  
  28. protected PasswordAuthentication getPasswordAuthentication() {
  29. return new PasswordAuthentication(user,password);
  30. }
  31.  
  32. public synchronized void sendMail(String subject,String body,String sender,String recipients) throws Exception {
  33. MimeMessage message = new MimeMessage(session);
  34. DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(),"text/plain"));
  35. message.setSender(new InternetAddress(sender));
  36. message.setSubject(subject);
  37. message.setDataHandler(handler);
  38. if (recipients.indexOf(',') > 0)
  39. message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(recipients));
  40. else
  41. message.setRecipient(Message.RecipientType.TO,new InternetAddress(recipients));
  42. Transport.send(message);
  43. }
  44.  
  45. public class ByteArrayDataSource implements DataSource {
  46. private byte[] data;
  47. private String type;
  48.  
  49. public ByteArrayDataSource(byte[] data,String type) {
  50. super();
  51. this.data = data;
  52. this.type = type;
  53. }
  54.  
  55. public ByteArrayDataSource(byte[] data) {
  56. super();
  57. this.data = data;
  58. }
  59.  
  60. public void setType(String type) {
  61. this.type = type;
  62. }
  63.  
  64. public String getContentType() {
  65. if (type == null)
  66. return "application/octet-stream";
  67. else
  68. return type;
  69. }
  70.  
  71. public InputStream getInputStream() throws IOException {
  72. return new ByteArrayInputStream(data);
  73. }
  74.  
  75. public String getName() {
  76. return "ByteArrayDataSource";
  77. }
  78.  
  79. public OutputStream getOutputStream() throws IOException {
  80. throw new IOException("Not Supported");
  81. }
  82. }
  83. }

JSSEProvider类

  1. @SuppressWarnings("serial")
  2. public final class JSSEProvider extends Provider {
  3. public JSSEProvider() {
  4. super("HarmonyJSSE",1.0,"Harmony JSSE Provider");
  5. AccessController.doPrivileged(new java.security.PrivilegedAction() {
  6. public Void run() {
  7. put("SSLContext.TLS","org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
  8. put("Alg.Alias.SSLContext.TLSv1","TLS");
  9. put("KeyManagerFactory.X509","org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
  10. put("TrustManagerFactory.X509","org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
  11. return null;
  12. }
  13. });
  14. }
  15. }

然后发送电子邮件就像这样简单

  1. GMailSender sender = new GMailSender("username","password");
  2. sender.sendMail("subject",body,"sender",recipients);

猜你在找的Android相关文章