Apache Commons Email使用心得
最近在項目中開發中需要用到發送郵件功能,當后臺定時任務處理完畢后通知調用者。Java Mail API使用比較麻煩,所以這里采用的是Apache Commons Email,官網地址:http://commons.apache.org/proper/commons-email/,Commons Email API比較簡潔高效,學習起來也很快。寫篇文章跟大家分享一下,有問題的可以留言!
1、發送簡單文本郵件
Email email = new SimpleEmail(); email.setHostName("smtp.googlemail.com"); email.setSmtpPort(465); email.setAuthenticator(new DefaultAuthenticator("username", "password")); email.setSSLOnConnect(true); email.setFrom("user@gmail.com"); email.setSubject("TestMail"); email.setMsg("This is a test mail ... :-)"); email.addTo("foo@bar.com"); email.send();
2、發送帶附件的郵件
// Create the attachment EmailAttachment attachment = new EmailAttachment(); attachment.setPath("mypictures/john.jpg"); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription("Picture of John"); attachment.setName("John"); // Create the email message MultiPartEmail email = new MultiPartEmail(); email.setHostName("mail.myserver.com"); email.addTo("jdoe@somewhere.org", "John Doe"); email.setFrom("me@apache.org", "Me"); email.setSubject("The picture"); email.setMsg("Here is the picture you wanted"); // add the attachment email.attach(attachment); // send the email email.send();
另外還可以通過任意的鏈接來將網絡上的文件添加到附件中,例如:
// Create the attachment EmailAttachment attachment = new EmailAttachment(); attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif")); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription("Apache logo"); attachment.setName("Apache logo"); // Create the email message MultiPartEmail email = new MultiPartEmail(); email.setHostName("mail.myserver.com"); email.addTo("jdoe@somewhere.org", "John Doe"); email.setFrom("me@apache.org", "Me"); email.setSubject("The logo"); email.setMsg("Here is Apache's logo"); // add the attachment email.attach(attachment); // send the email email.send();
3、發送HTML格式的郵件
// Create the email message HtmlEmail email = new HtmlEmail(); email.setHostName("mail.myserver.com"); email.addTo("jdoe@somewhere.org", "John Doe"); email.setFrom("me@apache.org", "Me"); email.setSubject("Test email with inline image"); // embed the image and get the content id URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif"); String cid = email.embed(url, "Apache logo"); // set the html message email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>"); // set the alternative message email.setTextMsg("Your email client does not support HTML messages"); // send the email email.send();
4、發送帶圖片的HTML格式郵件
// load your HTML email template String htmlEmailTemplate = .... // define you base URL to resolve relative resource locations URL url = new URL("http://www.apache.org"); // create the email message HtmlEmail email = new ImageHtmlEmail(); email.setDataSourceResolver(new DataSourceResolverImpl(url)); email.setHostName("mail.myserver.com"); email.addTo("jdoe@somewhere.org", "John Doe"); email.setFrom("me@apache.org", "Me"); email.setSubject("Test email with inline image"); // set the html message email.setHtmlMsg(htmlEmailTemplate); // set the alternative message email.setTextMsg("Your email client does not support HTML messages"); // send the email email.send();
另外,在使用過程中發現Email.addTo一次只能添加一個聯系人,如果想發送給多個人的話,需要使用for循環嵌套來實現,以下是一個簡單的例子:
public static void main(String[] args){ String mailList = "abc@163.com;tt@qq.com"; String[] list = mailList.split(";"); for(int i=0;list!=null && i<list.length;i++){ //嵌套調用 sendEmail(list[i]); } } public static void sendEmail(String target) { try{ Email email = new SimpleEmail(); email.setHostName("smtp.163.com"); email.setSmtpPort(465); email.setAuthenticator(new DefaultAuthenticator("abc@163.com","abc")); email.setSSLOnConnect(true); email.setFrom("abc@163.com"); email.addTo(target); email.setSubject("Test Mail"); email.setMsg("This is a test mail"); email.send(); }catch (Exception e){ e.printStackTrace(); } }
詳細教程可以參考官網UserGuide,鏈接:http://commons.apache.org/proper/commons-email/userguide.html
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!