使用SpringMVC+Java mail发送HTML邮件
2015-11-29 本文已影响2534人
Kjiang
1 添加基本依赖
- mail.jar
- spring-beans-3.2.0.RELEASE.jar
- spring-context-3.2.0.RELEASE.jar
- spring-context-support-3.2.0.RELEASE.jar
- spring-core-3.2.0.RELEASE.jar
- spring-expression-3.2.0.RELEASE.jar
- spring-web-3.2.0.RELEASE.jar
- spring-webmvc-3.2.0.RELEASE.jar
- commons-logging-1.1.1.jar
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);