你的位置:首页 > Java教程

[Java教程]JavaMail API 1.4.7邮件发送



下载oraclejavaMail API:

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



下载:JavaMailAPI 1.4.7

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

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

在PachageExplorer中,右击javamail,在BuildPath中选择ConfigureBuild Path

packagejavamail;


importjava.util.Properties;

importjavax.mail.Message;

importjavax.mail.Session;

importjavax.mail.Transport;

importjavax.mail.internet.InternetAddress;

importjavax.mail.internet.MimeMessage;



publicclassSendmail {


/**

*@paramargs

*@throwsException

*/

publicstaticvoidmain(String[] args)throwsException {

 

Propertiesprop= newProperties();

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

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

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

//使用JavaMail发送邮件的5个步骤

//1、创建session

Sessionsession= Session.getInstance(prop);

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

session.setDebug(true);

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

Transportts= session.getTransport();

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

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

//4、创建邮件

Messagemessage= createSimpleMail(session);

//5、发送邮件

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

ts.close();

}

 


publicstaticMimeMessage createSimpleMail(Session session)

throwsException {

//创建邮件对象

MimeMessagemessage= newMimeMessage(session);

//指明邮件的发件人

message.setFrom(newInternetAddress("xxxx@126.com"));

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

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

//邮件的标题

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

//邮件的文本内容

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

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

returnmessage;

}

}



Run As – JavaApplication


DEBUG:setDebug: JavaMail version 1.4.7

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

DEBUGSMTP: useEhlo true, useAuth true

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

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

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


EHLOubuntu

250-mail

250-PIPELINING

250-AUTHLOGIN PLAIN

250-AUTH=LOGINPLAIN

250-coremail1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UrM5ErrUCa0xDrUUUUj

250-STARTTLS

2508BITMIME

DEBUGSMTP: Found extension "PIPELINING", arg ""

DEBUGSMTP: Found extension "AUTH", arg "LOGIN PLAIN"

DEBUGSMTP: Found extension "AUTH=LOGIN", arg "PLAIN"

DEBUGSMTP: Found extension "coremail", arg"1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UrM5ErrUCa0xDrUUUUj"

DEBUGSMTP: Found extension "STARTTLS", arg ""

DEBUGSMTP: Found extension "8BITMIME", arg ""

DEBUGSMTP: Attempt to authenticate using mechanisms: LOGIN PLAINDIGEST-MD5 NTLM

DEBUGSMTP: AUTH LOGIN command trace suppressed

DEBUGSMTP: AUTH LOGIN succeeded

DEBUGSMTP: use8bit false

MAILFROM:<zhoushuopeng@126.com>

250Mail OK

RCPTTO:<zhoushuopeng@126.com>

250Mail OK

DEBUGSMTP: Verified Addresses

DEBUGSMTP: zhoushuopeng@126.com

DATA

354End 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

.

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

QUIT

221Bye



发送带附件的邮件:


packagejavamail2;



importjava.io.FileOutputStream;

importjava.util.Properties;


importjavax.activation.DataHandler;

importjavax.activation.FileDataSource;

importjavax.mail.Message;

importjavax.mail.Session;

importjavax.mail.Transport;

importjavax.mail.internet.InternetAddress;

importjavax.mail.internet.MimeBodyPart;

importjavax.mail.internet.MimeMessage;

importjavax.mail.internet.MimeMultipart;



publicclasssendmail {


/**

*@paramargs

*@throwsException

*/

publicstaticvoidmain(String[] args)throwsException {

 

Propertiesprop= newProperties();

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

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

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

//使用JavaMail发送邮件的5个步骤

//1、创建session

Sessionsession= Session.getInstance(prop);

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

session.setDebug(true);

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

Transportts= session.getTransport();

//3、连上邮件服务器

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

//4、创建邮件

Messagemessage= createAttachMail(session);

//5、发送邮件

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

ts.close();

}

 


publicstaticMimeMessage createAttachMail(Session session)throwsException{

MimeMessagemessage= newMimeMessage(session);

 

//设置邮件的基本信息

//发件人

message.setFrom(newInternetAddress("xxxx@126.com"));

//收件人

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

//邮件标题

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

 

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

MimeBodyParttext= newMimeBodyPart();

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

 

//创建邮件附件

MimeBodyPartattach= newMimeBodyPart();

DataHandlerdh= newDataHandler(newFileDataSource("src/1.jpg"));

attach.setDataHandler(dh);

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

 

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

MimeMultipartmp= newMimeMultipart();

mp.addBodyPart(text);

mp.addBodyPart(attach);

mp.setSubType("mixed");

 

message.setContent(mp);

message.saveChanges();

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

message.writeTo(newFileOutputStream("src/attachMail.eml"));

//返回生成的邮件

returnmessage;

}

}



DEBUG:setDebug: JavaMail version 1.4.7

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

DEBUGSMTP: useEhlo true, useAuth true

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

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

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


EHLOubuntu

250-mail

250-PIPELINING

250-AUTHLOGIN PLAIN

250-AUTH=LOGINPLAIN

250-coremail1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UrXlMBNUCa0xDrUUUUj

250-STARTTLS

2508BITMIME

DEBUGSMTP: Found extension "PIPELINING", arg ""

DEBUGSMTP: Found extension "AUTH", arg "LOGIN PLAIN"

DEBUGSMTP: Found extension "AUTH=LOGIN", arg "PLAIN"

DEBUGSMTP: Found extension "coremail", arg"1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Uj8Cz28x1UUUUU7Ic2I0Y2UrXlMBNUCa0xDrUUUUj"

DEBUGSMTP: Found extension "STARTTLS", arg ""

DEBUGSMTP: Found extension "8BITMIME", arg ""

DEBUGSMTP: Attempt to authenticate using mechanisms: LOGIN PLAINDIGEST-MD5 NTLM

DEBUGSMTP: AUTH LOGIN command trace suppressed

DEBUGSMTP: AUTH LOGIN succeeded

DEBUGSMTP: use8bit false

MAILFROM:<zhoushuopeng@126.com>

250Mail OK

RCPTTO:<391902958@qq.com>

250Mail OK

DEBUGSMTP: Verified Addresses

DEBUGSMTP: 391902958@qq.com

DATA

354End 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--

.

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

QUIT

221Bye