<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
spring.mail.port=25 # SMTP server port spring.mail.username= # Login used for authentication spring.mail.password= # Password for the given login spring.mail.protocol=smtp spring.mail.defaultEncoding=UTF-8 # Default message encoding
spring.mail.host = smtp.gmail.com spring.mail.username = *****@gmail.com spring.mail.password = **** spring.mail.properties.mail.smtp.auth = true spring.mail.properties.mail.smtp.socketFactory.port = 587 spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory spring.mail.properties.mail.smtp.socketFactory.fallback = false
@Service
public class MailClient {
private JavaMailSender mailSender;
@Autowired
public MailService(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void prepareAndSend(String recipient, String message) {
//TODO implement
}
}
public void prepareAndSend(String recipient, String message) {
MimeMessagePreparator messagePreparator = mimeMessage -> {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
messageHelper.setFrom("sample@dolszewski.com");
messageHelper.setTo(recipient);
messageHelper.setSubject("Sample mail subject");
messageHelper.setText(message);
};
try {
mailSender.send(messagePreparator);
} catch (MailException e) {
// runtime exception; compiler will not force you to handle it
}
}
<dependency> <groupId>com.icegreen</groupId> <artifactId>greenmail</artifactId> <version>1.5.0</version> <scope>test</scope> </dependency>
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class MailClientTest {
private GreenMail smtpServer;
@Before
public void setUp() throws Exception {
smtpServer = new GreenMail(new ServerSetup(25, null, "smtp"));
smtpServer.start();
}
@After
public void tearDown() throws Exception {
smtpServer.stop();
}
}
@Autowired
private MailClient mailClient;
@Test
public void shouldSendMail() throws Exception {
//given
String recipient = "name@hotmail.com";
String message = "Test message content";
//when
mailClient.prepareAndSend(recipient, message);
//then
assertReceivedMessageContains(message);
}
private void assertReceivedMessageContains(String expected) throws IOException, MessagingException {
MimeMessage[] receivedMessages = smtpServer.getReceivedMessages();
assertEquals(1, receivedMessages.length);
String content = (String) receivedMessages[0].getContent();
assertTrue(content.contains(expected));
}
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
@Service
public class MailContentBuilder {
private TemplateEngine templateEngine;
@Autowired
public MailContentBuilder(TemplateEngine templateEngine) {
this.templateEngine = templateEngine;
}
public String build(String message) {
Context context = new Context();
context.setVariable("message", message);
return templateEngine.process("mailTemplate", context);
}
}
@Test
public void shouldSendMail() throws Exception {
//given
String recipient = "name@dolszewski.com";
String message = "Test message content";
//when
mailService.prepareAndSend(recipient, message);
//then
String content = "<span>" + message + "</span>";
assertReceivedMessageContains(content);
}
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Siemens Sinnovation</title>
<style>
.button {
background-color: #4CAF50;
border-radius: 12px;
border: none;
color: white;
padding: 10px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 18px;
margin: 4px 2px;
cursor: pointer;
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
.button:hover {
box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 50px 0 rgba(0, 0, 0, 0.19);
}
</style>
</head>
<body style="margin: 0;padding: 0;">
<table align="center" border="1" cellpadding="0" cellspacing="0" width="600px">
<tr>
<td>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="600"
style="border-collapse: collapse;">
<tr>
<td align="center" style="padding: 40px 0 30px 0;">
<!---->

</td>
</tr>
<tr>
<td bgcolor="#ffffff" style="padding: 20px 30px 20px 30px">
<h4>The following message was created by <span th:text="${owner.getName()}"></span> in the
Siemens DFFA group: </h4>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><span th:text="${title}">title</span></td>
</tr>
<tr>
<td style="padding: 20px 0 30px 0">
<span th:text="${description}">description</span>
</td>
</tr>
<tr>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<!--<td align="center" style="padding: 5px 0 3px 0">Status:<span-->
<!--th:text="${status}">status</span></td>-->
<!--<td align="center" style="padding: 5px 0 3px 0">Date submitted: <span-->
<!--th:text="${createDate}">createDate</span></td>-->
<!--<td align="center" style="padding: 5px 0 3px 0">Days left to join:<span-->
<!--th:text="${leftTime}">leftTime</span></td>-->
<td align="center" style="padding: 5px 0 3px 0">Status:<span
th:text="${status}"> OPEN FOR JOINING</span></td>
<td align="center" style="padding: 5px 0 3px 0">Date submitted: 28/08/2017 <span
th:text="${createDate}">createDate</span></td>
<td align="center" style="padding: 5px 0 3px 0">Days left to join: 10h<span
th:text="${leftTime}">leftTime</span></td>
</tr>
</table>
</tr>
<tr>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td style="padding: 40px 0px 10px 0px">
Team Member:
</td>
</tr>
<tr th:each="member :${members}">
<td style="padding: 5px 0px 5px 0px"><span
th:text="${member.getName()}+', Email: '+${member.getEmail()}"></span>
</td>
</tr>
</table>
</tr>
<tr>
<td align="center" style="padding: 5px 40px 5px 40px">
<button class="button">View Details</button>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有