美文网首页
发送邮件apace mail

发送邮件apace mail

作者: LH_0811 | 来源:发表于2017-06-06 15:11 被阅读18次

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

相关文章

  • 发送邮件apace mail

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

  • python电子邮件系列(二)之SMTP发送邮件

    邮件发送与接收的流程 SMTP是发送邮件的协议,是MUA(Mail User Agent)发送到MTA(Mail ...

  • Flask-Mail 发送邮件

    Flask 发送邮件 @(Flask)[Flask开发|Flask-Mail] 使用flask-mail扩展发送电...

  • email

    1. django 发送邮件 settings.py中配置发送邮件邮箱 使用send_mail方法发送邮件

  • python3之发送和读取邮件

    一、发送邮件 发送邮件使用SMTP协议【Simple Mail Transfer Protocol简单的邮件传输协...

  • Python3收发邮件(smtplib、email、poplib

    一、发送邮件 发送邮件使用SMTP协议【Simple Mail Transfer Protocol简单的邮件传输协...

  • Mail发送邮件

    本来是想用阿里云服务器测试发送邮箱,可是由于阿里云端口等问题,无奈只好在本地搭建虚拟机进行测试。本文是通过163邮...

  • 2020-03-02 邮件提醒

    1. 发送邮件 目前发送邮件的协议是SMTP(Simple Mail Transfer Protocol,简单邮件...

  • java发送邮件

    使用java mail forAndroid实现发送邮件 1.邮件发送协议smtp协议 smtp用户连接上邮件服务...

  • 2018-07-17

    发送邮件 django中内置了邮件发送功能,被定义在django.core.mail模块中,发送邮件需要使用SMT...

网友评论

      本文标题:发送邮件apace mail

      本文链接:https://www.haomeiwen.com/subject/qkogfxtx.html