?
//SendMail.java
import
?javax.mail.
*
;
import
?javax.mail.internet.
*
;
import
?java.util.
*
;
import
?javax.activation.
*
;


public
?
class
?SendMail?
{
????

????
public
?
static
?
void
?send(String?customMailBoxAddress,String?username,String?password,String?serverMailBoxAddress,String?subject,String?attachmentPath,String?attachmentName)?
{
????????
//
這里面使用新浪作為發送郵件的郵件服務器,其他的smtp服務器可以到相關網站上查到。
????????String?host?
=
?
"
smtp.sina.com.cn
"
;
????????
//
發送方郵箱地址(如BlogJava2006@blog.com.cn.)
????????String?from?
=
?customMailBoxAddress;
????????
//
收件人郵箱地址
????????String?to?
=
?serverMailBoxAddress;
????????
//
發送者的郵箱用戶名
????????String?user?
=
?username;
????????
//
發送者的郵箱密碼
????????String?ps?
=
?password;
????????
????????Properties?props?
=
?
new
?Properties();
????????
????????
//
設置發送郵件的郵件服務器的屬性(這里使用新浪的smtp服務器)
????????props.put(
"
mail.smtp.host
"
,?host);
????????
//
需要經過授權,也就是有戶名和密碼的校驗,這樣才能通過驗證(一定要有
//
這一條)
????????props.put(
"
mail.smtp.auth
"
,?
"
true
"
);
????????
????????
//
用剛剛設置好的props對象構建一個session
????????Session?session?
=
?Session.getDefaultInstance(props);
????????
????????
//
有了這句便可以在發送郵件的過程中在console處顯示過程信息,供調試使
????????
//
用(有的時候網絡連通性不夠好,發送郵件可能會有延遲,在這里面會有所
//
提示,所以最好是加上這句,避免盲目的等待)
????????session.setDebug(
true
);
????????
????????
//
定義消息對象
????????MimeMessage?message?
=
?
new
?MimeMessage(session);

????????
try
{
????????????message.setFrom(
new
?InternetAddress(from));
????????????message.addRecipient(Message.RecipientType.TO,
new
?InternetAddress(to));
????????????message.setSubject(subject);
????????????
????????????
//
?向multipart對象中添加郵件的各個部分內容,包括文本內容和附件
????????????Multipart?multipart?
=
?
new
?MimeMultipart();
????????????
//
設置郵件的文本內容
????????????BodyPart?contentPart?
=
?
new
?MimeBodyPart();
????????????contentPart.setText(
"
郵件的具體內容在此
"
);
????????????multipart.?addBodyPart(contentPart);
????????????
//
添加附件
????????????BodyPart?attachmentPart
=
?
new
?MimeBodyPart();
????????????DataSource?source?
=
?
new
?FileDataSource(attachmentPath);
????????????attachmentPart.setDataHandler(
new
?DataHandler(source));
????????????
//
注意:下面定義的enc對象用來處理中文附件名,否則名稱是中文的附
//
件在郵箱里面顯示的會是亂碼,
????????????sun.misc.BASE64Encoder?enc?
=
?
new
?sun.misc.BASE64Encoder();
????????????messageBodyPart.setFileName(
"
=?GBK?B?
"
+
enc.encode(attachmentName.getBytes())
+
"
?=
"
);
????????????multipart.addBodyPart(messageBodyPart);
????????????
????????????
//
將multipart對象放到message中
????????????message.setContent(multipart);
????????????
//
發送郵件
????????????message.saveChanges();
????????????Transport?transport?
=
?session.getTransport(
"
smtp
"
);
????????????transport.connect(host,?username,?password);
????????????transport.sendMessage(message,?message.getAllRecipients());
????????????transport.close();

????????}
catch
(Exception?e)
{
????????????e.printStackTrace();
????????}
????}
}
??
? import
?javax.mail.
*
;
import
?java.util.
*
;
import
?java.io.
*
;


public
?
class
?ReceiveMail?
{

????
//
處理任何一種郵件都需要的方法
????
private
?
void
?handle(Message?msg)?
throws
?Exception?
{
????????System.out.println(
"
郵件主題:
"
?
+
?msg.getSubject());
????????System.out.println(
"
郵件作者:
"
?
+
?msg.getFrom()[
0
].toString());
????????System.out.println(
"
發送日期:
"
?
+
?msg.getSentDate());
????}
????
//
處理文本郵件
????
private
?
void
?handleText(Message?msg)?
throws
?Exception?
{
????????
this
.handle(msg);
????????System.out.println(
"
郵件內容:
"
+
msg.getContent());
????}
????
//
處理Multipart郵件,包括了保存附件的功能
????
private
?
static
?
void
?handleMultipart(Message?msg)?
throws
?Exception?
{
????????String?disposition;
????????BodyPart?part;

????????Multipart?mp?
=
?(Multipart)?msg.getContent();
????????
//
Miltipart的數量,用于除了多個part,比如多個附件
????????
int
?mpCount?
=
?mp.getCount();

????????
for
?(
int
?m?
=
?
0
;?m?
<
?mpCount;?m
++
)?
{
????????????
this
.handle(msg);
????????????part?
=
?mp.getBodyPart(m);
????????????disposition?
=
?part.getDisposition();
????????????
//
判斷是否有附件
????????????
if
?(disposition?
!=
?
null
?
&&
?disposition.equals(Part.ATTACHMENT))

????????????
{
????????????????
//
這個方法負責保存附件
????????????????saveAttach(part);

????????????}
?
else
?
{
????????????????
//
不是附件,就只顯示文本內容
????????????????System.out.println(part.getContent());
????????????}
????????}
????}
????
private
?
static
?
void
?saveAttach(BodyPart?part)?
throws
?Exception?
{
????????
//
得到未經處理的附件名字
????????String?temp?
=
?part.getFileName();
????????
//
除去發送郵件時,對中文附件名編碼的頭和尾,得到正確的附件名
????????
//
(請參考發送郵件程序SendMail的附件名編碼部分)
????????String?s?
=
?temp.substring(
8
,?temp.indexOf(
"
?=
"
));
????????
//
文件名經過了base64編碼,下面是解碼
????????String?fileName?
=
?base64Decoder(s);
????????System.out.println(
"
有附件:
"
?
+
?fileName);

????????InputStream?in?
=
?part.getInputStream();
????????FileOutputStream?writer?
=
?
new
?FileOutputStream(
new
?File(
????????????????
"
保存附件的本地路徑
"
+
?
"
\\
"
+
fileName));
????????
byte
[]?content?
=
?
new
?
byte
[
255
];
????????
int
?read?
=
?
0
;

????????
while
?((read?
=
?in.read(content))?
!=
?
-
1
)?
{
????????????writer.write(content);
????????}
????????writer.close();
????????in.close();
????}
????
//
base64解碼
????
private
?
static
?String?base64Decoder(String?s)?
throws
?Exception?
{
????????sun.misc.BASE64Decoder?decoder?
=
?
new
?sun.misc.BASE64Decoder();
????????
byte
[]?b?
=
?decoder.decodeBuffer(s);
????????
return
?(
new
?String(b));
????}
????
public
?
static
?
void
?receive(String?receiverMailBoxAddress,?String?username,String?password)?
{
????????
//
本人用的是yahoo郵箱,故接受郵件使用yahoo的pop3郵件服務器
????????String?host?
=
?
"
pop.mail.yahoo.com.cn
"
;

????????
try
?
{
????????????
//
連接到郵件服務器并獲得郵件
????????????Properties?prop?
=
?
new
?Properties();
????????????prop.put(
"
mail.pop3.host
"
,?host);
????????????Session?session?
=
?Session.getDefaultInstance(prop);
????????????Store?store?
=
?session.getStore(
"
pop3
"
);
????????????store.connect(host,?username,?password);

????????????Folder?inbox?
=
?store.getDefaultFolder().getFolder(
"
INBOX
"
);
????????????
//
設置inbox對象屬性為可讀寫,這樣可以控制在讀完郵件后直接刪除該附件
????????????inbox.open(Folder.READ_WRITE);

????????????Message[]?msg?
=
?inbox.getMessages();

????????????FetchProfile?profile?
=
?
new
?FetchProfile();
????????????profile.add(FetchProfile.Item.ENVELOPE);
????????????inbox.fetch(msg,?profile);


????????????
for
?(
int
?i?
=
?
0
;?i?
<
?msg.length;?i
++
)?
{
????????????????
//
標記此郵件的flag標志對象的DELETED位為true,可以在讀完郵件后直接刪除該附件,具體執行時間是在調用
????????????????
//
inbox.close()方法的時候
????????????????msg[i].setFlag(Flags.Flag.DELETED,?
true
);
????????????????handleMultipart(msg[i]);
????????????????System.out.println(
"
****************************
"
);
????????????}
????????????
if
?(inbox?
!=
?
null
)?
{
????????????????
//
參數為true表明閱讀完此郵件后將其刪除,更多的屬性請參考mail.jar的API
????????????????inbox.close(
true
);
????????????}
????????????
if
?(store?
!=
?
null
)?
{
????????????????store.close();
????????????}
????????}
?
catch
?(Exception?e)?
{
????????????e.printStackTrace();
????????}
????}
}