大概弄懂了Java Mail的使用方法

今天花了一下午把Java Mail的API文档看完了,对java mail api有了一个大致的了解.
然后发现Jakarta的commons api更加简单,方便.
看一个最简单的commons api发邮件的例子:

SimpleEmail email = new SimpleEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe@somewhere.org", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("Test message");
email.setMsg("This is a simple test of commons-email");
email.send();

主页是这里:
http://jakarta.apache.org/commons/index.html

就这么几行,就搞定...
不过我还是决定学习使用Java mail,毕竟看了一下午时间...

简单笔记:

1.发邮件使用smtp(Simple Mail Transfer Protocol)协议
读邮件使用广泛使用的pop3(Post Office Protocol)协议

2.Java Mail里的几个类,我看文档时边看边记了几个:
Message:
抽象类,定义了一组一个邮件的属性和内容.

Folder:
Messages是存储在Folder对象中.Folder提供了fetch,appden,copy和delete消息的方法.

客户创造一个合适的Message子类.最后用Transport.send()发出消息

Session:
定义了客户和网络之间整体和个人邮件属性的接口.
final类,不能被继承.

Address:
邮件地址

BodyPart:
实现Part接口.

Multipart:
继承Message,是一个包含Bodypart的容器.

Flages:
说明在floder里面的message的状态.
answered:这个message已经被回答.
draft:这个message是草稿.
...

Strore:
定义了一个包含Folder的数据库
Folder里面是Message.

Folder:
可以包含subfolders.
store的getDefaultFolder()返回用户默认目录的根目录

The JavaBeans Activation Framework
决定传输数据的类型.

Setting Message Content
两种方法:
setDataHandler
setContent

Viewing a Message:
一般是3种: text/plain, text/html, multipart/mixed

Transport:
用来发送邮件,可以接受到成功或失败的事件.
session.getTransport(Address address);

session.getTransport(String protocol);
protocol写为:"SMTP"

3.然后嘛,就开始写个能够发邮件和收邮件的小程序热热身咯.

我是拿163,和126开刀的,它们的服务器域名是:
smtp.163.com
pop.126.com

smtp.126.com
pop.126.com

很快就写出了程序,程序的难点主要是密码认证那里,开始有点不知道怎么写.
认证代码是这样的:
// create some properties and get the default Session
   Properties props = new Properties();
   props.put("mail.smtp.host", "host");
   Session session = Session.getDefaultInstance(
     props,
     new Authenticator() {
       public PasswordAuthentication
          getPasswordAuthentication() {
            return new PasswordAuthentication("username", "password");
       }
   });
发给大部分邮件都没有问题,进自己的邮箱也能读.

程序输出:

EHLO eclipse
250-mail
250-PIPELINING
250-AUTH LOGIN PLAIN NTLM
250-AUTH=LOGIN PLAIN NTLM
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN NTLM"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN NTLM"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 dXNlcm5hbWU6
cmFuZ3dlaV81MjA=
334 UGFzc3dvcmQ6
NjI0NTk0Mg==
235 Authentication successful
DEBUG SMTP: use8bit false
MAIL FROM:<rangwei_520@126.com>
250 Mail OK
RCPT TO:<rangwei@gmail.com>
250 Mail OK
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   rangwei@gmail.com
DATA
354 End data with <CR><LF>.<CR><LF>
Message-ID: <367156.01136471509015.JavaMail.Rey@eclipse>
Date: Thu, 5 Jan 2006 22:31:48 +0800 (CST)
From: rangwei_520@126.com
To: rangwei@gmail.com
Subject: Just a test
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

I love you
.
250 Mail OK queued as smtp5,N8DrAagtvUMwBzQA.1486S2
QUIT

Mail was sent successfully.

S: +OK Welcome to coremail Mail Pop3 Server (126com[030901])
C: USER rangwei_520
S: +OK core mail
C: PASS 123456
S: +OK 1 message(s) [5932 byte(s)]
Name:      
Full Name:
URL:       pop3://rangwei_520@pop.126.com/
Is Directory

Name:      INBOX
Full Name: INBOX
URL:       pop3://rangwei_520@pop.126.com/INBOX
Total Messages:  -1
New Messages:    -1
Unread Messages: -1

C: QUIT
S: +OK core mail

呵呵,一个不错的开端.
只有一个小问题,收件箱里3种邮件数量显示的是-1,太累了,明天再看是怎么回事.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值