使用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("[email protected]");//发送者
        message.setTo("[email protected]");//接收者
        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("[email protected]");//发送者
    helper.setTo("[email protected]");
    helper.setSubject("测试主题");//邮件主题
 
    //
    //测试内嵌图片 #####切记 src里的CID必须和addInline的第一个参数一致否则不显示并会变为错误附件
    String includ="这是图片:";
    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);
}
 
}
就可以发送了
 
 

转载于:https://www.cnblogs.com/345214483-qq/p/6472032.html

你可能感兴趣的:(java,数据库,json)