|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
通过视频学习比传统的大课堂学习更适合成人化的学习规律。有人说大课堂气氛好,学习氛围浓,热闹,可以认识很多人。Attachmentsareresourcesassociatedwithamailmessage,usuallykeptoutsideofthemessagelikeatextfile,spreadsheet,orimage.AswithcommonmailprogramslikeEudoraandpine,youcanattachresourcestoyourmailmessagewiththeJavaMailAPIandgetthoseattachmentswhenyoureceivethemessage.
Sendingattachments:
Sendingattachmentsisquitelikeforwardingmessages.Youbuildupthepartstomakethecompletemessage.Afterthefirstpart,yourmessagetext,youaddotherpartswheretheDataHandlerforeachisyourattachment,insteadofthesharedhandlerinthecaseofaforwardedmessage.Ifyouarereadingtheattachmentfromafile,yourattachmentdatasourceisaFileDataSource.ReadingfromaURL,itisaURLDataSource.OnceyouhaveyourDataSource,justpassitontotheDataHandlerconstructor,beforefinallyattachingittotheBodyPartwithsetDataHandler().Assumingyouwanttoretaintheoriginalfilenamefortheattachment,thelastthingtodoistosetthefilenameassociatedwiththeattachmentwiththesetFileName()methodofBodyPart.Allthisisshownhere:
//Definemessage
Messagemessage=newMimeMessage(session);
message.setFrom(newInternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
newInternetAddress(to));
message.setSubject("HelloJavaMailAttachment");
//Createthemessagepart
BodyPartmessageBodyPart=newMimeBodyPart();
//Fillthemessage
messageBodyPart.setText("PardonIdeas");
Multipartmultipart=newMimeMultipart();
multipart.addBodyPart(messageBodyPart);
//Parttwoisattachment
messageBodyPart=newMimeBodyPart();
DataSourcesource=newFileDataSource(filename);
messageBodyPart.setDataHandler(newDataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
//Putpartsinmessage
message.setContent(multipart);
//Sendthemessage
Transport.send(message);
Whenincludingattachmentswithyourmessages,ifyourprogramisaservlet,yourusersmustuploadtheattachmentbesidestellingyouwheretosendthemessage.Uploadingeachfilecanbehandledwithaformencodingtypeofmultipart/form-data.
<FORMENCTYPE="multipart/form-data"
method=postaction="/myservlet">
<INPUTTYPE="file"NAME="thefile">
<INPUTTYPE="submit"VALUE="Upload">
</FORM>
Note:MessagesizeislimitedbyyourSMTPserver,nottheJavaMailAPI.Ifyourunintoproblems,considerincreasingtheJavaheapsizebysettingthemsandmxparameters.
Exercise:
Exercise5.Howtosendattachments
Gettingattachments:
GettingattachmentsoutofyourmessagesisalittlemoreinvolvedthensendingthembecauseMIMEhasnosimplenotionofattachments.ThecontentofyourmessageisaMultipartobjectwhenithasattachments.YouthenneedtoprocesseachPart,togetthemaincontentandtheattachment(s).PartsmarkedwithadispositionofPart.ATTACHMENTfrompart.getDisposition()areclearlyattachments.However,attachmentscanalsocomeacrosswithnodisposition(andanon-textMIMEtype)oradispositionofPart.INLINE.WhenthedispositioniseitherPart.ATTACHMENTorPart.INLINE,youcansaveoffthecontentforthatmessagepart.JustgettheoriginalfilenamewithgetFileName()andtheinputstreamwithgetInputStream().
Multipartmp=(Multipart)message.getContent();
for(inti=0,n=multipart.getCount();i<n;i++){
Partpart=multipart.getBodyPart(i));
Stringdisposition=part.getDisposition();
if((disposition!=null)&&
((disposition.equals(Part.ATTACHMENT)||
(disposition.equals(Part.INLINE))){
saveFile(part.getFileName(),part.getInputStream());
}
}
ThesaveFile()methodjustcreatesaFilefromthefilename,readsthebytesfromtheinputstream,andwritesthemofftothefile.Incasethefilealreadyexists,anumberisaddedtotheendofthefilenameuntiloneisfoundthatdoesntexist.
//fromsaveFile()
Filefile=newFile(filename);
for(inti=0;file.exists();i++){
file=newFile(filename+i);
}
Thecodeabovecoversthesimplestcasewheremessagepartsareflaggedappropriately.Tocoverallcases,handlewhenthedispositionisnullandgettheMIMEtypeoftheparttohandleaccordingly.
if(disposition==null){
//Checkifplain
MimeBodyPartmbp=(MimeBodyPart)part;
if(mbp.isMimeType("text/plain")){
//Handleplain
}else{
//Specialnon-attachmentcaseshereofimage/gif,text/html,...
}
...
}
再举这样一个例子:如果你想对一个数字取绝对值,你会怎么做呢?java的做法是intc=Math.abs(-166);而ruby的做法是:c=-166.abs。呵呵,这就看出了java与ruby的区别。 |
|