预览加载中,请您耐心等待几秒...
1/4
2/4
3/4
4/4

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

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

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

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

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

Spring如何实现邮件发送源文件: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);System.out.println(“邮件发送结果:”+ret);}}使用MimeMessageHelper,可以实现Multipartemail,方便添加附件和内嵌资源等。