不信网恋 发表于 2017-1-5 10:04:42

Java发送邮件之Apache Commons Email

  java Email 发送
  Apache Commons Email是对 JavaMail进行封装从而简化其操作。
  import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;
  /**     
 * @{#} 类名称: EmailUtil.java
 *    创建时间: Aug 24, 2009 2:04:35 PM     
 *         
 */
  /**     
 * @作者 <a href="http://qing393260529.iteye.com">昨夜风</a>    
 * @版本 1.0     
 */
  public class EmailUtil {
   /**
  * 简单 邮件发送
  *
  * 方法名称:sendEmail
  * 作者:黎青春
  * 创建日期:Aug 24, 2009
  * 方法描述: 
  * @throws Exception void
  */
 public static void sendEmail()throws Exception{
 
  SimpleEmail email = new SimpleEmail();
  email.setHostName("smtp.sina.com");
  //  邮箱 用户名 密码
  email.setAuthentication("用户名", "密码");
     //  邮件 发送 编码 格式
  email.setCharset("utf-8");
  //  收件人地址。。。
  email.addTo("收件人地址");
  //  发件人地址。。。
  email.setFrom("发件人地址");
  //  邮件标题
  email.setSubject("emai 发送测试");
  //  邮件发送内容
  email.setMsg("email --> 发送!");
  email.send();
  
 }
 
 /**
  * 附件 邮件发送
  *
  * 方法名称:AnnexEmailSend
  * 作者:黎青春
  * 创建日期:Aug 24, 2009
  * 方法描述: 
  * @throws Exception void
  */
 public static void AnnexEmailSend() throws Exception{
  
  MultiPartEmail  email = new MultiPartEmail ();
  email.setHostName("smtp.sina.com");
  //  邮箱 用户名 密码
  email.setAuthentication("用户名", "密码");
     //  邮件 发送 编码 格式
  email.setCharset("utf-8");
  //  收件人地址。。。
  email.addTo("收件人地址");
  //  发件人地址。。。
  email.setFrom("发件人地址");
  //  邮件标题
  email.setSubject("emai 发送测试");
  //  邮件发送内容
  email.setMsg("email --> 发送!");
  // 添加 附件
  EmailAttachment attachment = new EmailAttachment();
     attachment.setPath("d:/test.doc");// 本地文件
     // attachment.setURL(new URL("http://xxx/a.gif"));//远程文件
     attachment.setDisposition(EmailAttachment.ATTACHMENT);
    
     attachment.setDescription("dwr 帮助文档"); // 文件描述
     attachment.setName("test.doc"); // 文件下载名称
    
     email.attach(attachment);
     email.send();
  
 }
 /**
  * HTML 邮件发送
  *
  * 方法名称:HTMLEmailSend
  * 作者:黎青春
  * 创建日期:Aug 24, 2009
  * 方法描述: 
  * @throws Exception void
  */
 
 public static void HTMLEmailSend() throws Exception{
  
  HtmlEmail email =new HtmlEmail ();
  email.setHostName("smtp.sina.com");
  //  邮箱 用户名 密码
  email.setAuthentication("用户名", " 密码");
     //  邮件 发送 编码 格式
  email.setCharset("utf-8");
  //  收件人地址。。。
  email.addTo("收件人地址");
  //  发件人地址。。。
  email.setFrom("发件人地址");
  //  邮件标题
  email.setSubject("emai 发送测试");
      //  邮件发送内容
  email.setHtmlMsg("<font color='red'><h1> email --> 发送!</h1></font>");
  
  email.send();
    
 }
 
}
页: [1]
查看完整版本: Java发送邮件之Apache Commons Email