添加依赖
<dependency>
<groupId>com.rick.mail</groupId>
<artifactId>sharp-mail</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
邮件服务器配置
spring:
mail: # SMTP 发邮件
host: smtp.qiye.aliyun.com
port: 25
username: XXX
password: XXX
default-encoding: UTF-8
imap: # IMAP 收邮件
host: imap.qiye.aliyun.com
port: 143
properties:
mail:
smtp:
auth: true
starttls:
enable: true
spring:
mail: # SMTP 发邮件
host: smtp.163.com
port: 25
username: jkxyx205@163.com
password: XXX # 将邮箱开启smtp服务后获取的授权码作为密码;https://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac21b8ba4d48ed49ebc
default-encoding: UTF-8
imap: # IMAP 收邮件
host: imap.163.com
port: 143
properties:
mail:
smtp:
auth: true
starttls:
enable: true
spring:
mail: # SMTP 发邮件
host: smtp.gmail.com
port: 587
username: XX
password: xxx
default-encoding: UTF-8
imap: # IMAP 收邮件
host: imap.gmail.com
port: 993
properties:
mail:
smtp:
auth: true
starttls:
enable: true
spring:
mail: # SMTP 发邮件
host: smtp.qq.com
port: 465
username: 1050205@qq.com
password: XXX
default-encoding: UTF-8
imap: # IMAP 收邮件
host: imap.qq.com
port: 993
properties:
mail:
smtp:
auth: true
starttls:
enable: true
测试
邮件操作接口
MailHandler.java
public interface MailHandler {
/**
* 发送邮件
* @param email
* @return
*/
MimeMessage send(Email email);
/**
* 发送邮件并保存到已发送
* @param email
* @return
*/
MimeMessage sendAndSaveToOutbox(Email email);
MimeMessage send(MimeMessage message);
MimeMessage sendAndSaveToOutbox(MimeMessage message);
/**
* 根据Message ID 获取邮件
* @param folderName
* @param messageId
* @return
* @throws MessagingException
*/
Optional<MimeMessage> searchByMessageId(String folderName, String messageId) throws MessagingException;
Optional<MimeMessage> searchByMessageId(Folder folder, String messageId) throws MessagingException;
/**
* 邮箱文件夹列表
* @return
* @throws MessagingException
*/
Folder[] listFolders() throws MessagingException;
/**
* 未读邮件列表
* @param consumer
* @throws MessagingException
*/
void listUnreadMessage(Consumer<MimeMessage> consumer) throws MessagingException;
/**
* 文件夹所有邮件
* @param folderName
* @param consumer true 表示终止遍历
* @throws MessagingException
*/
void listMessages(String folderName, Function<MimeMessage, Boolean> consumer) throws MessagingException;
void listMessages(Folder folder, Function<MimeMessage, Boolean> consumer) throws MessagingException;
}
测试
@SpringBootTest
public class AliyunMailTest {
@Autowired
private MailHandler mailHandler;
@Test
public void testUnReadMessages() throws MessagingException {
mailHandler.listUnreadMessage(message -> {
try {
System.out.println(message.getSubject());
System.out.println("---------");
System.out.println(MailUtils.contentText(message.getContent()));
System.out.println("----contentText end-----");
System.out.println(MailUtils.hasAttachment(message));
System.out.println("---------");
System.out.println(MailUtils.downloadAttachments(message, "/Users/rick/Documents/mail"));
System.out.println("---------");
// System.out.println(IOUtils.toString(message.getRawInputStream(), "UTF-8"));
} catch (MessagingException | IOException e) {
e.printStackTrace();
}
});
}
@Test
public void testListMessages() throws MessagingException {
mailHandler.listMessages("INBOX", message -> {
try {
System.out.println(message.getSubject());
System.out.println("---------");
System.out.println(MailUtils.contentText(message.getContent()));
System.out.println("---------");
System.out.println(MailUtils.hasAttachment(message));
System.out.println("---------");
System.out.println(MailUtils.downloadAttachments(message, "/Users/rick/Documents/mail"));
System.out.println("---------");
} catch (MessagingException | IOException e) {
e.printStackTrace();
}
return true;
});
}
@Test
public void testSearchById() throws MessagingException, IOException {
Optional<MimeMessage> optional = mailHandler.searchByMessageId("INBOX", "<475871799.1.1668661823168@[192.168.2.7]>");
System.out.println(optional.isPresent() == false);
Optional<MimeMessage> optional2= mailHandler.searchByMessageId("已发送", "<1733354369.1.1668662103782@[192.168.2.7]>");
System.out.println(optional2.get().getSubject());
}
@Test
public void testSenderSimple0() throws IOException, MessagingException {
MimeMessage message = mailHandler.send(Email.builder()
.subject("no attachment testSenderSimple0")
.from("xu.xu@yodean.com", "Rick.Xu")
.to("jkxyx205@163.com", "JIM")
.to("1050216579@qq.com")
.plainText("Here is an example to send a simple e-mail from your machine. It is assumed that your localhost is connected to the Internet and capable enough to send an e-mail.\n" +
"\n")
.build());
System.out.println(message.getMessageID());
}
@Test
public void testSenderSimple() throws IOException, MessagingException {
mailHandler.send(Email.builder()
.subject("no attachment")
.from("xu.xu@yodean.com", "Rick.Xu")
.to("jkxyx205@163.com", "JIM")
.to("1050216579@qq.com")
// .plainText("Here is an example to send a simple e-mail from your machine. It is assumed that your localhost is connected to the Internet and capable enough to send an e-mail.\n" +
// "\n")
.htmlText("hello <span style='color: red'>world</span>用户登录Webmail端企业邮箱,通过“设置>反垃圾/黑白名单”进入反垃圾/黑白名单页面。如下图所示,用户可自助设置垃圾邮件的判定规则、垃圾邮件的处理规则等。")
.build());
}
@Test
public void testSender() throws IOException, MessagingException {
mailHandler.send(Email.builder()
.subject("has attachment")
.from("xu.xu@yodean.com", "Rick.Xu")
.to("jkxyx205@163.com", "JIM")
.to("1050216579@qq.com")
// .plainText("Here is an example to send a simple e-mail from your machine. It is assumed that your localhost is connected to the Internet and capable enough to send an e-mail.\n" +
// "\n")
.htmlText("hello <img src=\"cid:learn.png\" /><span style='color: red'>world</span>用户登录Webmail端企业邮箱,通过“设置>反垃圾/黑白名单”进入反垃圾/黑白名单页面。如下图所示,用户可自助设置垃圾邮件的判定规则、垃圾邮件的处理规则等。")
.embeddedImage("learn.png", Files.readAllBytes(Paths.get("/Users/rick/Desktop/learn.png")), "image/png")
.attachment("pom_extend.txt", Files.readAllBytes(Paths.get("/Users/rick/Desktop/pom_extend.txt")), "text/plain")
.attachment("部分合作单位.html", Files.readAllBytes(Paths.get("/Users/rick/Desktop/部分合作单位.html")), "text/html")
.build());
}
@Test
public void testSendAndSaveToOutbox() throws IOException, MessagingException {
MimeMessage message = mailHandler.sendAndSaveToOutbox(Email.builder()
.subject("Java - Sending Email-testSendAndSaveToOutBox")
.from("xu.xu@yodean.com", "Rick.Xu")
.to("jkxyx205@163.com", "JIM")
.to("1050216579@qq.com")
.cc("154894898@qq.com", "JK")
// .plainText("Here is an example to send a simple e-mail from your machine. It is assumed that your localhost is connected to the Internet and capable enough to send an e-mail.\n" +
// "\n")
.htmlText("hello <img src=\"cid:learn.png\" /><span style='color: red'>world</span>用户登录Webmail端企业邮箱,通过“设置>反垃圾/黑白名单”进入反垃圾/黑白名单页面。如下图所示,用户可自助设置垃圾邮件的判定规则、垃圾邮件的处理规则等。")
.embeddedImage("learn.png", Files.readAllBytes(Paths.get("/Users/rick/Desktop/learn.png")), "image/png")
.attachment("pom_extend.txt", Files.readAllBytes(Paths.get("/Users/rick/Desktop/pom_extend.txt")), "text/plain")
.attachment("部分合作单位.html", Files.readAllBytes(Paths.get("/Users/rick/Desktop/部分合作单位.html")), "text/html")
.build());
System.out.println(message.getMessageID());
}
@Test
public void listFolder() throws MessagingException {
Folder[] folders = mailHandler.listFolders();
for (Folder folder : folders) {
System.out.println(folder.getFullName());
}
folders[2].open(Folder.READ_ONLY);
System.out.println(folders[2].getMessage(1).getSubject());
folders[2].close();
}
}