common email送信メール

4874 ワード

プロジェクトの住所:
http://commons.apache.org/email/userguide.html
1.簡単メール送信
  Email email = new SimpleEmail();
email.setHostName("smtp.gmail.com");
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setTLS(true);
email.setFrom("[email protected]");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("[email protected]");
email.send();
2.添付ファイル付きメール送信
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("[email protected]", "John Doe");
  email.setAuthentication("[email protected]", "password");
  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();
3.urlで自動的にダウンロードしてメールを送る
 import org.apache.commons.mail.*;
...

  // 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.setAuthentication("[email protected]", "password");
  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();
4.html形式のメールを送る
  import org.apache.commons.mail.HtmlEmail;
...

  // Create the email message
  HtmlEmail email = new HtmlEmail();
  email.setHostName("mail.myserver.com");
  email.setAuthentication("[email protected]", "password");
  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();
5.埋め込み画像を一括送信するメール
  import org.apache.commons.mail.HtmlEmail;
...

  // load your HTML email template
  String htmlEmailTemplate = ....
  
  // Create the email message
  HtmlEmail email = new ImageHtmlEmail();
  email.setHostName("mail.myserver.com");
  email.setAuthentication("[email protected]", "password");
  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(htmlEmailTemplate, new File("").toURI().toURL(), false);

  // set the alternative message
  email.setTextMsg("Your email client does not support HTML messages");

  // send the email
  email.send();