下载oracle
javaMail API:

http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eeplat-419426.html#javamail-1.4.5-oth-JPR

下载:JavaMail
API 1.4.7

解压缩将lib文件夹中的jar文件全部拷贝到

zhousp@ubuntu:~/Documents/eclipsedoc/workspace/javamail/javamailApi目录下。

在Pachage
Explorer中,右击javamail,在Build
Path中选择Configure
Build Path

Add
External JARs

New-class

编写代码:

package
javamail;

import
java.util.Properties;

import
javax.mail.Message;

import
javax.mail.Session;

import
javax.mail.Transport;

import
javax.mail.internet.InternetAddress;

import
javax.mail.internet.MimeMessage;

publicclass
Sendmail {

/**

*
@param
args

*
@throws
Exception

*/

publicstaticvoid
main(String[] args)
throws
Exception {

Properties
prop
= new
Properties();

prop.setProperty("mail.host",
"smtp.126.com");

prop.setProperty("mail.transport.protocol",
"smtp");

prop.setProperty("mail.smtp.auth",
"true");

个步骤

//1、创建session

Session
session
= Session.getInstance(prop);

//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态

session.setDebug(true);

//2、通过session得到transport对象

Transport
ts
= session.getTransport();

//3、使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,用户名和密码都通过验证之后才能够正常发送邮件给收件人。

ts.connect("smtp.126.com",
"用户名",
"password");

//4、创建邮件

Message
message
= createSimpleMail(session);

//5、发送邮件

ts.sendMessage(message,
message.getAllRecipients());

ts.close();

}

publicstatic
MimeMessage createSimpleMail(Session session)

throws
Exception {

//创建邮件对象

MimeMessage
message
= new
MimeMessage(session);

//指明邮件的发件人

message.setFrom(new
InternetAddress("xxxx@126.com"));

//指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发

message.setRecipient(Message.RecipientType.TO,
new
InternetAddress("xxxx@126.com"));

//邮件的标题

message.setSubject("只包含文本的简单邮件");

//邮件的文本内容

message.setContent("你好啊!",
"text/html;charset=UTF-8");

//返回创建好的邮件对象

returnmessage;

}

}

Run As – Java
Application

DEBUG:
setDebug: JavaMail version 1.4.7

DEBUG:
getProvider() returning
javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]

DEBUG
SMTP: useEhlo true, useAuth true

DEBUG
SMTP: trying to connect to host "smtp.126.com", port 25,
isSSL false

220
126.com Anti-spam GT for Coremail System (126com[20140526])

DEBUG
SMTP: connected to host "smtp.126.com", port: 25

EHLO
ubuntu

250-mail

250-PIPELINING

250-AUTH
LOGIN PLAIN

250-AUTH=LOGIN
PLAIN

250-coremail
1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UrM5ErrUCa0xDrUUUUj

250-STARTTLS

250
8BITMIME

DEBUG
SMTP: Found extension "PIPELINING", arg ""

DEBUG
SMTP: Found extension "AUTH", arg "LOGIN PLAIN"

DEBUG
SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"

DEBUG
SMTP: Found extension "coremail", arg
"1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UrM5ErrUCa0xDrUUUUj"

DEBUG
SMTP: Found extension "STARTTLS", arg ""

DEBUG
SMTP: Found extension "8BITMIME", arg ""

DEBUG
SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN
DIGEST-MD5 NTLM

DEBUG
SMTP: AUTH LOGIN command trace suppressed

DEBUG
SMTP: AUTH LOGIN succeeded

DEBUG
SMTP: use8bit false

MAIL
FROM:<zhoushuopeng@126.com>

250
Mail OK

RCPT
TO:<zhoushuopeng@126.com>

250
Mail OK

DEBUG
SMTP: Verified Addresses

DEBUG
SMTP: zhoushuopeng@126.com

DATA

354
End data with <CR><LF>.<CR><LF>

From:
zhoushuopeng@126.com

To:
zhoushuopeng@126.com

Message-ID:
<526696804.0.1461464122621.JavaMail.zhousp@smtp.126.com>

Subject:
=?UTF-8?B?5Y+q5YyF5ZCr5paH5pys55qE566A5Y2V6YKu5Lu2?=

MIME-Version:
1.0

Content-Type:
text/html;charset=UTF-8

Content-Transfer-Encoding:
base64

5L2g5aW95ZWK77yB

.

250
Mail OK queued as smtp2,DMmowEDpxEI5LBxX388gAA--.16606S2 1461464122

QUIT

221
Bye

发送带附件的邮件:

package
javamail2;

import
java.io.FileOutputStream;

import
java.util.Properties;

import
javax.activation.DataHandler;

import
javax.activation.FileDataSource;

import
javax.mail.Message;

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;

publicclass
sendmail {

/**

*
@param
args

*
@throws
Exception

*/

publicstaticvoid
main(String[] args)
throws
Exception {

Properties
prop
= new
Properties();

prop.setProperty("mail.host",
"smtp.126.com");

prop.setProperty("mail.transport.protocol",
"smtp");

prop.setProperty("mail.smtp.auth",
"true");

个步骤

//1、创建session

Session
session
= Session.getInstance(prop);

//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态

session.setDebug(true);

//2、通过session得到transport对象

Transport
ts
= session.getTransport();

//3、连上邮件服务器

ts.connect("smtp.126.com",
"用户名",
"password");

//4、创建邮件

Message
message
= createAttachMail(session);

//5、发送邮件

ts.sendMessage(message,
message.getAllRecipients());

ts.close();

}

publicstatic
MimeMessage createAttachMail(Session session)
throws
Exception{

MimeMessage
message
= new
MimeMessage(session);

//设置邮件的基本信息

//发件人

message.setFrom(new
InternetAddress("xxxx@126.com"));

//收件人

message.setRecipient(Message.RecipientType.TO,
new
InternetAddress("zzzz@qq.com"));

//邮件标题

message.setSubject("JavaMail邮件发送测试");

//创建邮件正文,为了避免邮件正文中文乱码问题,需要使用charset=UTF-8指明字符编码

MimeBodyPart
text
= new
MimeBodyPart();

text.setContent("使用JavaMail创建的带附件的邮件",
"text/html;charset=UTF-8");

//创建邮件附件

MimeBodyPart
attach
= new
MimeBodyPart();

DataHandler
dh
= new
DataHandler(new
FileDataSource("src/1.jpg"));

attach.setDataHandler(dh);

attach.setFileName(dh.getName());
//

//创建容器描述数据关系

MimeMultipart
mp
= new
MimeMultipart();

mp.addBodyPart(text);

mp.addBodyPart(attach);

mp.setSubType("mixed");

message.setContent(mp);

message.saveChanges();

//将创建的Email写入到E盘存储

message.writeTo(new
FileOutputStream("src/attachMail.eml"));

//返回生成的邮件

returnmessage;

}

}

DEBUG:
setDebug: JavaMail version 1.4.7

DEBUG:
getProvider() returning
javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]

DEBUG
SMTP: useEhlo true, useAuth true

DEBUG
SMTP: trying to connect to host "smtp.126.com", port 25,
isSSL false

220
126.com Anti-spam GT for Coremail System (126com[20140526])

DEBUG
SMTP: connected to host "smtp.126.com", port: 25

EHLO
ubuntu

250-mail

250-PIPELINING

250-AUTH
LOGIN PLAIN

250-AUTH=LOGIN
PLAIN

250-coremail
1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UrXlMBNUCa0xDrUUUUj

250-STARTTLS

250
8BITMIME

DEBUG
SMTP: Found extension "PIPELINING", arg ""

DEBUG
SMTP: Found extension "AUTH", arg "LOGIN PLAIN"

DEBUG
SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"

DEBUG
SMTP: Found extension "coremail", arg
"1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UrXlMBNUCa0xDrUUUUj"

DEBUG
SMTP: Found extension "STARTTLS", arg ""

DEBUG
SMTP: Found extension "8BITMIME", arg ""

DEBUG
SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN
DIGEST-MD5 NTLM

DEBUG
SMTP: AUTH LOGIN command trace suppressed

DEBUG
SMTP: AUTH LOGIN succeeded

DEBUG
SMTP: use8bit false

MAIL
FROM:<zhoushuopeng@126.com>

250
Mail OK

RCPT
TO:<391902958@qq.com>

250
Mail OK

DEBUG
SMTP: Verified Addresses

DEBUG
SMTP: 391902958@qq.com

DATA

354
End data with <CR><LF>.<CR><LF>

From:
zhoushuopeng@126.com

To:
391902958@qq.com

Message-ID:
<2014432310.1.1461467389841.JavaMail.zhousp@smtp.126.com>

Subject:
=?UTF-8?Q?JavaMail=E9=82=AE=E4=BB=B6=E5=8F=91=E9=80=81=E6=B5=8B=E8=AF=95?=

MIME-Version:
1.0

Content-Type:
multipart/mixed;

boundary="----=_Part_0_86955769.1461467389824"

------=_Part_0_86955769.1461467389824

Content-Type:
text/html;charset=UTF-8

Content-Transfer-Encoding:
base64

5L2/55SoSmF2YU1haWzliJvlu7rnmoTluKbpmYTku7bnmoTpgq7ku7Y=

------=_Part_0_86955769.1461467389824

Content-Type:
application/octet-stream; name=1.jpg

Content-Transfer-Encoding:
base64

Content-Disposition:
attachment; filename=1.jpg

/9j/4Ra1RXhpZgAASUkqAAgAAAARAAABAwABAAAA0AwAAAEBAwABAAAAQBMAAAIBAwADAAAA2gAA

AAYBAwABAAAAAgAAAA8BAgASAAAA4AAAABABAgAJAAAA8gAAABIBAwABAAAAAQAAABUBAwABAAAA

….....

KT7T0fw/Gn+ry6+V/vH/AJmPmv8Aw9s7/wC76s9mW1f8k9f+aCf8dHUc3f8AuR/t/wDL1eX8bf8A

izYn/qBi/wChE9kt78T/AOrz6G1j/YD/AEo62mfgx/zK6j/5YD/oU+2LX+x/2vRgfhP29P8A3T/x

cJ/+Cy+z3ZuE/wDpT/k6Lt0+GH/V5dBhhP8Ai+bU/wCWqf8AEexUn/JPk/0v+ToMt/uVF9vR8No/

5+b/AILF/wBFe4t3j/cj8z1PPJ//ACTR9vQoeyTy6FnX/9k=

------=_Part_0_86955769.1461467389824--

.

250
Mail OK queued as smtp1,C8mowECZKEX2OBxX0n++AA--.33107S2 1461467417

QUIT

221
Bye

JavaMail API 1.4.7邮件发送的更多相关文章

  1. Android Java使用JavaMail API发送和接收邮件的代码示例

    JavaMail是Oracle甲骨文开发的Java邮件类API,支持多种邮件协议,这里我们就来看一下Java使用JavaMail API发送和接收邮件的代码示例 使用Javamail发送邮件,必需的j ...

  2. javamail模拟邮箱功能发送电子邮件-基础实战篇(javamail API电子邮件实例)

    引言: JavaMail 是一种可选的.能用于读取.编写和发送电子消息的包 JavaMail jar包下载地址:http://java.sun.com/products/javamail/downlo ...

  3. javamail模拟邮箱功能发送电子邮件-中级实战篇【新增附件发送方法】(javamail API电子邮件实例)

    引言: JavaMail jar包下载地址:http://java.sun.com/products/javamail/downloads/index.html 此篇是紧随上篇文章而封装出来的,阅读本 ...

  4. javamail模拟邮箱功能--邮件删除-中级实战篇【邮件标记方法】(javamail API电子邮件实例)

    前言: JavaMail jar包下载地址:http://java.sun.com/products/javamail/downloads/index.html 本章可能是讲解javamail的最后一 ...

  5. JavaMail如何保证邮件发送成功

    使用过JavaMail的api发送邮件的人可能会有这样一个疑惑:我如何知道我调用该api发送的邮件是否成功呢?一般的开放的api给我们调用都会有个返回值或者状态码,来告诉我们执行成功与否.但是Java ...

  6. JavaMail邮件发送不成功的那些坑人情况及分析说明

    [我的Segmentfault原文]https://segmentfault.com/a/1190000008030346 前言   JavaMail的使用本身并不难,网上有不少案例,简单易懂,而且有 ...

  7. 基于JavaMail的Java邮件发送:简单邮件发送

    使用Java应用程序发送 E-mail 十分简单,但是首先你应该在你的机器上安装 JavaMail API 和Java Activation Framework (JAF) . 您可以从 Java 网 ...

  8. javamail模拟邮箱功能--邮件回复-中级实战篇【邮件回复方法】(javamail API电子邮件实例)

    引言: JavaMai下载地址l jar包:http://java.sun.com/products/javamail/downloads/index.html 此篇是紧随上篇文章而封装出来的,阅读本 ...

  9. javamail模拟邮箱功能获取邮件内容-中级实战篇【内容|附件下载方法】(javamail API电子邮件实例)

    引言: JavaMail jar包下载地址:http://java.sun.com/products/javamail/downloads/index.html 此篇是紧随上篇文章而封装出来的,阅读本 ...

随机推荐

  1. 在ASP.NET MVC中对手机号码的验证

    在ASP.NET MVC中,可以使用RegularExpression特性来验证手机号码. public class Customer { [Required(ErrorMessage = " ...

  2. HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)

    Coconuts Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  3. 开发备必:WEB前端开发规范文档

    为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档. 本规范文档一经确认, 前端开发人员必 须按本文档规范进行前台页面开发. 本文档如有不对或者不合适的地 ...

  4. Linux学习笔记20——第一个多线程程序

    一 什么是线程 线程:是一个进程内部的一个控制序列. 二 使用POSIX的注意点 1 为了使用线程函数库,必须定义宏_REENTRANT,通过定义_REENTRANT来告诉编译器我们需要可重入功能,可 ...

  5. oracle自动编号

    oracle自动编号 在access中有自动编号的数据类型,MSSQL和MYSQL也都有自动增长的数据类型,插入记录时不用操作此字段,会自动获得数据值,而oracle没有自动增长的数据类型,我们需要建 ...

  6. C++Primer第5版学习笔记(一)

    C++Primer第5版学习笔记(一) 第一.二章的重难点内容        这个笔记本主要记录了我在学习C++Primer(第5版,中文版)的过程中遇到的重难点及其分析.因为第一.二章都比较简单,因 ...

  7. hdu1026 Ignatius and the Princess I (优先队列 BFS)

    Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...

  8. poj 1328贪心

    Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...

  9. C语言学习_C如何在一个文件里调用另一个源文件中的函数

    问题 C如何在一个文件里调用另一个源文件中的函数,如题. 解决办法 当程序大了代码多了之后,想模块化开发,不同文件中存一点,是很好的解决办法,那我们如何做才能让各个文件中的代码协同工作呢?我们知道,m ...

  10. 【转】Android通过Wifi来调试你的应用

    http://stormzhang.com/android/2014/08/27/adb-over-wifi/ 在Android中调试我们经常要用一根USB数据线连接到手机和电脑,一方面麻烦不说,手机 ...