




已阅读5页,还剩14页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
javamail 发送验证邮件(jsp/servlet实例源码)发送email:包括文本邮件、html邮件、带附件的邮件、smtp验证接收email:pop3远程连接、收取不同mime的邮件、处理附件首先需要配置环境。需要mail.jar和activation.jar两个包。地址在上,很容易找到。放到classpath中,比如我的是这样.e:tomcat5commonlibactivation.jare:tomcat5commonlibmail.jare:tomcat5commonlibmailapi.jare:tomcat5commonlibe:jdk1.5libdt.jare:jdk1.5libtool.jare:jdk1.5libe:jdk1.5bine:jdk1.5。-例子一、javamail.jsp发送验证邮件源代码javamail电子邮件发送系统javamail电子邮件发送系统本例子是用javamail来发送邮件的最简单的例子,认证才能正常发送邮件。收件人email:发件人mail:邮件标题内容:stringtouser=request.getparameter(recipients)!=null?request.getparameter(recipients):out.print(recipients:+touser)stringfromuser=request.getparameter(frommail)!=null?request.getparameter(frommail):out.print(frommail:+fromuser)stringsubject=request.getparameter(subject)!=null?request.getparameter(subject):out.print(subject:+subject)stringcontents=request.getparameter(contents)!=null?request.getparameter(contents):out.print(contents:+contents)out.print()trypropertiesprops=newproperties()props.put(mail.smtp.authtrue)/是否验证props.put(mail.smtp.hosthost)props.put(mail.smtp.useruser)props.put(mail.smtp.passwordpassword)booleansessiondebug=falseauthenticatorauth=newauthenticator()/sessionmailsession=session.getdefaultinstance(propsauth)/有时可能被拒绝sessionmailsession=session.getinstance(propsauth)/用户验证/sessionmailsession=session.getinstance(props)mailsession.setdebug(sessiondebug)messagemsg=newmimemessage(mailsession)msg.setfrom(newinternetaddress(fromuser)msg.setrecipient(message.recipienttype.tonewinternetaddress(touser)msg.setsubject(邮件标题:+subject)/(mimemessage)msg).setsubject(subjectgbk)/设置中文标题msg.setsentdate(newdate()stringtext=javamail.jsp发送认证邮件测试。+contentsmsg.setcontent(textcontenttype)transporttransport=mailsession.gettransport(smtp)transport.connect(hostuserpassword)transport.send(msg)你的邮件已发送,请返回。-例子二、用servlet来发送邮件,postmail.java源代码ernet.importjava.util./创建日期2005-12-1todo要更改此生成的文件的模板,请转至窗口首选项java代码样式代码模板/authoradministratortodo要更改此生成的类型注释的模板,请转至窗口首选项java代码样式代码模板/publicclasspostmail/stringmailform=stringmailto=stringmailsubject=stringmailcontent=/stringhost=stringuser=usernamestringpassword=xxxxxpublicvoidsethost(stringhost)this.host=hostpublicvoidsetaccount(stringuserstringpassword)this.user=userthis.password=passwordpublicvoidsend(stringfromstringtostringsubjectstringcontent)propertiesprops=newproperties()props.put(mail.smtp.hosthost)/指定smtp服务器props.put(mail.smtp.authtrue)/指定是否需要smtp验证try/sessionmailsession=session.getdefaultinstance(props)/用这个有时被拒绝;sessionmailsession=session.getinstance(props)mailsession.setdebug(true)/是否在控制台显示debug信息messagemessage=newmimemessage(mailsession)message.setfrom(newinternetaddress(from)/发件人message.addrecipient(message.recipienttype.tonewinternetaddress(to)/收件人/message.setsubject(subject)(mimemessage)message).setsubject(subjectgbk)/得到中文标题forlinux,windows下不用/message.setsubject(subject)/邮件主题/内容类型content-type/普通文本为text/plainhtml格式为text/htmlmessage.setcontent(contenttext/htmlcharset=gbk)/message.settext(javamail,你好!)message.settext(content)/邮件内容message.savechanges()transporttransport=mailsession.gettransport(smtp)transport.connect(hostuserpassword)transport.sendmessage(messagemessage.getallrecipients()transport.close()catch(exceptione)system.out.println(e)publicstaticvoidmain(stringargs)/postmailsm=newpostmail()sm.sethost()/指定要使用的邮件服务器sm.setaccount(xxxxxxxxxx)/指定帐号和密码/paramstring发件人的地址paramstring收件人地址paramstring邮件标题paramstring邮件正文/调用postmail.java的jsp文件:postmail.jsp源代码电子邮件java.util.datedate=newjava.util.date()out.print(newjava.util.date()out.print()本例子是调用postmail.java来发送认证的邮件,需要下载mail.jar和activation.jar,并设置classpath请输入发件人电子邮件:请输入收件人电子邮件:0&!email.matches(w+(-+.w+)w+(-.w+).w+(-.w+)$)out.print(发件人邮件地址不正确)returnif(toemail.trim().length()0&!toemail.matches(w+(-+.w+)w+(-.w+).w+(-.w+)$)out.print(收件人邮件地址不正确)returnif(email.trim().length()0&toemail.trim().length()0)trymail.send(emailtoemailtestpostmail.java中文javamail,你好postmail!)out.print(邮件已发送)catch(exceptione)out.print(出错了)out.print(e.getmessage()system.out.print(e.getmessage()-其他java源代码:mailmanager.java源代码ernet.importjava.util.importjavax.activation.importjava.io.publicclassmailmanager/sun.misc.base64encoderenc=newsun.misc.base64encoder()/msg.setsubject(=?gb2312?b?+enc.encode(subject.getbytes()+?=)/比如说有一个邮件帐号:/pop3_host_name和smtp_host_name分别是这邮件地址的pop3和smtp服务器dns/则smtp_auth_user=smtpusersmtp_auth_pwd就是该帐号的密码privatefinalstringpop3_host_name=privatefinalstringsmtp_host_name=privatefinalstringsmtp_auth_user=usernameprivatefinalstringsmtp_auth_pwd=xxxxxxprivateauthenticatorauth=newauthenticator()publicpasswordauthenticationgetpasswordauthentication()returnnewpasswordauthentication(smtp_auth_usersmtp_auth_pwd)publicvoidsendmail(stringtoaddrstringsubjectstringbodystringfromaddrstringcontenttype)trypropertiesprops=newproperties()/指定smtp服务器,邮件通过它来投递props.put(mail.smtp.host(string)smtp_host_name)props.put(mail.smtp.auth(string)true)/sessionsession=session.getdefaultinstance(propsauth)sessionsession=session.getinstance(propsauth)/messagemsg=newmimemessage(session)/指定发信人msg.setfrom(newinternetaddress(fromaddr)/指定收件人/internetaddresstos=newinternetaddress(toaddr)/msg.setrecipients(message.recipienttype.totos)/指定收件人,多人时用逗号分隔internetaddresstos=internetaddress.parse(toaddr)msg.setrecipients(message.recipienttype.totos)/标题/转码base64encoder/sun.misc.base64encoderenc=newsun.misc.base64encoder()/msg.setsubject(=?gbk?b?+enc.encode(subject.getbytes()+?=)/msg.setsubject(newstring(subject.getbytes(gbk)iso8859-1)/msg.setsubject(=?gb2312?b?+enc.encode(subject.getbytes()+?=)/msg.setsubject(subject)(mimemessage)msg).setsubject(subjectgbk)/得到中文标题forlinux,windows下不用/内容msg.settext(body)/发送时间msg.setsentdate(newdate()/内容类型content-type/普通文本为text/plainhtml格式为text/htmlcharset=gbkmsg.setcontent(bodycontenttype)/发送transport.send(msg)catch(exceptione)system.out.println(e)publicvoidsendmailwithattatchment(stringtoaddrstringsubjectstringbodystringfromaddrstringcontenttypestringfilelist)trypropertiesprops=newproperties()/指定smtp服务器,邮件通过它来投递props.put(mail.smtp.hostsmtp_host_name)props.put(mail.smtp.authtrue)sessionsession=session.getdefaultinstance(propsauth)/sessionsession=session.getinstance(propsauth)/securitymanagersecurity=system.getsecuritymanager()system.out.println(securitymanager+security)/lookingatyourcodeitlookslikeyouwillseeanullvalueforsecurity./ifthesecurityisnullusesession.getinstance(propsauth)/insteadofsession.getdefaultinstance(propsauth).messagemsg=newmimemessage(session)/指定发信人msg.setfrom(newinternetaddress(fromaddr)/指定收件人/internetaddresstos=newinternetaddress(toaddr)/msg.setrecipients(message.recipienttype.totos)/指定收件人,多人时用逗号分隔internetaddresstos=internetaddress.parse(toaddr)msg.setrecipients(message.recipienttype.totos)/标题/转码base64encoder/sun.misc.base64encoderenc=newsun.misc.base64encoder()/msg.setsubject(=?gbk?b?+enc.encode(subject.getbytes()+?=)/msg.setsubject(newstring(subject.getbytes(gbk)iso8859-1)/msg.setsubject(=?gb2312?b?+enc.encode(subject.getbytes()+?=)/msg.setsubject(subject)(mimemessage)msg).setsubject(subjectgbk)/得到中文标题forlinux,windows下不用/发送时间/msg.setsentdate(newdate()msg.setsentdate(newjava.util.date()multipartmultipart=newmimemultipart()mimebodypartbodypart=newmimebodypart()/内容bodypart.settext(body)/content-typebodypart.setcontent(bodycontenttype)multipart.addbodypart(bodypart)for(inti=0ifilelist.length+i)bodypart=newmimebodypart()filef=newfile(filelisti)datasourcesource=newfiledatasource(f)bodypart.setdatahandler(newdatahandler(source)bodypart.setfilename(f.getname()multipart.addbodypart(bodypart)msg.setcontent(multipart)/发送transport.send(msg)catch(exceptione)system.out.println(e)/publicmailsgetmails()mailsmails=nulltry/propertiesprops=system.getproperties()propertiesprops=newproperties()props.put(mail.pop3.hostsmtp_host_name)props.put(mail.pop3.authtrue)sessionsession=session.getdefaultinstance(propsauth)storestore=session.getstore(pop3)store.connect()folderinbox=store.getfolder(inbox)mails=newmails(inbox)store.close()catch(exceptione)system.out.println(e)returnmails/-sendmailusingauthentication.java源代码,引用这个javabean发送认证email例子ernet.importjava.util./tousethisprogramchangevaluesforthefollowingthreeconstantssmtp_host_name-hasyoursmtphostnamesmtp_auth_user-hasyoursmtpauthenticationusernamesmtp_auth_pwd-hasyoursmtpauthenticationpasswordnextchangevaluesforfieldsemailmsgtxt-messagetextfortheemailemailsubjecttxt-subjectforemailemailfromaddress-emailaddresswhosenamewillappearsasfromaddressnextchangevalueforemaillist.thisstringarrayhaslistofallemailaddressestoemailemailneedstobesentto.nexttoruntheprogramuteitasfollowssendmailusingauthenticationauthprog=newsendmailusingauthentication()/publicclasssendmailusingauthenticationprivatestaticfinalstringsmtp_host_name=privatestaticfinalstringsmtp_auth_user=usernameprivatestaticfinalstringsmtp_auth_pwd=privatestaticfinalstringemailmsgtxt=onlineorderconfirmationmessage.alsoincludethetrackingnumber.中国人privatestaticfinalstringemailsubjecttxt=这里是标题javamailtestprivatestaticfinalstringemailfromaddress=/addlistofemailaddresstowhoemailneedstobesenttoprivatestaticfinalstringemaillist=publicstaticvoidmain(stringargs)throwsexception/sendmailusingauthenticationsmtpmailsender=newsendmailusingauthentication()/smtpmailsender.postmail(emaillistemailsubjecttxtemailmsgtxtemailfromaddress)/smtpmailsender.postmail(emaillistemailsubjecttxtemailmsgtxtemailfromaddress)/system.out.println(sucessfullysentmailtoallusers)publicvoidpostmail(stringrecipientsstringsubject/publicvoidpostmail(stringrecipientsstringsubjectstringmessagestringfrom)throwsmessagingexceptionbooleandebug=false/setthehostsmtpaddresspropertiesprops=newproperties()props.put(mail.smtp.hostsmtp_host_name)props.put(mail.smtp.authtrue)authenticatorauth=newsmtpauthenticator()/sessionsession=session.getdefaultinstance(propsauth)/有时可能被拒绝sessionsession=session.getinstance(propsauth)session.setdebug(debug)/createamessagemessagemsg=newmimemessage(session)/se
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 滚动习题(二)范围6.3
- 媒体传播企业会计核算及财务风险防控合同
- 离婚协议中子女抚养及教育费用支付协议范本
- 离婚协议书模板(夫妻共同债务清偿)
- 签订即编制确立的影视制作公司艺人聘用合同
- 离婚协议签订后共同财产分割及债权债务处理协议
- 异地跨省私房房屋买卖合同示范文本
- 形体礼仪培训内容
- 老干部工作课件
- 网络维护培训方案
- 初中英语单词表(For-Junior)2182个 带音标
- 人教鄂教版六年级上册科学全册教案
- 财务工作内部培训课件
- 铁路防雷及接地工程技术规范(TB 10180-2016)
- 网络安全意识培训
- 建筑艺术赏析(职业通用)全套教学课件
- 无人机理论知识无人机理论基础
- 医院检验科质量手册
- 农业科技在2024年的发展与前景展望
- 护理不良事件警示教育(新)
- 人生规划和人生定位课件
评论
0/150
提交评论