准备好工作了-下载最新的版本号JMail

https://java.net/projects/javamail/pages/Home#Download_JavaMail_1.5.2_Release

http://www.oracle.com/technetwork/java/javase/downloads/index-135046.html

在android上发送邮件方式:

第一种:借助GMail APPclient。缺点是必须使用GMail帐号,有点是比較方便

不须要写非常多代码。可是不是非常灵活。

另外一种:基于JMail实现。能够非常灵活的自己设置各种属性。不须要GMail帐号

在另外一种方式的实现之前。看一下JMail对EMail结构的划分:

基于SMTP协议发送EMail,所以client必须要知道SMTP的主机

腾讯邮件的SMTP主机为:stmp.qq.com端口为465基于SSL协议

最后我做了一个简单的封装,把发送文本加图像附件的功能做出了

一个单独的Class。仅仅要调用一下就可以完毕:

package com.gloomyfish.jmail.demo;

import java.util.Date;
import java.util.Properties; import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart; public class EMailSender { private String host;
private String port;
private String userName;
private String password;
private String[] images; public String[] getImagePath() {
return images;
} public void setImagePath(String[] imagePath) {
this.images = imagePath;
} public EMailSender(String host, String port, String userName, String password)
{
this.host = host;
this.port = port;
this.userName = userName;
this.password = password;
} public void sendEmail(String subject, String recepits, String sender, String content)
{
Properties props = new Properties();
props.put("mail.smtp.host", host); //设置smtp的server地址
// props.put("mail.smtp.starttls.enable", "true");
// props.put("mail.smtp.port", port); // 设置端口
// props.put("mail.smtp.auth", "true"); //设置smtpserver要身份验证。 props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", port); // 返回授权Base64编码
PopupAuthenticator auth = new PopupAuthenticator(userName, password);
// 获取会话对象
Session session = Session.getInstance(props, auth);
// 设置为DEBUG模式
session.setDebug(true); // 邮件内容对象组装
MimeMessage message = new MimeMessage(session);
try
{
Address addressFrom = new InternetAddress(sender, "Jia Zhi Gang");
Address addressTo = new InternetAddress(recepits, "My QQ E-Mail");
message.setSubject(subject);
message.setSentDate(new Date());
message.setFrom(addressFrom);
message.addRecipient(Message.RecipientType.TO,addressTo); // 邮件文本/HTML内容
Multipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(content, "text/html");
multipart.addBodyPart(messageBodyPart); // 加入邮件附件
if (images != null && images.length > 0) {
for (String filePath : images) {
MimeBodyPart attachPart = new MimeBodyPart();
DataSource source = new FileDataSource(filePath);
attachPart.setDataHandler(new DataHandler(source));
attachPart.setFileName(filePath);
multipart.addBodyPart(attachPart);
}
} // 保存邮件内容
message.setContent(multipart); // 获取SMTP协议client对象,连接到指定SMPTserver
Transport transport = session.getTransport("smtp");
transport.connect(host, Integer.parseInt(port), userName, password);
System.out.println("connet it success!!!!"); // 发送邮件到SMTPserver
Thread.currentThread().setContextClassLoader( getClass().getClassLoader() );
Transport.send(message);
System.out.println("send it success!!!!"); // 关闭连接
transport.close();
}
catch(Exception e)
{
e.printStackTrace();
}
} public String getHost() {
return host;
} public void setHost(String host) {
this.host = host;
} public String getPort() {
return port;
} public void setPort(String port) {
this.port = port;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }

用户授权类:

package com.gloomyfish.jmail.demo;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication; class PopupAuthenticator extends Authenticator {
private String userName;
private String password;
public PopupAuthenticator(String userName, String password)
{
this.userName = userName;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
}

特别注意:

1.在android上发送邮件必须自己导入三个相关的JAVA文件

上述JAR下载的文件已经在文章的开头给定!

Android在发送带有附件的邮件的更多相关文章

  1. Java Mail 发送带有附件的邮件

    1.小编用的是163邮箱发送邮件,所以要先登录163邮箱开启POP3/SMTP/IMAP服务方法: 2.下载所需的java-mail 包 https://maven.java.net/content/ ...

  2. spring boot:发送带附件的邮件和html内容的邮件(以163.com邮箱为例/spring boot 2.3.2)

    一,网站哪些情况下需要发送电子邮件? 作为一个电商网站,以下情况需要发邮件通知用户: 注册成功的信息 用邮箱接收验证码 找回密码时发链接 发送推广邮件 下单成功后的订单通知 给商户的对账单邮件 说明: ...

  3. [Xcode 实际操作]八、网络与多线程-(7)使用MessageUI框架,创建并发送一封带有附件的邮件

    目录:[Swift]Xcode实际操作 本文将演示如何使用MessageUI框架,创建并发送一封带有附件的邮件. 使用邮件编辑视图控制器(MFMailComposeViewController)实现邮 ...

  4. 【Mail】JavaMail发送带附件的邮件(二)

    上一篇讲了使用JavaMail发送普通邮件([Mail]JavaMail介绍及发送邮件(一)),本例讲发送复杂的邮件(带有附件的邮件) 生成一封复杂的邮件 新建一个JavaWeb的Maven工程,引入 ...

  5. java发送带附件的邮件

    /** * java发送带附件的邮件 * 周枫 * 2013.8.10 */ package com.dsideal.Util; import javax.mail.*; import javax.m ...

  6. C#发送带附件的邮件的代码

    如下的代码是关于C#发送带附件的邮件的代码. MailMessage m = new MailMessage();m.Subject = "File attachment!";m. ...

  7. 利用Python+163邮箱授权码发送带附件的邮件

    背景 前段时间写了个自动爬虫的脚本,定时在阿里云服务器上执行,会从某个网站上爬取链接保存到txt文本中,但是脚本不够完善,我需要爬虫完毕之后通过邮件把附件给我发送过来,之前写过一个<利用Pyth ...

  8. [SpringBoot] - 发送带附件的邮件

    <!--发送email依赖--> <dependency> <groupId>org.springframework.boot</groupId> &l ...

  9. 接口测试基础——第2篇smtplib发送带附件的邮件

    我先给大家补充一个用QQ发送纯文本电子邮件的代码,用QQ的朋友可以参考一下: # coding=utf-8 import smtplib from email.mime.text import MIM ...

随机推荐

  1. 窗口显示于parent控件上(用到了ManualDock函数)

    procedure TForm1.btn1Click(Sender: TObject); begin with TForm2.Create(self) do begin ManualDock(self ...

  2. OCA读书笔记(12) - 数据库维护

    查询优化器统计信息 搜集统计信息: 不是实时的: SQL> conn /as sysdbaConnected.SQL> grant select on dba_objects to sco ...

  3. Android笔记二十七.Service组件入门(一).什么是Service?

    转载请表明出处:http://blog.csdn.net/u012637501(嵌入式_小J的天空) 一.Service 1.Service简单介绍     Service为Android四大组件之中 ...

  4. 实现ios常见菜单效果的思路

    眼下见过的实现边側菜单的效果.比較流行的有下面三种:(效果图) 1.菜单条覆盖在部分主视图上 附上实现该效果的一个不错的源代码地址: http://code4app.com/ios/RNFrosted ...

  5. 关于matlab矩阵卷积conv2和傅里叶变换求卷积ifft2的关系

    先定义两个矩阵 a = [1 2 3 5 ; 4 7 9 5;1 4 6 7;5 4 3 7;8 7 5 1] %a矩阵取5*4 b = [1 5 4; 3 6 8; 1 5 7]   %b矩阵如多数 ...

  6. 关于CodeReview(java)(转)

    关于codereview,在平时的开发中,经常忽略的环节,参照目前介绍写好代码的几本书和之前掉进的坑,做了一个总结,分享出来. 为什么要做 通过review规避一些代码层面的问题 提升可读性,方便后续 ...

  7. poj3259(spfa判负环)

    题目连接:http://poj.org/problem?id=3259 题意:John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时 ...

  8. JavaScript模板引擎

    JavaScript模板引擎实例应用   在之前的一篇名为<移动端基于HTML模板和JSON数据的JavaScript交互>的文章中,我向大家说明了为什么要使用JavaScript模板以及 ...

  9. dell N5010

    Inspiron N5010Microsoft Windows 10 企业版 (64位) (英特尔)Intel(R) Core(TM) i3 CPU       M 370  @ 2.40GHz(24 ...

  10. 数学之路-python计算实战(19)-机器视觉-卷积滤波

    filter2D Convolves an image with the kernel. C++: void filter2D(InputArray src, OutputArray dst, int ...