【JSP开源代码栏目提醒】:本文主要为网学会员提供“javamail_发送验证邮件(jspservlet实例源代码) - 编程语言”,希望对需要javamail_发送验证邮件(jspservlet实例源代码) - 编程语言网友有所帮助,学习一下!
javamail 发送验证邮件
jsp/servlet 实例源码● 发送 email:包括文本邮件、html 邮件、带附件的邮件、smtp 验证● 接收 email:pop3 远程连接、收取不同 mime 的邮件、处理附件首先需要配置环境。
需要 mail.jar 和 activation.jar 两个包。
地址在 java.sun.com上 , 很 容 易 找 到 。
放 到 classpath 中 , 比 如 我 的 是 这样 . e:tomcat5commonlibactivation.jar e:tomcat5commonlibmail.jar e: tomcat5commonlibmailapi.jar e:tomcat5commonlib e:jdk1.5libdt.jar e:jdk1.5libtool.jar e:jdk1.5lib e:jdk1.5bin e:jdk1.5。
--------------------------------------------------------------------------------例子一、javamail.
jsp 发送验证邮件源
代码lt page languagequot javaquot contenttypequot text/html charsetgbkquot pageencodingquot gbkquot gtlt//set chinese char//homepage:jiarry.126.comrequest.setcharacterencodingquot gbkquot response.setcharacterencodingquot gbkquot response.setcontenttypequot text/html charsetgbkquot gtlt page importquot javax.mail. javax.mail.internet. javax.activation. java.util. java.io. quot gtlt htmlgtlt headgtlt titlegt javamail 电子邮件发送系统lt /titlegtlt /headgtlt bodygtjavamail 电子邮件发送系统lt brgt 本例子是用 java mail 来发送邮件的最简单的例子,认证才能正常发送邮件。
lt form actionquot quot methodquot postquot onsubmitquot quot gt收件人 email:lt br /gt lt input typequot textquot namequot recipientsquot gt lt br /gt发件人 mail:lt br /gt lt input namequot frommailquot typequot textquot sizequot 30quot /gt lt br /gt邮件标题 lt br /gt lt input namequot subjectquot typequot textquot sizequot 50quot /gt lt br /gt内容:lt br /gt lt textarea namequot contentsquot colsquot 50quot rowsquot 10quot gt lt /textareagtlt br /gt lt input typequot submitquot namequot submitquot valuequot 发送邮件quot /gtlt formgtltstring host quot smtp.126.comquotstring user quot usernamequotstring password quot xxxxxquotstring contenttypequot text/html charsetgbkquotprivate class authenticator extends javax.mail.authenticator public passwordauthentication getpasswordauthentication string un user string pw password return new passwordauthenticationun pw gtlt string touser request.getparameterquot recipientsquot null request.getparameterquot recipientsquot :quotquot out.printquot lt brgt recipients:quot touser string fromuser request.getparameterquot frommailquot null request.getparameterquot frommailquot :quotquot out.printquot lt brgt frommail:quot fromuser string subject request.getparameterquot subjectquot null request.getparameterquot subjectquot : quot quot out.printquot lt brgt subject:quot subject string contents request.getparameterquot contentsquot null request.getparameterquot contentsquot :quotquot out.printquot lt brgt contents:quot contents out.printquot lt brgt quot tryproperties props new propertiesprops.putquot mail.smtp.authquot quot truequot //是否验证props.putquot mail.smtp.hostquot hostprops.putquot mail.smtp.userquot userprops.putquot mail.smtp.passwordquot passwordboolean sessiondebug falseauthenticator auth new authenticator//session mailsession session.getdefaultinstanceprops auth //有时可能被拒绝session mailsession session.getinstanceprops auth //用户验证//session mailsession session.getinstancepropsmailsession.setdebugsessiondebugmessage msg new mimemessagemailsessionmsg.setfromnew internetaddress fromuser msg.setrecipientmessage.recipienttype.to new internetaddress touser msg.setsubject quot 邮件标题:quot subject//mimemessagemsg.setsubjectsubject quot gbkquot //设置中文标题msg.setsentdatenew datestring text quot javamail.
jsp 发送认证邮件lt bgt 测试lt /bgt 。
lt hrgt quot contentsmsg.setcontenttext contenttypetransport transport mailsession.gettransportquot smtpquot transport.connecthost user passwordtransport.send msg gt lt pgt 你的邮件已发送,lt a hrefquot javascript:window.go-1quot gt 请返回。
lt /gt lt /pgt ltcatchexception mout.printlnm.tostring gtlt /bodygtlt /htmlgt--------------------------------------------------------------------------------例子二、用 servlet 来发送邮件 ,postmail.java 源
代码package mailimport javax.mail.import javax.mail.internet.import java.util./ 创建日期 2005-12-1 todo 要更改此生成的文件的模板,请转至 窗口 - 首选项 - java -
代码样式 -
代码模板// author administrator todo 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - java -
代码样式 -
代码模板/public class postmail /string mailformquot quot string mailtoquot quot string mailsubjectquot quot string mailcontentquot quot /string hostquot smtp.126.comquot string userquot usernamequot string passwordquot xxxxxquot public void sethoststring host this.hosthost public void setaccountstring user string password this.useruser this.passwordpassword public void sendstring from string to string subject string content properties props new properties props.putquot mail.smtp.hostquot host //指定 smtp 服务器 props.putquot mail.smtp.authquot quot truequot //指定是否需要 smtp 验证 try //session mailsession session.getdefaultinstanceprops //用这个有时被拒绝; session mailsession session.getinstanceprops mailsession.setdebugtrue //是否在控制台显示 debug 信息 message messagenew mimemessagemailsession message.setfromnew internetaddressfrom //发件人 message.addrecipientmessage.recipienttype.to new internetaddressto // 收 件人 //message.setsubjectsubject mimemessagemessage.setsubjectsubject quot gbkquot //得到中文标题 for linux,windows 下不用 // message.setsubjectsubject //邮件主题 //内容类型 content-type //普通文本为 text/plain html 格式为 text/html message.setcontentcontent quot text/html charsetgbkquot //message.settextquot lt htmlgt lt bodygt lt h1gt java mail,你好lt /bodygt lt /htmlgt quot message.settextcontent //邮件内容 message.savechanges transport transport mailsession.gettransportquot smtpquot transport.connecthost user password transport.sendmessagemessage message.getallrecipients transport.close catchexception e system.out.printlne public static void mainstring args / postmail smnew postmail sm.sethostquot smtp.126.comquot //指定要使用的邮件服务器 sm.setaccountquot xxxxxquot quot xxxxxquot //指定帐号和密码 / param string 发件人的地址 param string 收件人地址 param string 邮件标题 param string 邮件正文 / 调用 postmail.java 的
jsp 文件:postmail.
jsp 源
代码lt page languagequot javaquot contenttypequot text/html charsetgbkquot pageencodingquot gbkquot gtlt//set chinese char//homepage:jiarry.126.comrequest.setcharacterencodingquot gbkquot response.setcharacterencodingquot gbkquot response.setcontenttypequot text/html charsetgbkquot gtlt htmlgtlt headgtlt meta http-equivquot content-typequot contentquot text/html charsetgb2312quot gtlt titlegt 电子邮件lt /titlegtlt script languagequot javascriptquot gtlt --function checkdata var txt document.forms0.email.valueiftxt.searchquot w-.w w-.w.w-.wquot 0 alertquot 请输入正确电子邮件quot document.forms0.email.select return falsereturn true--gtlt /scriptgtlt /headgtlt bodygtlt pgtltjava.util.date date new java.util.dateout.printnew java.util.dateout.printquot lt hrgt quot gt本例子是调用 postmail.java 来发送认证的邮件,需要下载 mail.jar 和 activation.jar,并设置classpathlt
jsp:usebean idquot mailquot scopequot pagequot classquot mail.postmailquot /gtlt form actionquot quot methodquot postquot onsubmitquot return checkdataquot gtlt pgt 请输入发件人电子邮件:lt input typequot textquot namequot emailquot gtlt pgt 请输入收件人电子邮件:lt input typequot textquot namequot toemailquot gtlt pgt lt input typequot submitquot valuequot 发送quot gtlt formgtltstring email quot quot toemailquot quot ifrequest.getparameterquot emailquot null email request.getparameterquot emailquot ifrequest.getparameterquot toemailquot null toemail request.getparameterquot toemailquot //ifemailnull if email.trim.lengthgt 0 amp amp email.matchesquot w-.w w-.w.w-.wquot out.printquot 发件人邮件地址不正确quot return iftoemail.trim.lengthgt 0 amp amp toemail.matchesquot w-.w w-.w.w-.wquot out.printquot 收件人邮件地址不正确quot return ifemail.trim.lengthgt 0 amp amp toemail.trim.lengthgt 0 try mail.sendemail toemail quot test postmail.java 中 文 quot quot lt h1gt java mail , 你好 postmaillt /h1gt lt brgt lt brgt lt bgt jiarry.126.comlt /bgt quot out.printquot lt font color green gt 邮件已发送lt /fontgt quot catchexception e out.print quot lt font color red gt 出错了lt /fontgt quot out.printe.getmessage system.out.printe.getmessage gtlt /bodygtlt /htmlgt--------------------------------------------------------------------------------其他 java 源
代码:mailmanager.java 源
代码package mailimport javax.mail.import javax.mail.internet.import java.util.import javax.activation.import java.io.public class mailmanager //sun.misc.base64encoder enc new sun.misc.base64encoder //msg.setsubjectquot gb2312bquot enc.encodesubject.getbytesquot quot //比如说有一个邮件帐号: smtpuser xxx.com//pop3_host_name 和 smtp_host_name 分别是这邮件地址的 pop3 和 smtp 服务器 dns//则 smtp_auth_user quot smtpuserquot smtp_auth_pwd 就是该帐号的密码private final string pop3_host_name quot pop3.126.comquotprivate final string smtp_host_name quot smtp.126.comquotprivate final string smtp_auth_user quot usernamequotprivate final string smtp_auth_pwd quot xxxxxxquotprivate authenticator auth new authenticatorpublic passwordauthentication getpasswordauthentication return new passwordauthenticationsmtp_auth_user smtp_auth_pwdpublic void sendmailstring toaddr string subject string body string fromaddr string contenttype try properties props new properties //指定 smtp 服务器,邮件通过它来投递 props.putquot mail.smtp.hostquot stringsmtp_host_name props.putquot mail.smtp.authquot stringquot truequot //session session session.getdefaultinstanceprops auth session session session.getinstanceprops auth // message msg new mimemessagesession //指定发信人 msg.setfromnew internetaddressfromaddr //指定收件人//internetaddress tos new internetaddresstoaddr//msg.setrecipientsmessage.recipienttype.to tos//指定收件人,多人时用逗号分隔internetaddress tos internetaddress.parsetoaddrmsg.setrecipientsmessage.recipienttype.to tos//标题//转码 base64encoder//sun.misc.base64encoder enc new sun.misc.base64encoder//msg.setsubjectquot gbkbquot enc.encodesubject.getbytesquot quot //msg.setsubjectnew stringsubject.getbytesquot gbkquot quot iso8859-1quot //msg.setsubjectquot gb2312bquot enc.encodesubject.getbytesquot quot //msg.setsubjectsubjectmimemessagemsg.setsubjectsubject quot gbkquot //得到中文标题 for linux,windows 下不用//内容msg.settextbody//发送时间msg.setsentdatenew date//内容类型 content-type//普通文本为 text/plain html 格式为 text/html charsetgbkmsg.setcontentbody contenttype//发送transport.sendmsg catchexception esystem.out.printlne public void sendmailwithattatchmentstring toaddr string subject string body string fromaddr string contenttype string filelist try properties props new properties //指定 smtp 服务器,邮件通过它来投递 props.putquot mail.smtp.hostquot smtp_host_name props.putquot mail.smtp.authquot quot truequot session session session.getdefaultinstanceprops auth //session session session.getinstanceprops auth // securitymanager security system.getsecuritymanager system.out.printlnquot security managerquot security//looking at your code it looks like you will see a null value for security.//if the security is null use session.getinstanceprops auth//instead of session.getdefaultinstanceprops auth. message msg new mimemessagesession //指定发信人 msg.setfromnew internetaddressfromaddr //指定收件人 //internetaddress tos new internetaddresstoaddr //msg.setrecipientsmessage.recipienttype.to tos //指定收件人,多人时用逗号分隔 internetaddress tos internetaddress.parsetoaddr msg.setrecipientsmessage.recipienttype.to tos//标题//转码 base64encoder//sun.misc.base64encoder enc new sun.misc.base64encoder//msg.setsubjectquot gbkbquot enc.encodesubject.getbytesquot quot //msg.setsubjectnew stringsubject.getbytesquot gbkquot quot iso8859-1quot //msg.setsubjectquot gb2312bquot enc.encodesubject.getbytesquot quot //msg.setsubjectsubjectmimemessagemsg.setsubjectsubject quot gbkquot //得到中文标题 for linux,windows 下不用//发送时间//msg.setsentdatenew datemsg.setsentdatenew java.util.datemultipart multipart new mimemultipartmimebodypart bodypart new mimebodypart//内容bodypart.settextbody//content-typebodypart.setcontentbody contenttypemultipart.addbodypartbodypartforint i0 ilt filelist.length i bodypart new mimebodypartfile f new filefilelistidatasource source new filedatasourcefbodypart.setdatahandlernew datahandlersourcebodypart.setfilenamef.getnamemultipart.addbodypartbodypart msg.setcontentmultipart //发送 transport.sendmsg catchexception e system.out.printlne /public mails getmails mails mailsnull try //properties props system.getproperties properties props new properties props.putquot mail.pop3.hostquot smtp_host_name props.putquot mail.pop3.authquot quot truequot session session session.getdefaultinstanceprops auth store store session.getstorequot pop3quot store.connect folder inbox store.getfolderquot inboxquot mails new mailsinbox store.close catchexception e system.out.printlne return mails/--------------------------------------------------------------------------------sendmailusingauthentication.java 源
代码,引用这个 java bean 发送认证 email 例子package mailimport javax.mail.import javax.mail.internet.import java.util./ to use this program change values for the following three constants smtp_host_name -- has your smtp host name smtp_auth_user -- has your smtp authentication username smtp_auth_pwd -- has your smtp authentication password next change values for fields emailmsgtxt -- message text for the email emailsubjecttxt -- subject for email emailfromaddress -- email address whose name will appears as quot fromquot address next change value for quot emaillistquot . this string array has list of all email addresses to email email needs to be sent to. next to run the program ute it as follows sendmailusingauthentication authprog new sendmailusingauthentication/public class sendmailusingauthentication private static final string smtp_host_name quot smtp.126.comquot private static final string smtp_auth_user quot usernamequot private static final string smtp_auth_pwd quot quot private static final string emailmsgtxt quot online order confirmation message. also include thetracking number.中国人quot private static final string emailsubjecttxt quot 这里是标题 java mail testquot private static final string emailfromaddress quot user 126.comquot // add list of email address to who email needs to be sent to private static final string emaillist quot username 126.comquot quot xxxx 126.comquot public static void mainstring args throws exception //sendmailusingauthentication smtpmailsender new sendmailusingauthentication //smtpmailsender.postmail emaillist emailsubjecttxt emailmsgtxt emailfromaddress //smtpmailsender.postmail emaillist emailsubjecttxt emailmsgtxt emailfromaddress //system.out.printlnquot sucessfully sent mail to all usersquot public void postmail string recipients string subject //public void postmail string recipients string subject string message string from throws messagingexception boolean debug false //set the host smtp address prop.
上一篇:
JavaScript脚本形式的呼叫中心系统第三方开发接口源代码JSP和JAVA版
下一篇:
Function GetIp(IP) 获得ip asp