Android中的后台邮件发送

一、调用邮件发送工具类进行邮件发送

  1. new Thread(){  
  2.         public void run() {  
  3.             try {  
  4.             GMailSender sender = new GMailSender(  
  5.                     "lixeeq19910119@gmail.com",  
  6.                     "ee768lxq");  
  7.                 sender.sendMail("xxxxx注册验证""验证码:"  
  8.                         + localNum,  
  9.                         "lixeeq19910119@gmail.com",  
  10.                         email);  
  11.             } catch (Exception e) {  
  12.                 e.printStackTrace();  
  13.             }  
  14.         };  
  15.     }.start();  

将上面的邮箱和密码换成你自己的邮箱和密码。

二、邮件发送工具类

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


三、Provider类 

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

关于Provider请参看: http://www.766.com/doc/java/security/class-use/Provider.html

四、需要的jar包

jar包下载地址:http://download.csdn.net/detail/lxq_xsyu/6543081

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值