android实现邮件的发送

1.权限的声明:

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

2.导包(4jar):

 

 android实现邮件的发送_第1张图片

3.代码的写入:

import android.util.Log;

 

import org.apache.commons.mail.EmailAttachment;

import org.apache.commons.mail.EmailException;

import org.apache.commons.mail.HtmlEmail;

 

/**

 * Created by qinghua on 2016/9/24.

 */

public class sendMessage extends Thread {

    @Override

    public void run() {

        // TODO Auto-generated method stub

        super.run();

        try {

            EmailAttachment attach = new EmailAttachment();

            attach.setPath("/mnt/sdcard/data/user/0/example.com.zipfile/cache/zip"+"/test.zip");

            attach.setDisposition(EmailAttachment.ATTACHMENT);

            //创建HtmlEmail

            HtmlEmail email = new HtmlEmail();

            //填写邮件的主机明,我这里使用的是163

            email.setHostName("smtp.qq.com");

            email.setTLS(true);

            email.setSSL(true);

            //设置字符编码格式,防止中文乱码

            email.setCharset("gbk");

            //设置收件人的邮箱

            email.addTo("[email protected]");

            //设置发件人的邮箱

            email.setFrom("[email protected]");

            //填写发件人的用户名和密码

            email.setAuthentication("[email protected]", "gqhecmbshmlpbfaa");

            //填写邮件主题

            email.setSubject("您好");

 

 

            email.attach(attach);

 

            //填写邮件内容

            email.setMsg("qinghua" + "\n" + "make");

            //发送邮件

            email.send();

 

        } catch (EmailException e) {

            // TODO Auto-generated catch block

            Log.i("TAG", "---------------->"+e.getMessage());

        }

    }

}

你可能感兴趣的:(android实现邮件的发送)