使用SpringMVC+Java mail发送HTML邮件

2015-11-29  本文已影响2534人  Kjiang

1 添加基本依赖

JavaMail

Spring framework

Apache Commons Logging

2 添加xml配置到spring配置文件中

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
     <property name="host" value="smtp.gmail.com" />
     <property name="port" value="587" />
     <property name="username" value="你的email地址" />
     <property name="password" value="密码" />
     <property name="javaMailProperties">
           <props>
              <prop key="mail.transport.protocol">smtp</prop>
              <prop key="mail.smtp.auth">true</prop>
              <prop key="mail.smtp.starttls.enable">true</prop>
          </props>
</property>
</bean>

3 发送html格式邮件

MimeMessage mimeMsg = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, false, "utf-8");
mimeMsg.setContent(htmlContent, "text/html");
helper.setTo(recipientAddress);
helper.setSubject(subject);
helper.setFrom(Constant.SITE_EMAIL);
mailSender.send(mimeMsg);
上一篇下一篇

猜你喜欢

热点阅读