Android 发送电子邮件范例

Android 发送电子邮件范例

1.首先需要3个类库文件

activation.jar
additionnal.jar
mail.jar

网上可以找到的哦,勤快点哈!

2.编写一个发送邮件的操作类 SendMail.java


package com.jinshainfo.bofengbluetoothtool;
//QQ 458978 代码参考网络资源修改,亲测好用,不明请联络我
import java.io.File;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
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;

public class SendMail {

    private Properties properties;
    private Session session;
    private Message message;
    private MimeMultipart multipart;

    public SendMail() {
        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");
    }

    /**
     * 设置收件人
     * 
     * @param receiver
     * @throws MessagingException
     */
    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);
    }

    /**
     * 设置邮件
     * 
     * @param from
     * @param title
     * @param content
     * @throws AddressException
     * @throws MessagingException
     */
    public void setMessage(String from, String title, String content) throws AddressException, MessagingException {

        this.message.setFrom(new InternetAddress(from));
        this.message.setSubject(title);
        // 纯文本的话用setText()就行,不过有附件就显示不出来内容了
        MimeBodyPart textBody = new MimeBodyPart();
        textBody.setContent(content, "text/html;charset=gbk");
        this.multipart.addBodyPart(textBody);
    }

    /**
     * 添加附件
     * 
     * @param filePath
     * @throws MessagingException
     */
    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);
    }

    /**
     * 发送邮件
     * 
     * @param host 地址
     * @param account 账户名
     * @param pwd 密码
     * @throws MessagingException
     */
    public void sendEmail(String host, String account, String pwd) throws MessagingException {

        // 发送时间
        this.message.setSentDate(new Date());

        // 发送的内容,文本和附件
        this.message.setContent(this.multipart);
        this.message.saveChanges();

        // 创建邮件发送对象,并指定其使用SMTP协议发送邮件
        Transport transport = session.getTransport("smtp");

        // 登录邮箱
        transport.connect(host, account, pwd);

        // 发送邮件
        transport.sendMessage(message, message.getAllRecipients());

        // 关闭连接
        transport.close();
    }

}


在界面调用类里面使用


<pre name="code" class="java">//QQ 458978 代码参考网络资源修改,亲测好用,不明请联络我
import com.jinshainfo.bofengbluetoothtool.SendMail;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity_About extends Activity {

	Button btnBack;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aboutactivity);

		btnBack = (Button) this.findViewById(R.id.button_back_s);
		btnBack.setOnClickListener(new ClickEvent());

	}

	// 按钮事件
	class ClickEvent implements View.OnClickListener {
		@Override
		public void onClick(View v) {
			if (v == btnBack) {
				new SVGAsync().execute("");//发送邮件
				closeAndExit();//退出界面
				return;
			}
		}

	}

	public void closeAndExit() {
		
		finish();
	}

	class SVGAsync extends AsyncTask<String, Integer, Object> {

		@Override
		protected void onPreExecute() {

			super.onPreExecute();
		}

		@Override
		protected Object doInBackground(String... params) {

			SendMail sender = new SendMail();
			// 设置服务器地址和端口
			
			sender.setProperties("smtp.qq.com", "465");// 465或者587
			try {
				sender.setMessage("458978@qq.com", "来自Andriod客户端的邮件",
						"系统执行了您预先设置的动作!!!  " + new java.util.Date().toString());
				// 设置收件人
				sender.setReceiver(new String[] { "458978@qq.com" });
				// 发送邮件
				sender.sendEmail("smtp.qq.com", "458978@qq.com",
						"发送邮箱密码");

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return null;
		}

		@Override
		protected void onPostExecute(Object result) {
		}

	}
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值