Apache Common Email的几个例子

Apache Common Email的几个例子

最近用到了发邮件,于是就看到了JavaMail和Apache Common Email,先开始试的是JavaMail,后来发现一个不知道认证失败的错误,于是干脆就直接使用Common Email,觉得还是挺简单容易使用的。一般只用第一种,记录下来。以后有需要了再来看

  1. 最简单的Text mail

    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. Sending emails with attachments

    import org.apache.commons.mail.*;
    ...
    
    // 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();

    Attachment 也可以指向一个合法的url,发送时就会把这个url内容给download下来进行发送.

  3. Sending HTML formatted email

    import org.apache.commons.mail.HtmlEmail;
    ...
    
    // 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. Sending HTML formatted email with embedded images

    import org.apache.commons.mail.HtmlEmail;
    ...
    
    // load your HTML email template
    String htmlEmailTemplate = ".... <img src=\"http://www.apache.org/images/feather.gif\"> ....";
    
    // define you base URL to resolve relative resource locations
    URL url = new URL("http://www.apache.org");
    
    // create the email message
    ImageHtmlEmail email = new ImageHtmlEmail();
    email.setDataSourceResolver(new DataSourceUrlResolver(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(); 

    PS:
    在本地使用common mail的时候没有任何问题,但是放到了服务器不知道为什么就一直报connection reset,通过telnet手动发stmp的命令已经成功发送了邮件,无奈只能换回了JavaMail,没有出错。可能是某一项没有设置导致的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值