使用Springboot Email實現郵件發送

在springboot配置文件增長emai配置(此種方式不支持QQ郵箱):
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
 
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password=root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
 
 
 
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
 
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
#######server
server.port=80
########����ģʽ
dev=true
 
#####   json������ںʱ������
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
 
spring.jackson.serialization-inclusion=NON_NULL
 
########################################################
###mail setting
# 設置郵箱主機
spring.mail.host= smtp.mxhichina.com
spring.mail.port=465
# 設置用戶名
spring.mail.username=
spring.mail.password=
spring.mail.properties.mail.smtp.auth=true
#當SMTP須要SSL驗證時,須要設定,若是不設定,會出現以下異常
spring.mail.properties.mail.smtp.starttls.enable=true
#spring.mail.properties.mail.smtp.starttls.required=true
#spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
########################################################
 
 
 
而後寫個測試發送類:
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestSendEmai {
    @Autowired
    private JavaMailSender javaMailSender;
 
    @Test
    public void sendSimpleEmail(){
        SimpleMailMessage message=new SimpleMailMessage();
        message.setFrom("test@lebaidai.com");//發送者
        message.setTo("test@qq.com");//接收者
        message.setSubject("測試郵件主題");//郵件主題
        message.setText("測試郵件內容");//郵件內容
        javaMailSender.send(message);
    }
   /**
* 測試發送附件
*/
@Test
public void sendEmailWithAttachment() throws MessagingException {
    //這個是javax.mail.internet.MimeMessage下的
    MimeMessage mimeMessage=javaMailSender.createMimeMessage();
    MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
    helper.setFrom("sender@test.com");//發送者
    helper.setTo("recipient@test.com");
    helper.setSubject("測試主題");//郵件主題
 
    //////////////////////////////////////////////////////////////////////////
    //測試內嵌圖片 #####切記 src裏的CID必須和addInline的第一個參數一致不然不顯示並會變爲錯誤附件
    String includ="<body>這是圖片:<img src='cid:noticeHead'/></body>";
    helper.setText("測試內容,內容有附件"+includ,true);//郵件內容
    File file2=new File("C:\\Users\\zzl\\Desktop\\QQ圖片20161012123335.png");
    helper.addInline("noticeHead",file2);
    /////////////////////////////////////////////////////////////////////////
 
    //附件測試一張圖片
    File file=new File("C:\\Users\\zzl\\Desktop\\QQ圖片20160905160651.gif");
    //FileSystemResource fileSystemResource=new FileSystemResource(file);
    //添加附件
    helper.addAttachment("美女.gif",file);
    File file1=new File("C:\\Users\\zzl\\Desktop\\QQ圖片20160815112154.jpg");
    helper.addAttachment("美女1.jpg",file1);
    //////////////////////////////////////////////////////////////////////////////////
    javaMailSender.send(mimeMessage);
}
 
}
就能夠發送了
相關文章
相關標籤/搜索