JAVA网页编程之use javamail to send attachments(转)...
通过视频学习比传统的大课堂学习更适合成人化的学习规律。有人说大课堂气氛好,学习氛围浓,热闹,可以认识很多人。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的区别。 科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。 让你能够真正掌握接口或抽象类的应用,从而在原来的Java语言基础上跃进一步,更重要的是,设计模式反复向你强调一个宗旨:要让你的程序尽可能的可重用。 多重继承(以接口取代)等特性,增加了垃圾回收器功能用于回收不再被引用的对象所占据的内存空间,使得程序员不用再为内存管理而担忧。在 Java 1.5 版本中,Java 又引入了泛型编程(Generic Programming)、类型安全的枚举、不定长参数和自动装/拆箱等语言特性。 设计模式是高级程序员真正掌握面向对象核心思想的必修课。设计模式并不是一种具体"技术",它讲述的是思想,它不仅仅展示了接口或抽象类在实际案例中的灵活应用和智慧 Java自面世后就非常流行,发展迅速,对C++语言形成了有力冲击。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于个人PC、数据中心、游戏控制台 是一种使网页(Web Page)由静态(Static)转变为动态(Dynamic)的语言 是一种由美国SUN计算机公司(Sun Microsystems, Inc.)所研究而成的语言 一般学编程语言都是从C语开始学的,我也不例外,但还是可能不学过程语言而直接学面向对象语言的,你是刚接触语言,还是从C开始学比较好,基础会很深点,如果你直接学习JAVA也能上手,一般大家在学语言的时候都记一些语言的关键词,常有的包和接口等。再去做逻辑代码的编写,以后的学习过程都是从逻辑代码编写中提升的,所以这方面都是经验积累的。你要开始学习就从 是一种简化的C++语言 是一种安全的语言,具有阻绝计算机病毒传输的功能 你就该学一学Servlet了。Servlet就是服务器端小程序,他负责生成发送给客户端的HTML文件。JSP在执行时,也是先转换成Servlet再运行的。虽说JSP理论上可以完全取代Servlet,这也是SUN推出JSP的本意,可是Servlet用来控制流程跳转还是挺方便的,也令程序更清晰。接下来你应该学习一下Javabean了,可能你早就看不管JSP在HTML中嵌Java代码的混乱方式了,这种方式跟ASP又有什么区别呢? 是一种将安全性(Security)列为第一优先考虑的语言 是一种使网页(Web Page)由静态(Static)转变为动态(Dynamic)的语言
页:
[1]