发送邮件apace mail

原文:http://www.open-open.com/lib/view/open1410621206648.html

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();
        }
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Django 官方文档 发送email 官网地址:[https://docs.djangoproject.com/...
    学以致用123阅读 6,833评论 0 2
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,798评论 19 139
  • 从古代的八百里加急,到现在的电子邮件,邮件的发展见证了上下五千年的发展史,这些当然是废话,只是要说说邮件的重要性。...
    大牧莫邪阅读 5,477评论 0 13
  • 前言 在进行日常的自动化测试实践中,我们总是需要将测试过程中的记录、结果等等等相关信息通过自动的手段发送给相关人员...
    苦叶子阅读 3,954评论 0 5
  • 背景 公司内网有个论坛,各种公司的前沿消息都会有人在讨论。一忙起来,经常忘记逛论坛,所以写了个爬虫,爬取论坛前10...
    aialin阅读 4,449评论 0 11