预览加载中,请您耐心等待几秒...
1/10
2/10
3/10
4/10
5/10
6/10
7/10
8/10
9/10
10/10

在线预览结束,喜欢就下载吧,查找使用更方便

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

Java认证考试:Spring实现邮件发送Java认证考试:Spring实现邮件发送Spring提供了一个发送邮件的抽象层,使发送邮件实现非常简单。下面代码需要mail.jar包,如果服务器需要认证,必须加入如下加粗代码:源文件:SendMail.java:packagemail;importorg.springframework.mail.javamail.JavaMailSenderImpl;importorg.springframework.mail.javamail.MimeMessageHelper;importjavax.mail.internet.MimeMessage;importjava.util.Properties;importjava.util.Date;/***@authorchrischen*/publicclassSendMail{//邮件发送器publicstaticStringSender(Stringsubject,Stringmsg,StringsendTo,StringfromMail,Stringuser,Stringpw,StringfromName,Stringprotocol,Stringhost,Stringport){try{finalStringusername=user;finalStringpass=pw;//需要认证Propertiesprops=newProperties();props.put(“mail.smtp.host”,host);props.put(“mail.smtp.auth”,“true”);props.put(“mail.transport.protocol”,protocol);props.put(“mail.from”,fromMail);//创建发送器JavaMailSenderImplsender=newJavaMailSenderImpl();sender.setHost(host);sender.setUsername(username);sender.setPassword(pass);//创建消息MimeMessagemessage=sender.createMimeMessage();message.addHeader(“X-Mailer”,“JavaMailer”);MimeMessageHelperhelper=newMimeMessageHelper(message);helper.setTo(sendTo);helper.setFrom(fromMail,fromName);helper.setSubject(subject);helper.setText(msg);helper.setSentDate(newDate());//开始发送sender.setJavaMailProperties(props);sender.send(message);}catch(Exceptione){System.out.println(“Error:”+e);return“Failure”;}return“Success”;}//测试publicstaticvoidmain(Stringargs[])throwsException{Stringsubject=“测试邮件”;//标题StringsendTo=“test@my.com”;//接收者邮件StringfromMail=“send@my.com”;//发送者邮件Stringuser=“send@my.com”;//发送者用户Stringpw=“password”;//发送者邮件密码StringfromName=“Chen”;//发送者名字Stringprotocol=“smtp”;//协议Stringhost=“smtp.my.com”;//发送主机Stringport=“25”;//端口Stringmsg=“简单邮件发送。”;//发送内容Stringret=Sender(subject,msg,sendTo,fromMail,user,pw,fromName,protocol,host,port);Sy