java发送邮件工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.zhangzob.util;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.simplejavamail.email.Email;
import org.simplejavamail.mailer.Mailer;
import javax.mail.Message;
import java.util.List;
import java.util.Map;

/**
* Created by zhangzb on 17-2-10.
*/

@Slf4j
@Data
public class SendEmailUtil {
/**
* @param map 要发送的消息
* @param toUser 收件人邮箱
*/
public static void sendEmail(Map<String, List<String>> map, String toUser){


StringBuilder stringBuilder = new StringBuilder();
for(String value : map.keySet()){
stringBuilder.append(value);
stringBuilder.append(":");
stringBuilder.append(map.get(value).toString());
stringBuilder.append("\n");
}

Email email = new Email();

email.setFromAddress("apiTest", "[email protected]");
email.addRecipient("", toUser, Message.RecipientType.TO);
email.setSubject("测试结果");
email.setText(stringBuilder.toString());

new Mailer("smtp.163.com", 25, "[email protected]", "yourPassword").sendMail(email);
log.info("发送成功!");
}
}
觉得有帮助就赞赏一下吧
0%