?????塢???Apache Mail???????

 

1 public class ApacheMailTest {
2     // smtp??????
3     private String hostName = "smtp.qq.com";
4     // ?????????
5     private String userName = "779554589";
6     private String password = "?????????";
7     // ??????
8     private String fromAddress = "779554589@qq.com";
9     // ??????????
10     private String fromName = "loadfate";
11
12     public static void main(String[] args) throws Exception {
13         // ????????????????
14         String toAddress = "loadfate@163.com";
15         String toName = "loadfate";
16         ApacheMailTest test = new ApacheMailTest();
17         // ???е??????????????????
18
19         test.sendSimpleEmail(toAddress?? toName);
20         test.sendHtmlEmail(toAddress?? toName);
21         test.sendMultiPartEmail(toAddress?? toName);
22         System.out.println("???????");
23     }
24
25     // ????????????????????
26     public void sendSimpleEmail(String toAddress?? String toName) throws Exception {
27         SimpleEmail email = new SimpleEmail();
28         email.setHostName(hostName);// ????smtp??????
29         email.setAuthentication(userName?? password);// ??????????
30         email.setCharset("utf-8");
31         email.setFrom(fromAddress?? fromName?? "utf-8");// ???÷????????
32         email.addTo(toAddress?? toName?? "utf-8");// ????????????
33         email.setSubject("????????");// ????????
34         email.setMsg("???????????????");// ???????????
35         email.send();// ???????
36     }
37
38     // ????Html????????
39     public void sendHtmlEmail(String toAddress?? String toName) throws Exception {
40         HtmlEmail email = new HtmlEmail();
41         email.setHostName(hostName);
42         email.setAuthentication(userName?? password);
43         email.setCharset("utf-8");
44         email.addTo(toAddress?? toName?? "utf-8");
45         email.setFrom(fromAddress?? fromName?? "utf-8");
46         email.setSubject("???????html???");
47         // ????html????????????????????????д???html????
48         email.setHtmlMsg("<div style='width:100px;height:200px;'>a</div>");
49         email.send();
50
51     }
52
53     // ???????????????????????
54     public void sendMultiPartEmail(String toAddress?? String toName) throws Exception {
55         MultiPartEmail email = null;
56         email = new MultiPartEmail();
57         email.setHostName(hostName);
58         email.setAuthentication(userName?? password);
59         email.setCharset("utf-8");
60         email.addTo(toAddress?? toName?? "utf-8");
61         email.setFrom(fromAddress?? fromName?? "utf-8");
62         email.setSubject("?????и????????");
63         email.setMsg("<a href='#'>????????</a>");
64
65         // ??????????????
66         EmailAttachment attachment = new EmailAttachment();
67         attachment.setPath("D:\???.txt");// ???????
68         // attachment.setURL(new URL("http://xxx/a.gif"));//??????
69         attachment.setDisposition(EmailAttachment.ATTACHMENT);
70         attachment.setDescription("???????");
71         // ????????????????????????????????????
72         attachment.setName(MimeUtility.encodeText("???.txt"));
73         // ???????????????
74         email.attach(attachment);
75         email.send();
76     }
77 }

????????????
????????????http://pan.baidu.com/s/1qW8rcAw
????????Ч?????LZ
????????????
????1??maildemo.zip :maildemo???????
????2??mail.jar :Java Mail??jar??
????3??commons-email-1.3.1.jar :Apache Mail??jar??