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");
原文地址:https://www.cnblogs.com/highfly2012/p/3152076.html