福利!!在附件中赠送相关jar!!

1.导入相关jar:

调用邮件操作相关的jar如下(可在code.google.com上下载):

activation.jar

additionnal.jar

mail.jar

在需要导入的Android工程中属性页设置:

选择Add External Jar,并将三个jar包导入;

现在即可在代码调用相关API:

2.在项目中新建一个邮件发送的MailSender类:

代码:

import java.util.Properties;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.Address;import javax.mail.Authenticator;import javax.mail.MessagingException;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;import javax.mail.Message;public class MailSender extends Authenticator {    private Properties properties;    private Session session;    private MimeMultipart multipart;    private Message message;    public MailSender() {        super();        this.properties = new Properties();    }    public void setProperties(String host, String post) {        this.properties.put("mail.smtp.host", host);        this.properties.put("mail.smtp.post", post);        this.properties.put("mail.smtp.auth", true);        this.session = Session.getInstance(properties);        this.message = new MimeMessage(session);        this.multipart = new MimeMultipart("mixed");    }    public void setReceiver(String[] receiver) throws MessagingException {        Address[] address = new InternetAddress[receiver.length];        for (int i = 0; i < receiver.length; i++) {            address[i] = new InternetAddress(receiver[i]);        }        this.message.setRecipients(Message.RecipientType.TO, address);    }    public void setMessage(String from, String title, String content)            throws AddressException, MessagingException {        this.message.setFrom(new InternetAddress(from));        this.message.setSubject(title);        MimeBodyPart textBody = new MimeBodyPart();        textBody.setContent(content, "text/html;charset=gbk");        this.multipart.addBodyPart(textBody);    }    public void addAttachment(String filePath) throws MessagingException {        FileDataSource fileDataSource = new FileDataSource(new File(filePath));        DataHandler dataHandler = new DataHandler(fileDataSource);        MimeBodyPart mimeBodyPart = new MimeBodyPart();        mimeBodyPart.setDataHandler(dataHandler);        mimeBodyPart.setFileName(fileDataSource.getName());        this.multipart.addBodyPart(mimeBodyPart);    }    public void sendEmail(String host, String account, String pwd)            throws MessagingException {        this.message.setSentDate(new Date());        this.message.setContent(this.multipart);        this.message.saveChanges();        Transport transport = session.getTransport("smtp");        transport.connect(host, account, pwd);        transport.sendMessage(message, message.getAllRecipients());        transport.close();    }}


注意区别两个同名的类:javax.mail.Message和android.os.Message;

3.在主类中新建子线程调用MailSender类:

3.1 Activity主类中变量声明、参数设置:

private MailSender mailsender;private String str_SMTP = "smtp.126.com";private String str_SMTP_PORT = "587";private String str_USER_NAME = "xxx@126.com";private String str_PASSWORD = "******";private String str_MAILTITLE = "System.Log";private String str_TARGETMAIL = "yyy@yahoo.com";private String str_SMTP = "smtp.126.com";private String str_SMTP_PORT = "587";mailsender = new MailSender();mailsender.setProperties(str_SMTP, str_SMTP_PORT);

本例使用一个126的邮箱发到一个yahoo邮箱中;

3.2 写了个方法:

public void SendingTargetMail(String str_content) throws AddressException,MessagingException {mailsender.setMessage(str_USER_NAME, str_MAILTITLE, str_content);mailsender.setReceiver(new String[] { str_TARGETMAIL });mailsender.sendEmail(str_SMTP, str_USER_NAME, str_PASSWORD);


3.3 写个子线程调用(邮件正文遵循html语法):

Runnable Sward_runnable = new Runnable() {        public void run() {                            SendingTargetMail("<html><body>"   + "System time    :"+ getCurrentTime() + "<br>"                                                   + "</body><html>");             }    };

3.4 主线程启动子线程,后者一启动就发了个邮件:

Sward_Thread = new Thread(Sward_runnable);Sward_Thread.start();


3.5 Manifest文件中别忘了加上权限声明:

<uses-permission android:name="android.permission.INTERNET" />




更多相关文章

  1. Android(安卓)手把手教你写EventBus
  2. [置顶] android 多线程Thread,Runnable,Handler,AsyncTask等之间
  3. 【Android】主线程调用Http请求无效
  4. Android中隐式Intent的匹配规则
  5. Android消息传递机制Handler完全解析之1基础介绍
  6. Android使用http协议与服务器通信的实例
  7. Android(安卓)Studio如何设置自动import价包
  8. Android定时器实现方法
  9. Android(安卓)简单动画中SurfaceView 的应用

随机推荐

  1. cocos creator android studio多渠道打包
  2. android学习网站
  3. 如何让EditText不自动获取焦点
  4. Android开机自动启动程序设置
  5. android RadioGroup的使用
  6. edittext 随文字换行 而高度增加
  7. pytest-skip详解
  8. 2011.10.17——— android 多点触控
  9. 解决Android(安卓)Studio 和 Android(安
  10. android调用系统打电话功能