首页 > 代码库 > java SpringMVC 邮件发送配置及Code
java SpringMVC 邮件发送配置及Code
首先是spring中的配置:(我里面使用了FreeMarker模板引擎)
<!--邮件发送实现类--> <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="http://www.mamicode.com/smtp.qq.com"/> <property name="port" value="http://www.mamicode.com/25"/> <property name="username" value="http://www.mamicode.com/你的邮箱"/> <property name="password" value="http://www.mamicode.com/你的邮箱密码"/> <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="http://www.mamicode.com/发件人" /> <property name="subject" value="http://www.mamicode.com/邮件主题" /> <!-- <property name="text" value="http://www.mamicode.com/邮件内容" /> --> </bean> <!--线程池异步发送邮件--> <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="http://www.mamicode.com/5" /> <property name="maxPoolSize" value="http://www.mamicode.com/10" /> <property name="queueCapacity" value="http://www.mamicode.com/25" /> </bean> <!--FreeMarker模板--> <bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="http://www.mamicode.com/classpath:template" /> <!--<property name="templateLoaderPath" value="http://www.mamicode.com/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 subject, 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(); } } }
服务层调用:
1、要注入发送邮件工具类
@Inject private MailSender mailSender;
2、调用代码
User user = (User)request.getSession().getAttribute("user"); String url = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+ request.getContextPath(); mailSender.send(user,url,email);
java SpringMVC 邮件发送配置及Code
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。