Apache Commons Email使用心得
发布时间:2020-05-25 16:02:19 所属栏目:Java 来源:互联网
导读:Apache Commons Email使用心得
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 Email email = new SimpleEmail();
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username","password"));
email.setSSLOnConnect(true);
email.setFrom("[emailprotected]");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("[emailprotected]");
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("[emailprotected]","John Doe");
email.setFrom("[emailprotected]","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("[emailprotected]","John Doe");
email.setFrom("[emailprotected]","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("[emailprotected]","John Doe");
email.setFrom("[emailprotected]","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("[emailprotected]","John Doe");
email.setFrom("[emailprotected]","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 = "[emailprotected];[emailprotected]";
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("[emailprotected]","abc"));
email.setSSLOnConnect(true);
email.setFrom("[emailprotected]");
email.addTo(target);
email.setSubject("Test Mail");
email.setMsg("This is a test mail");
email.send();
}catch (Exception e){
e.printStackTrace();
}
}
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
