JavaMail--发送html邮件
生活随笔
收集整理的這篇文章主要介紹了
JavaMail--发送html邮件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//發郵件用到的兩個包
"org.springframework:spring-context-support:$springVersion",
"javax.mail:mail:1.4.7"
如果想添加附件的話,只需添加以下代碼即可
---------------------------------------------------------
//加載文件資源,作為附件] FileSystemResource file = new FileSystemResource(new File("E:\\dd/2.png"));
//加入附件
helper.addAttachment(file.getFilename(), file); -----------------------------------------------------------------------
mailSender.send(msg);//發送*
}@Testpublic void test() throws MessagingException {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("Spring_mvc.xml");mailSender = (JavaMailSender) ctx.getBean("mailSender");sendMailDemo();};}
"org.springframework:spring-context-support:$springVersion",
"javax.mail:mail:1.4.7"
?
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1"><servlet><servlet-name>mvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:Spring_mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>mvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping> </web-app>?
Spring_mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 開啟mvc--><mvc:annotation-driven/><!-- 配置掃描發現所有具有 @Controller 注解的類,加載到容器 --><context:component-scan base-package="text"/><bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"><property name="host" value="smtp.qq.com" /><!--設置郵件服務主機--><property name="username" value="8962xxx@qq.com" /><!--/發送者郵箱的用戶名 --><property name="password" value="密碼" /><!--//發送者郵箱的密碼--><property name="port" value="25" /><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><prop key="mail.debug">true</prop></props></property></bean></beans>?
UserService.java
package text;import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service;import javax.annotation.Resource; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage;@Service public class UserService {@Resourceprivate JavaMailSender mailSender;//,mailSender找Spring_mvc.xml的id的public void sendMailDemo() throws MessagingException { String html="<html>" +"<p>我HTML哦</p>" +"</html>";MimeMessage msg = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(msg,true,"utf-8");//SimpleMailMessage mailMsg = new SimpleMailMessage();//只支持文字helper.setFrom("89@qq.com");//發件人helper.setTo("110@qq.com");//收件人helper.setSubject("很棒哦,今晚你回去掃地了");//郵件標題 helper.setText(html,true); //測試內容(html)如果想添加附件的話,只需添加以下代碼即可
---------------------------------------------------------
//加載文件資源,作為附件] FileSystemResource file = new FileSystemResource(new File("E:\\dd/2.png"));
//加入附件
helper.addAttachment(file.getFilename(), file); -----------------------------------------------------------------------
mailSender.send(msg);//發送*
}@Testpublic void test() throws MessagingException {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("Spring_mvc.xml");mailSender = (JavaMailSender) ctx.getBean("mailSender");sendMailDemo();};}
?
?
?
轉載于:https://www.cnblogs.com/896240130Master/p/6435708.html
總結
以上是生活随笔為你收集整理的JavaMail--发送html邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微pe工具箱是微软的吗_微PE:装机不求
- 下一篇: 设备树和pinctrl粗解