Android 使用javaMail jar包发送邮件到指定邮箱,并可以发送图片附件
来源:互联网 发布:如何搜索二次元软件 编辑:程序博客网 时间:2024/06/05 09:44
Android发送邮件到指定邮箱一种是调用系统发邮件的软件,可以添加邮箱账号就可以发送邮件;第二种是使用javamail来发送邮件,使用javamail来发送邮件需要使用javaMail的jar包 mail.jar 、additionnal.jar 、activation.jar 这三个包 。关键代码如下:
import java.io.File;import java.util.Date;import java.util.Properties;import javax.activation.DataHandler;import javax.activation.FileDataSource;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;/** * Created by Ryan on 2017/12/10. */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{ javax.mail.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(); }}
import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import java.io.File;import javax.mail.MessagingException;import javax.mail.internet.AddressException;public class SendMailActivity extends Activity { private Button btnSend = null; private static final String TAG = "SendMailActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_send_mail); btnSend = (Button) findViewById(R.id.btnSend); btnSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //耗时操作要起线程...有几个新手都是这个问题 new Thread(new Runnable() { @Override public void run() { try { EmailSender sender = new EmailSender(); //设置服务器地址和端口,网上搜的到 sender.setProperties("smtp.163.com", "25"); //分别设置发件人,邮件标题和文本内容 sender.setMessage("15527927670@163.com", "This is title", "This is content!"); //设置收件人的邮箱 sender.setReceiver(new String[]{"RyanHuang323@163.com"}); File file = new File("/sdcard/DCIM/Camera/IMG_20150917_141427.jpg"); if(file.exists()) { Log.d(TAG,"file.exists()------>>>>>>"); //添加附件 //这个附件的路径是我手机里的啊,要发你得换成你手机里正确的路径 sender.addAttachment("/sdcard/DCIM/Camera/IMG_20150917_141427.jpg"); } else { Log.d(TAG,"file.notexists()------>>>>>>"); } //发送邮件,sender.setMessage("你的163邮箱账号", "EmailS//ender", "Java Mail !");这里面两个邮箱账号要一致 sender.sendEmail("smtp.163.com", "15527927670@163.com", "password"); } catch (AddressException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } }); }}
源码下载地址:https://github.com/Ryan-HANG/EMailSend.git
阅读全文
0 0
- Android 使用javaMail jar包发送邮件到指定邮箱,并可以发送图片附件
- Android 使用javaMail jar包 发送邮件
- android 后台发送邮件到指定邮箱
- android 调用系统邮件 发送邮件到指定邮箱
- 使用JavaMail进行简单SMTP邮件发送测试,从网易邮箱发送到QQ邮箱
- javamail发送邮件 jar冲突javaee包
- 使用javamail、阿里云邮箱发送邮件
- Android发送邮件到邮箱
- android自己获取并保存错误日志(可以通过邮件发送到自己邮箱)
- Android使用JavaMail 发送邮件
- android 使用JavaMail发送邮件
- Android发送邮件到指定邮箱(可带附件)
- Javamail发送邮件后并保存邮件到已发送
- javaMail实现邮件发送,用javamail.jar包
- 使用javamail实现发送邮件(QQ邮箱和163邮箱)
- 使用javaMail发送 邮件需要jar包mail.jar 和 activtion.jar
- 使用javamail包来发送邮件
- android javamail发送邮件zip压缩包在qq邮箱不显示后缀名问题
- 解决eclipse启动慢方法
- pygame的简介与安装
- Python3中的基本数据类型
- PCA与LDA
- 知识学习——jQuery文档操作
- Android 使用javaMail jar包发送邮件到指定邮箱,并可以发送图片附件
- 657. Judge Route Circle
- 17ANoldbach problem
- Python基础-高阶函数-Higher-order function
- 查找算法
- 省选专练APIO2009抢掠计划
- Unity3D实用脚本(二)—— 检查场景中用到的资源
- POJ 刷题系列:3006. Dirichlet's Theorem on Arithmetic Progressions
- 欢迎使用CSDN-markdown编辑器