您的位置:首页 > 编程语言 > Java开发

使用Springboot Email实现邮件发送

2017-02-27 09:17 1011 查看
在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);
}

}
就可以发送了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: