您的位置:首页 > 运维架构 > Apache

使用Apache Commons Email 发生邮件

2017-02-16 16:11 288 查看

 

Apache Commons Email的Maven依赖

1 <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-email -->
2 <dependency>
3     <groupId>org.apache.commons</groupId>
4     <artifactId>commons-email</artifactId>
5     <version>1.4</version>
6 </dependency>

使用示例:

1 package app.hihn.me.mail.demo;
2
3 import java.net.MalformedURLException;
4 import java.net.URL;
5
6 import org.apache.commons.mail.DefaultAuthenticator;
7 import org.apache.commons.mail.Email;
8 import org.apache.commons.mail.EmailAttachment;
9 import org.apache.commons.mail.EmailException;
10 import org.apache.commons.mail.HtmlEmail;
11 import org.apache.commons.mail.ImageHtmlEmail;
12 import org.apache.commons.mail.MultiPartEmail;
13 import org.apache.commons.mail.SimpleEmail;
14 import org.apache.commons.mail.resolver.DataSourceUrlResolver;
15
16 /**
17  *
18  * <pre>
19  * <h1>Debugging</h1>
20  * The JavaMail API supports a debugging option that will can be very
21  * useful if you run into problems. You can activate debugging on any of the
22  * mail classes by calling setDebug(true). The debugging output will be written
23  * to System.out.
24  *
25  * Sometimes you want to experiment with various security setting or features of
26  * commons-email. A good starting point is the test class EmailLiveTest and
27  * EmailConfiguration which are used for testing commons-email with real SMTP
28  * servers.
29  *
30  * </pre>
31  *
32  * @describe Apache Commons Email 使用示例
33  * @author liwen
34  *
35  */
36 public class SendEmailDemo {
37     /**
38      * <pre>
39      *
40      * Our first example will create a basic email message to "John Doe" and
41      * send it through your Google Mail (GMail) account.
42      *
43      * The call to setHostName("mail.myserver.com") sets the address of the outgoing SMTP
44      * server that will be used to send the message. If this is not set, the
45      * system property "mail.host" will be used.
46      *
47      * </pre>
48      *
49      * @describe 发送内容为简单文本的邮件
50      * @throws EmailException
51      */
52     public static void sendSimpleTextEmail() throws EmailException {
53         Email email = new SimpleEmail();
54         email.setHostName("smtp.googlemail.com");
55         email.setSmtpPort(465);
56         // 用户名和密码为邮箱的账号和密码(不需要进行base64编码)
57         email.setAuthenticator(new DefaultAuthenticator("username", "password"));
58         email.setSSLOnConnect(true);
59         email.setFrom("user@gmail.com");
60         email.setSubject("TestMail");
61         email.setMsg("This is a test mail ... :-)");
62         email.addTo("foo@bar.com");
63         email.send();
64     }
65
66     /**
67      * <pre>
68      *
69      * To add attachments to an email, you will need to use the MultiPartEmail
70      * class. This class works just like SimpleEmail except that it adds several
71      * overloaded attach() methods to add attachments to the email. You can add
72      * an unlimited number of attachments either inline or attached. The
73      * attachments will be MIME encoded.
74      *
75      * The simplest way to add the attachments is by using the EmailAttachment
76      * class to reference your attachments.
77      *
78      * In the following example, we will create an attachment for a picture. We
79      * will then attach the picture to the email and send it.
80      *
81      * </pre>
82      *
83      * @describe 发送包含附件的邮件(附件为本地资源)
84      * @throws EmailException
85      */
86     public static void sendEmailsWithAttachments() throws EmailException {
87         // Create the attachment
88         EmailAttachment attachment = new EmailAttachment();
89         attachment.setPath("mypictures/john.jpg");
90         attachment.setDisposition(EmailAttachment.ATTACHMENT);
91         attachment.setDescription("Picture of John");
92         attachment.setName("John");
93
94         // Create the email message
95         MultiPartEmail email = new MultiPartEmail();
96         email.setHostName("mail.myserver.com");
97         email.addTo("jdoe@somewhere.org", "John Doe");
98         email.setFrom("me@apache.org", "Me");
99         email.setSubject("The picture");
100         email.setMsg("Here is the picture you wanted");
101
102         // add the attachment
103         email.attach(attachment);
104
105         // send the email
106         email.send();
107     }
108
109     /**
110      * <pre>
111      *
112      * You can also use EmailAttachment to reference any valid URL for files
113      * that you do not have locally. When the message is sent, the file will be
114      * downloaded and attached to the message automatically.
115      *
116      * The next example shows how we could have sent the apache logo to John
117      * instead.
118      *
119      * </pre>
120      *
121      * @describe 发送包含附件的邮件(附件为在线资源)
122      * @throws EmailException
123      * @throws MalformedURLException
124      */
125     public static void sendEmailsWithOnlineAttachments() throws EmailException, MalformedURLException {
126         // Create the attachment
127         EmailAttachment attachment = new EmailAttachment();
128         attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
129         attachment.setDisposition(EmailAttachment.ATTACHMENT);
130         attachment.setDescription("Apache logo");
131         attachment.setName("Apache logo");
132
133         // Create the email message
134         MultiPartEmail email = new MultiPartEmail();
135         email.setHostName("mail.myserver.com");
136         email.addTo("jdoe@somewhere.org", "John Doe");
137         email.setFrom("me@apache.org", "Me");
138         email.setSubject("The logo");
139         email.setMsg("Here is Apache's logo");
140
141         // add the attachment
142         email.attach(attachment);
143
144         // send the email
145         email.send();
146     }
147
148     /**
149      * <pre>
150      *
151      * Sending HTML formatted email is accomplished by using the HtmlEmail
152      * class. This class works exactly like the MultiPartEmail class with
153      * additional methods to set the html content, alternative text content if
154      * the recipient does not support HTML email, and add inline images.
155      *
156      * In this example, we will send an email message with formatted HTML
157      * content with an inline image.
158      *
159      * First, notice that the call to embed() returns a String. This String is a
160      * randomly generated identifier that must be used to reference the image in
161      * the image tag.
162      *
163      * Next, there was no call to setMsg() in this example. The method is still
164      * available in HtmlEmail but it should not be used if you will be using
165      * inline images. Instead, the setHtmlMsg() and setTextMsg() methods were
166      * used.
167      *
168      * <pre>
169      *
170      * @describe 发送内容为HTML格式的邮件
171      * @throws EmailException
172      * @throws MalformedURLException
173      */
174     public static void sendHTMLFormattedEmail() throws EmailException, MalformedURLException {
175         // Create the email message
176         HtmlEmail email = new HtmlEmail();
177         email.setHostName("mail.myserver.com");
178         email.addTo("jdoe@somewhere.org", "John Doe");
179         email.setFrom("me@apache.org", "Me");
180         email.setSubject("Test email with inline image");
181
182         // embed the image and get the content id
183         URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
184         String cid = email.embed(url, "Apache logo");
185
186         // set the html message
187         email.setHtmlMsg("<html>The apache logo - <img src=\"cid:" + cid + "\"></html>");
188
189         // set the alternative message
190         email.setTextMsg("Your email client does not support HTML messages");
191
192         // send the email
193         email.send();
194     }
195
196     /**
197      * <pre>
198      *
199      * The previous example showed how to create a HTML email with embedded
200      * images but you need to know all images upfront which is inconvenient when
201      * using a HTML email template. The ImageHtmlEmail helps you solving this
202      * problem by converting all external images to inline images.
203      *
204      * First we create a HTML email template referencing some images. All
205      * referenced images are automatically transformed to inline images by the
206      * specified DataSourceResolver.
207      *
208      * </pre>
209      *
210      * @describe 发送内容为HTML格式的邮件(嵌入图片更方便)
211      * @throws MalformedURLException
212      * @throws EmailException
213      */
214     public static void sendHTMLFormattedEmailWithEmbeddedImages() throws MalformedURLException, EmailException {
215         // load your HTML email template
216         String htmlEmailTemplate = ".... <img src=\"http://www.apache.org/images/feather.gif\"> ....";
217
218         // define you base URL to resolve relative resource locations
219         URL url = new URL("http://www.apache.org");
220
221         // create the email message
222         ImageHtmlEmail email = new ImageHtmlEmail();
223         email.setDataSourceResolver(new DataSourceUrlResolver(url));
224         email.setHostName("mail.myserver.com");
225         email.addTo("jdoe@somewhere.org", "John Doe");
226         email.setFrom("me@apache.org", "Me");
227         email.setSubject("Test email with inline image");
228
229         // set the html message
230         email.setHtmlMsg(htmlEmailTemplate);
231
232         // set the alternative message
233         email.setTextMsg("Your email client does not support HTML messages");
234
235         // send the email
236         email.send();
237     }
238
239     public static void main(String[] args) throws EmailException {
240         Email email = new SimpleEmail();
241         email.setHostName("smtp.163.com");
242         email.setSmtpPort(465);
243         email.setAuthenticator(new DefaultAuthenticator("liwenzlw@163.com", "xxxxxxxxxxxxx"));
244         email.setSSLOnConnect(true);
245         email.setFrom("liwenzlw@163.com", "利文");
246         email.setSubject("异常信息");
247         email.setMsg("This is a test mail ... :-)");
248         email.addTo("1309928657@qq.com");
249         email.send();
250     }
251 }

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: