android 自动发电子邮件,android实现自动发送邮件

本文提供了一个Android应用程序的实例,展示了如何使用JavaMail API实现自动发送邮件的功能。邮件支持添加附件,并且详细解释了配置SMTP服务器、设置发件人、收件人、邮件主题、正文以及附件的方法。此外,还给出了MainActivity中的代码片段,演示了如何在Android环境中运行该功能。
摘要由CSDN通过智能技术生成

本文实例为大家分享了实现了一个android自动发送邮件的demo。支持163,qq邮箱

需要添加activation.jar,additionnal.jar和mail.jar这三个包

首先是一个EmailSender类

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 EmailSender {

private Properties properties;

private Session session;

private Message message;

private MimeMultipart multipart;

public EmailSender() {

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

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();

}

}

下面是mainactivity代码

import javax.mail.MessagingException;

import javax.mail.internet.AddressException;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MainActivity extends Activity {

private Button btnOK;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btnOK = (Button) findViewById(R.id.button);

btnOK.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

// sendEmail();

//耗时操作要起线程...有几个新手都是这个问题

new Thread(new Runnable() {

@Override

public void run() {

try {

EmailSender sender = new EmailSender();

//设置服务器地址和端口,网上搜的到

sender.setProperties("smtp.163.com", "25");

//分别设置发件人,邮件标题和文本内容

sender.setMessage("你的163邮箱账号", "EmailSender", "Java Mail !");

//设置收件人

sender.setReceiver(new String[]{"收件人邮箱"});

//添加附件

//这个附件的路径是我手机里的啊,要发你得换成你手机里正确的路径

// sender.addAttachment("/sdcard/DCIM/Camera/asd.jpg");

//发送邮件

sender.sendEmail("smtp.163.com", "你的163邮箱账号", "你的邮箱密码");//sender.setMessage("你的163邮箱账号", "EmailS//ender", "Java Mail !");这里面两个邮箱账号要一致

} catch (AddressException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (MessagingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}).start();

}

});

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值