Skip to content

Instantly share code, notes, and snippets.

@mloza

mloza/HtmlMailService.java

Last active Dec 15, 2019
Embed
What would you like to do?
Przykłady kodu do wpisu o Spring Mail znajdującego się pod adresem: https://blog.mloza.pl/spring-mail-spring-boot-latwe-wysylanie-maili-z-aplikacji-w-javie/
<h1>Cześć {{ username }}</h1>
<p>To jest przykładowy e-mail!</p>
<p>
<a href="https://blog.mloza.pl" target="_blank" style="display: inline-block; color: #ffffff; background-color: #3498db;
border: solid 1px #3498db; border-radius: 5px; box-sizing: border-box; cursor: pointer; text-decoration: none;
font-size: 14px; font-weight: bold; margin: 0 auto; padding: 12px 25px; border-color: #3498db;">Podoba mi się!</a>
</p>
--<br>
Pozdrawiam!<br>
Michał z blog.mloza.pl
public void sendHtmlEmail(String to, String subject, String content) {
MimeMessage mail = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mail, true);
helper.setTo(to);
helper.setFrom("Blog example <from@email.com>");
helper.setSubject(subject);
helper.setText(content, true);
} catch (MessagingException e) {
e.printStackTrace();
}
javaMailSender.send(mail);
}
@RestController
public class MailController {
private MailService mailService;
public MailController(MailService mailService) {
this.mailService = mailService;
}
@GetMapping("/sendemail")
public String sendEmail() {
mailService.sendSimpleEmail("Odbiorca <odbiorca@maila.pl>", "Test e-mail", "Testing email functionality");
return "E-mail sent!";
}
}
@Service
public class MailService {
private JavaMailSender javaMailSender; // 1
public MailService(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
public void sendSimpleEmail(String to, String subject, String content) {
SimpleMailMessage msg = new SimpleMailMessage();
msg.setTo(to);
msg.setFrom("Blog Example <from@email.com>");
msg.setSubject(subject);
msg.setText(content);
javaMailSender.send(msg);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pl.mloza</groupId>
<artifactId>spring-mail</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<type>pom</type>
<version>2.1.6.RELEASE</version>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<source>10</source>
<target>10</target>
<release>10</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
<dependency>
<groupId>org.jtwig</groupId>
<artifactId>jtwig-core</artifactId>
<version>5.87.0.RELEASE</version>
</dependency>
@GetMapping("/sendhtmlemail")
public String sendHtmlEmail() {
JtwigTemplate emailTemplate = JtwigTemplate.classpathTemplate("email/htmlEmail.twig");
JtwigModel model = JtwigModel.newModel()
.with("username", "Michał");
String emailMessage = emailTemplate.render(model);
mailService.sendHtmlEmail("Michal <michal@mloza.pl>", "Test e-mail", emailMessage);
return "E-mail sent!";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.