下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
<!--邮件发送实现类--> <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="smtp.qq.com"/> <property name="port" value="25"/> <property name="username" value="你的邮箱"/> <property name="password" value="你的邮箱密码"/> <property name="javaMailProperties"> <props > <prop key="mail.smtp.auth">true</prop> </props> </property> </bean> <bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage"> <property name="from" value="发件人" /> <property name="subject" value="邮件主题" /> <!-- <property name="text" value="邮件内容" /> --> </bean> <!--线程池异步发送邮件--> <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="5" /> <property name="maxPoolSize" value="10" /> <property name="queueCapacity" value="25" /> </bean> <!--FreeMarker模板--> <bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="classpath:template" /> <!--<property name="templateLoaderPath" value="/template" />--> <property name="freemarkerSettings"> <props> <prop key="locale">zh_CN</prop> <prop key="default_encoding">UTF-8</prop> </props> </property> </bean>
public class MailSender { @Inject private JavaMailSender javaMailSender; @Inject private SimpleMailMessage simpleMailMessage; @Inject private FreeMarkerConfigurer freeMarkerConfigurer; @Inject private TaskExecutor taskExecutor; /** * 构建邮件内容,发送邮件。 * @param user 用户 * @param url 链接 */ public void send(User user, String url,String email) { String nickname = user.getNickname(); String to = email; String text = ""; Map<String, String> map = new HashMap<String, String>(1); map.put("url", url); try { // 从FreeMarker模板生成邮件内容 Template template = freeMarkerConfigurer.getConfiguration().getTemplate("register_mail.ftl"); // 模板中用${XXX}站位,map中key为XXX的value会替换占位符内容。 text = FreeMarkerTemplateUtils.processTemplateIntoString(template, map); } catch (IOException e) { e.printStackTrace(); } catch (TemplateException e) { e.printStackTrace(); } this.taskExecutor.execute(new SendMailThread(to,null,text)); } // 内部线程类,利用线程池异步发邮件。 private class SendMailThread implements Runnable { private String to; private String subject; private String content; private SendMailThread(String to, String subject, String content) { super(); this.to = to; this.subject = subject; this.content = content; } @Override public void run() { sendMail(to, subject, content); } } /** * 发送邮件 * @param to 收件人邮箱 * @param subject 邮件主题 * @param content 邮件内容 */ public void sendMail(String to, String content) { try { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "UTF-8"); messageHelper.setFrom(simpleMailMessage.getFrom()); if (subject != null) { messageHelper.setSubject(subject); } else { messageHelper.setSubject(simpleMailMessage.getSubject()); } messageHelper.setTo(to); messageHelper.setText(content, true); javaMailSender.send(message); } catch (MessagingException e) { e.printStackTrace(); } } }
@Inject private MailSender mailSender;
User user = (User)request.getSession().getAttribute("user"); String url = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+ request.getContextPath(); mailSender.send(user,url,email);
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
原文链接:https://www.f2er.com/springmvc/461408.html