android中使用javamail发送邮件附件

2024-01-04 01:58

本文主要是介绍android中使用javamail发送邮件附件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用javamail必须先下载3个JAR包并导入工程 activation.jar   additonnal.jar   mail.jar导入方法为: project->properties->java build path->libraries->add external jars然后在android项目中添加网络访问权限<uses-permission android:name="android.permission.INTERNET"></uses-permission>最后在程序中加载如下包import android.app.Activity;
import android.os.Bundle;
import android.util.Log;import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.PasswordAuthentication;调用函数代码如下 class MyAuthenticatorextends javax.mail.Authenticator {private String strUser;private String strPwd;public MyAuthenticator(String user, String password) {this.strUser = user;this.strPwd = password;}protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(strUser, strPwd);}
}public void send_mail_file(String str_to_mail,String str_from_mail,String str_smtp,String str_user,String str_pass,String str_title,String str_body,String str_file_path){Log.v("lengfeng","send_mail_file");String host = str_smtp;   //发件人使用发邮件的电子信箱服务器String from = str_from_mail;    //发邮件的出发地(发件人的信箱)String to 	= str_to_mail;   //发邮件的目的地(收件人信箱)Log.v("lengfeng",str_smtp);Log.v("lengfeng",str_from_mail);Log.v("lengfeng",str_to_mail);Properties props = System.getProperties();// Get system propertiesprops.put("mail.smtp.host", host);// Setup mail serverprops.put("mail.smtp.auth", "true"); //这样才能通过验证MyAuthenticator myauth = new MyAuthenticator(str_user, str_pass);// Get sessionSession session = Session.getDefaultInstance(props, myauth);MimeMessage message = new MimeMessage(session); // Define messagetry {message.setFrom(new InternetAddress(from)); // Set the from address} catch (AddressException e) {e.printStackTrace();} catch (MessagingException e) {e.printStackTrace();}try {message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));// Set the to address} catch (AddressException e) {e.printStackTrace();} catch (MessagingException e) {e.printStackTrace();}try {message.setSubject(str_title);// Set the subject} catch (MessagingException e) {e.printStackTrace();}try {message.setText(str_body);// Set the content} catch (MessagingException e) {e.printStackTrace();}MimeBodyPart attachPart = new MimeBodyPart(); FileDataSource fds = new FileDataSource(str_file_path); //打开要发送的文件try {attachPart.setDataHandler(new DataHandler(fds));} catch (MessagingException e) {e.printStackTrace();} try {attachPart.setFileName(fds.getName());} catch (MessagingException e) {e.printStackTrace();} MimeMultipart allMultipart = new MimeMultipart("mixed"); //附件try {allMultipart.addBodyPart(attachPart);//添加} catch (MessagingException e) {e.printStackTrace();} try {message.setContent(allMultipart);} catch (MessagingException e) {e.printStackTrace();} try {message.saveChanges();} catch (MessagingException e) {e.printStackTrace();} try {Transport.send(message);//开始发送} catch (MessagingException e) {e.printStackTrace();}        } 

这篇关于android中使用javamail发送邮件附件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/567809

相关文章

使用Python开发一个Ditto剪贴板数据导出工具

《使用Python开发一个Ditto剪贴板数据导出工具》在日常工作中,我们经常需要处理大量的剪贴板数据,下面将介绍如何使用Python的wxPython库开发一个图形化工具,实现从Ditto数据库中读... 目录前言运行结果项目需求分析技术选型核心功能实现1. Ditto数据库结构分析2. 数据库自动定位3

Python yield与yield from的简单使用方式

《Pythonyield与yieldfrom的简单使用方式》生成器通过yield定义,可在处理I/O时暂停执行并返回部分结果,待其他任务完成后继续,yieldfrom用于将一个生成器的值传递给另一... 目录python yield与yield from的使用代码结构总结Python yield与yield

Go语言使用select监听多个channel的示例详解

《Go语言使用select监听多个channel的示例详解》本文将聚焦Go并发中的一个强力工具,select,这篇文章将通过实际案例学习如何优雅地监听多个Channel,实现多任务处理、超时控制和非阻... 目录一、前言:为什么要使用select二、实战目标三、案例代码:监听两个任务结果和超时四、运行示例五

python使用Akshare与Streamlit实现股票估值分析教程(图文代码)

《python使用Akshare与Streamlit实现股票估值分析教程(图文代码)》入职测试中的一道题,要求:从Akshare下载某一个股票近十年的财务报表包括,资产负债表,利润表,现金流量表,保存... 目录一、前言二、核心知识点梳理1、Akshare数据获取2、Pandas数据处理3、Matplotl

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入