欢迎来到天天文库
浏览记录
ID:47864612
大小:12.92 KB
页数:5页
时间:2019-08-05
《javamial邮件发送程序》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、这里我用了两个类文件,邮件服务器是james我写好了能发给163qqsina等在这里分享下,里面有很详细的注释packagecom.james.mail.action;importjavax.mail.Authenticator;importjavax.mail.PasswordAuthentication;/***邮件验证类**@authordengyang**/publicclassMailAuthenticatorextendsAuthenticator{StringauthenName;StringauthenPass;publicMailAuthenticator
2、(Stringname,Stringpass){this.authenName=name;this.authenPass=pass;}publicStringgetAuthenName(){returnauthenName;}publicvoidsetAuthenName(StringauthenName){this.authenName=authenName;}publicStringgetAuthenPass(){returnauthenPass;}publicvoidsetAuthenPass(StringauthenPass){this.authenPass=aut
3、henPass;}@OverrideprotectedPasswordAuthenticationgetPasswordAuthentication(){//TODOAuto-generatedmethodstubreturnnewPasswordAuthentication(authenName,authenPass);}}packagecom.james.mail.action;importjava.util.Date;importjava.util.Properties;importjavax.mail.Authenticator;importjavax.mail.M
4、essage;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeMessage;importjavax.servlet.http.HttpServletRequest;importorg.apache.struts2.ServletActionContext;publicclassSendMail{publicStringsendMail(){HttpServletRequest
5、request=ServletActionContext.getRequest();try{Authenticatorac=newMailAuthenticator("dengyang","dengyang");Propertiesprops=newProperties();//也可用System.getProperties();props.put("mail.transport.protocol","smtp");//传输用的协议smtpprops.put("mail.smtp.host","192.168.1.2");//利用那个邮件服务发送props.put("mai
6、l.smtp.port","25");//发送的端口号props.put("mail.smtp.auth","true");//是否要验证Sessionsession=Session.getInstance(props,ac);//Session会自动调用//Sessionsession=Session.getInstance(props);//B.Session会自动调用session.setDebug(true);Messagemessage=newMimeMessage(session);//由邮件会话新建一个消息对象/***设置邮件*/InternetAddress
7、from=newInternetAddress("dengyang@rr12580.com");message.setFrom(from);//设置发件人InternetAddress[]to=newInternetAddress[]{newInternetAddress("275001477@qq.com"),newInternetAddress("793164760@qq.com")};//设置收件人message.setRecipients(Message.RecipientType.TO,to);//设置收
此文档下载收益归作者所有