• android 发送email


    在android的手机上发email网上有很多都介绍的是:

    1             Intent emailIntent = new Intent(Intent.ACTION_SEND);
    2             String[] extra = new String[] { mailTo };
    3             emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    4             emailIntent.putExtra(Intent.EXTRA_TEXT, body);
    5             emailIntent.putExtra(Intent.EXTRA_EMAIL, extra);
    6             //emailIntent.setType("message/rfc822");
    7              emailIntent.setType("plain/text");
    8             Registry.context2.startActivity(Intent.createChooser(emailIntent, "请选择邮件发送软件"));

    后来发现这种方法在有的手机上可以,有的手机上没法用,

    第二种方法:

    使用:javamail-android,这是一个开源的项目我们可以利用此项目自己写一个发email的客户端,

    首先需要下载一下3个jar包:

     

    additionnal.jar

    mail.jar

    activation.jar

    http://code.google.com/p/javamail-android/downloads/list

      1 import javax.activation.DataHandler;
      2 import javax.activation.DataSource;
      3 import javax.mail.Message;
      4 import javax.mail.PasswordAuthentication;
      5 import javax.mail.Session;
      6 import javax.mail.Transport;
      7 import javax.mail.internet.InternetAddress;
      8 import javax.mail.internet.MimeMessage;
      9 import java.io.ByteArrayInputStream;
     10 import java.io.IOException;
     11 import java.io.InputStream;
     12 import java.io.OutputStream;
     13 import java.security.Security;
     14 import java.util.Properties;
     15 
     16 public class GMailSender extends javax.mail.Authenticator {
     17     private String mailhost = "smtp.163.com";
     18     private String user;
     19     private String password;
     20     private Session session;
     21 
     22     static {
     23         Security.addProvider(new JSSEProvider());
     24     }
     25 
     26     public GMailSender(String user, String password) {
     27         this.user = user;
     28         this.password = password;
     29 
     30         Properties props = new Properties();
     31         props.setProperty("mail.transport.protocol", "smtp");
     32         props.setProperty("mail.host", mailhost);
     33         props.put("mail.smtp.auth", "true");
     34         props.put("mail.smtp.port", "465");
     35         props.put("mail.smtp.socketFactory.port", "465");// 465
     36         props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
     37         props.put("mail.smtp.socketFactory.fallback", "false");
     38         props.setProperty("mail.smtp.quitwait", "false");
     39 
     40         session = Session.getDefaultInstance(props, this);
     41     }
     42 
     43     protected PasswordAuthentication getPasswordAuthentication() {
     44         return new PasswordAuthentication(user, password);
     45     }
     46 
     47     public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
     48         try {
     49             MimeMessage message = new MimeMessage(session);
     50             DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
     51             message.setSender(new InternetAddress(sender));
     52             message.setSubject(subject);
     53             message.setDataHandler(handler);
     54             if (recipients.indexOf(',') > 0)
     55                 message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
     56             else
     57                 message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
     58             Transport.send(message);
     59         } catch (Exception e) {
     60             System.out.println(e.getMessage());
     61         }
     62     }
     63 
     64     public class ByteArrayDataSource implements DataSource {
     65         private byte[] data;
     66         private String type;
     67 
     68         public ByteArrayDataSource(byte[] data, String type) {
     69             super();
     70             this.data = data;
     71             this.type = type;
     72         }
     73 
     74         public ByteArrayDataSource(byte[] data) {
     75             super();
     76             this.data = data;
     77         }
     78 
     79         public void setType(String type) {
     80             this.type = type;
     81         }
     82 
     83         public String getContentType() {
     84             if (type == null)
     85                 return "application/octet-stream";
     86             else
     87                 return type;
     88         }
     89 
     90         public InputStream getInputStream() throws IOException {
     91             return new ByteArrayInputStream(data);
     92         }
     93 
     94         public String getName() {
     95             return "ByteArrayDataSource";
     96         }
     97 
     98         public OutputStream getOutputStream() throws IOException {
     99             throw new IOException("Not Supported");
    100         }
    101     }
    102 }
     1 import java.security.AccessController;
     2 import java.security.Provider;
     3 
     4 public final class JSSEProvider extends Provider {
     5 
     6     public JSSEProvider() {
     7         super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
     8         AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
     9             public Void run() {
    10                 put("SSLContext.TLS",
    11                         "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
    12                 put("Alg.Alias.SSLContext.TLSv1", "TLS");
    13                 put("KeyManagerFactory.X509",
    14                         "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
    15                 put("TrustManagerFactory.X509",
    16                         "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
    17                 return null;
    18             }
    19         });
    20     }
    21 }
    1     GMailSender sender = new GMailSender("****@163.com", "****");
    2     sender.sendMail("标题", "内容", "****@163.com", "*****@163.com");
  • 相关阅读:
    Microsoft NLayerApp“.NET研究”案例理论与实践 项目简介与环境搭建 狼人:
    .NET中的“.NET研究”异步编程:使用F#简化异步编程 狼人:
    ASP.NET MV“.NET研究”C3 基础教程 – Web Pages 1.0 狼人:
    引用“.NET研究”类型赋值为null与加速垃圾回收 狼人:
    使用WCF实现SOA面向服务编程“.NET研究”—— 架构设计 狼人:
    MEF——.NET中值“.NET研究”得体验的精妙设计 狼人:
    Silverlight“.NET研究” 2.5D RPG游戏技巧与特效处理:(十)空间分层战斗系统 狼人:
    再次分享一个多选文件上传方案“.NET研究” 狼人:
    C#中标准Dis“.NET研究”pose模式的实现 狼人:
    .NET中的异步编程 IO完成端口以及FileStream.“.NET研究”BeginRead 狼人:
  • 原文地址:https://www.cnblogs.com/highfly2012/p/3152076.html
Copyright © 2020-2023  润新知