准备步骤
首先需要获取mail server的客户端授权码。
本例中使用的是搜狐邮箱,登录邮箱,点击“选项” -> “设置”:
在设置页面,点击 POP3/SMTP/IMAP
:
选中 POP3/SMTP/IMAP
,获取第三方客户端独立密码:
注:授权码是10个字符组成的一个字符串。
代码
set up project
新建Project “testmail”,在依赖关系中,选中“Web -> Spring Web”和“I/O -> Java Mail Sender”
在 application.properties
文件中添加如下代码:
spring.mail.host=smtp.sohu.com
spring.mail.username=duke_ding@sohu.com
spring.mail.password=<客户端授权码>
spring.mail.default-encoding=UTF-8
简单mail
Service 代码:
package com.example.testmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
@Service
public class TestMailServiceImpl implements TestMailServiceIF{
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
@Override
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setFrom(from);
message.setSubject(subject);
message.setText(content);
mailSender.send(message);
}
}
注: TestMailServiceIF
是自己定义的接口。
Controller代码:
package com.example.testmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestMailController {
@Autowired
TestMailServiceIF service;
@RequestMapping(value = "/sendsimplemail", method = RequestMethod.POST)
public void sendSimpleMail(String to, String subject, String content) {
service.sendSimpleMail(to, subject, content);
}
}
运行:
➜ ~ curl --silent --write-out '\n%{http_code}\n' -XPOST "http://localhost:8080/sendsimplemail?to=duke_ding@sohu.com&subject=testmail&content=hello" | jq .
200
搜狐邮箱,收到mail如下:
HTML mail
Service 代码:
@Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage message = mailSender.createMimeMessage();
String str = "<html><body><h1>" + content + "</h1></body></html>";
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(str, true);
} catch (MessagingException e) {
e.printStackTrace();
}
mailSender.send(message);
}
Controller代码:
@RequestMapping(value = "/sendhtmlmail", method = RequestMethod.POST)
public void sendHtmlMail(String to, String subject, String content) {
service.sendHtmlMail(to, subject, content);
}
运行:
➜ ~ curl --silent --write-out '\n%{http_code}\n' -XPOST "http://localhost:8080/sendhtmlmail?to=duke_ding@sohu.com&subject=testhtmlmail&content=hello_html" | jq .
200
搜狐邮箱,收到mail如下:
带图片的mail
Service代码:
@Override
public void sendPictureMail(String to, String subject, String content, String resPath, String resId) {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
String str = "<html><body>" + content + "<br><img width='250px' src=\'cid:" + resId + "\'></body></html>";
helper.setText(str, true);
File file = new File(resPath);
FileSystemResource res = new FileSystemResource(file);
helper.addInline(resId, res);
} catch (MessagingException e) {
e.printStackTrace();
}
mailSender.send(message);
}
Controller代码:
@RequestMapping(value = "/sendpicturemail", method = RequestMethod.POST)
public void sendPictureMail(String to, String subject, String content, String respath, String resid) {
service.sendPictureMail(to, subject, content, respath, resid);
}
运行:
➜ ~ curl --silent --write-out '\n%{http_code}\n' -XPOST "http://localhost:8080/sendpicturemail?to=duke_ding@sohu.com&subject=testpicturemail&content=hello&respath=/home/ding/temp/spring.jpg&resid=aaa" | jq .
200
搜狐邮箱,收到mail如下:
带附件的mail
Service代码:
@Override
public void sendAttachmentMail(String to, String subject, String content, String resPath) {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(resPath));
String fileName = file.getFilename();
helper.addAttachment(fileName, file);
} catch (MessagingException e) {
e.printStackTrace();
}
mailSender.send(message);
}
Controller代码:
@RequestMapping(value = "/sendattachmentmail", method = RequestMethod.POST)
public void sendAttachmentMail(String to, String subject, String content, String respath) {
service.sendAttachmentMail(to, subject, content, respath);
}
运行:
➜ ~ curl --silent --write-out '\n%{http_code}\n' -XPOST "http://localhost:8080/sendattachmentmail?to=duke_ding@sohu.com&subject=testattachmentmail&content=hello_attachment&respath=/home/ding/temp/aaa.txt" | jq .
200
搜狐邮箱,收到mail如下: