simpleemail

simpleEmail实现邮件发送

需要的包

1
2
commons-email-1.4
mail

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package org.mail;
*
* @author 吕云鹏
*
*/
public interface {
* 发送邮件
*
* @param from
* 发件人
* @param to
* 收件人
* @param title
* 标题
* @param msg
* 正文
*/
public void sendMail(String from, String to, String title, String msg) throws Exception;
}
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
package org.mail;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
*
* @author 吕云鹏
*
*/
public class MailServiceImpl implements {
private String host = "smtp.163.com";
private String username = "****";
private String password = "****";
public void sendMail(String from, String to, String title, String msg)
throws EmailException {
SimpleEmail email = new SimpleEmail();
email.setHostName(host); // 设置邮件服务
email.setAuthentication(username, password);// 登陆邮件服务
email.setCharset("UTF-8");
//email.setFrom(from, "ibm_test");
email.setFrom(from);
email.addTo(to);
email.setSubject(title);// 设置标题
email.setMsg(msg);// 设置正文
email.send();
}
public static void main(String[] args) {
MailServiceImpl impl = new MailServiceImpl();
try {
impl.sendMail("***", "***", "about workflow", "请完成工作任务");
System.out.println("邮件发送成功");
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}