Apache Commons Email使用心得
发布时间:2020-12-15 03:19:09 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 Email email = new SimpleEmail();email.setHostName("smtp.googlemail.com");email.setSmtpPort(465);email.setAuthenticator(new DefaultAuthentica
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 Email email = new SimpleEmail(); email.setHostName("smtp.googlemail.com"); email.setSmtpPort(465); email.setAuthenticator(new DefaultAuthenticator("username","password")); email.setSSLOnConnect(true); email.setFrom("[email?protected]"); email.setSubject("TestMail"); email.setMsg("This is a test mail ... :-)"); email.addTo("[email?protected]"); 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("[email?protected]","John Doe"); email.setFrom("[email?protected]","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("[email?protected]","John Doe"); email.setFrom("[email?protected]","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("[email?protected]","John Doe"); email.setFrom("[email?protected]","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(); // 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("[email?protected]","John Doe"); email.setFrom("[email?protected]","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(); public static void main(String[] args){ String mailList = "[email?protected];[email?protected]"; 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("[email?protected]","abc")); email.setSSLOnConnect(true); email.setFrom("[email?protected]"); email.addTo(target); email.setSubject("Test Mail"); email.setMsg("This is a test mail"); email.send(); }catch (Exception e){ e.printStackTrace(); } } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Java中的接口知识汇总
- java – 为什么FileSystemXmlApplicationContext会对存在的
- 将Java对象添加到数据库
- JSP JSTL <fmt:parseDate>标签:转换为时间
- java – 为什么j.u.c.CopyOnWriteArrayList在方法中创建局部
- java – InterruptedException取消文件打开对话框 – 1.6.0
- Java IO编程——File文件操作类
- java – 在Google Drive API v3中等效的File.setTitle方法
- java – 多个枚举与一个枚举
- 详谈Java 异常处理的误区和经验总结(分享)