1 添加基本依賴
JavaMail
Spring framework
- 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
Apache Commons Logging
- 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 發(fā)送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);
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者