通過common-emai類庫發送普通郵件和帶有附件的郵件的完整示例

jopen 9年前發布 | 2K 次閱讀 Java

1.此示例是通過maven構建,pom.xml文件如下:

    <project xmlns="http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
<modelVersion>4.0.0</modelVersion>

  <groupId>com.ilucky.util.email</groupId>  
  <artifactId>email-util</artifactId>  
  <version>1.0-SNAPSHOT</version>  
  <packaging>war</packaging>  
  <name>email-util</name>  
  <url>http://maven.apache.org</url>  

  <properties>  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  </properties>  

  <dependencies>  
    <dependency>  
        <groupId>org.apache.commons</groupId>  
        <artifactId>commons-email</artifactId>  
        <version>1.3.3</version>  
    </dependency>  
  </dependencies>  
</project>  </pre> 


2.工具類如下:

import java.net.URL;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;

/**

  • @author IluckySi
  • @since 20150108 */
    public class EmailUtil {

    private String hostName;// 郵箱服務器.

    private int port; //服務器端口號.

    private String defaultAuthenticatorUsername;// 用戶名.

    private String defaultAuthenticatorPassword;// 密碼.

    private String charset = "GB2312";// 郵件編碼方式,默認為GB2312.

    private int timeout = 8000;//超時時間,單位毫秒,默認為8000.

    private String from;// 發送方.

    private String to;// 接收方.

    private String[] tos;// 多個接收方.

    private String cc;// 抄送方.

    private String[] ccs;// 多個抄送方.

    private String bcc;// 秘密抄送方.

    private String[] bccs;// 多個秘密抄送方.

    private String subject;// 標題.

    private String msg;// 內容.

    private String localAttachmentPath;// 本地附件.

    private String remoteAttachmentPath;// 遠程附件.

    private String attachmentName;//附件名稱.

    private String attachDescription;//附件描述.

    public String getHostName() {

     return hostName;  
    

    }

    public void setHostName(String hostName) {

     this.hostName = hostName;  
    

    }

    public int getPort() {

     return port;  
    

    }

    public void setPort(int port) {

     this.port = port;  
    

    }

    public String getDefaultAuthenticatorUsername() {

     return defaultAuthenticatorUsername;  
    

    }

    public void setDefaultAuthenticatorUsername(

         String defaultAuthenticatorUsername) {  
     this.defaultAuthenticatorUsername = defaultAuthenticatorUsername;  
    

    }

    public String getDefaultAuthenticatorPassword() {

     return defaultAuthenticatorPassword;  
    

    }

    public void setDefaultAuthenticatorPassword(

         String defaultAuthenticatorPassword) {  
     this.defaultAuthenticatorPassword = defaultAuthenticatorPassword;  
    

    }

    public String getCharset() {

     return charset;  
    

    }

    public void setCharset(String charset) {

     this.charset = charset;  
    

    }

    public int getTimeout() {

     return timeout;  
    

    }

    public void setTimeout(int timeout) {

     this.timeout = timeout;  
    

    }

    public String getFrom() {

     return from;  
    

    }

    public void setFrom(String from) {

     this.from = from;  
    

    }

    public String getTo() {

     return to;  
    

    }

    public void setTo(String to) {

     this.to = to;  
    

    }

    public String[] getTos() {

     return tos;  
    

    }

    public void setTos(String[] tos) {

     this.tos = tos;  
    

    }

    public String getCc() {

     return cc;  
    

    }

    public void setCc(String cc) {

     this.cc = cc;  
    

    }

    public String[] getCcs() {

     return ccs;  
    

    }

    public void setCcs(String[] ccs) {

     this.ccs = ccs;  
    

    }

    public String getBcc() {

     return bcc;  
    

    }

    public void setBcc(String bcc) {

     this.bcc = bcc;  
    

    }

    public String[] getBccs() {

     return bccs;  
    

    }

    public void setBccs(String[] bccs) {

     this.bccs = bccs;  
    

    }

    public String getSubject() {

     return subject;  
    

    }

    public void setSubject(String subject) {

     this.subject = subject;  
    

    }

    public String getMsg() {

     return msg;  
    

    }

    public void setMsg(String msg) {

     this.msg = msg;  
    

    }

    public String getLocalAttachmentPath() {

     return localAttachmentPath;  
    

    }

    public void setLocalAttachmentPath(String localAttachmentPath) {

     this.localAttachmentPath = localAttachmentPath;  
    

    }

    public String getRemoteAttachmentPath() {

     return remoteAttachmentPath;  
    

    }

    public void setRemoteAttachmentPath(String remoteAttachmentPath) {

     this.remoteAttachmentPath = remoteAttachmentPath;  
    

    }

    public String getAttachmentName() {

     return attachmentName;  
    

    }

    public void setAttachmentName(String attachmentName) {

     this.attachmentName = attachmentName;  
    

    }

    public String getAttachDescription() {

     return attachDescription;  
    

    }

    public void setAttachDescription(String attachDescription) {

     this.attachDescription = attachDescription;  
    

    }

    /**

    • 發送普通郵件. 支持一個/多個接收方,一個/多個抄送方,一個/多個秘密抄送方.
    • @return boolean */
      public boolean sendWithMsg() {
      boolean result = false;
      SimpleEmail email = new SimpleEmail();
      email.setSmtpPort(port);
      try {

       email.setHostName(hostName);  
       email.setAuthenticator(new DefaultAuthenticator(  
               defaultAuthenticatorUsername, defaultAuthenticatorPassword));  
       email.setCharset(charset);  
       email.setSocketConnectionTimeout(timeout);  
       email.setFrom(from);  
       if (to != null) {  
           email.addTo(to);  
       }  
       if (tos != null) {  
           email.addTo(tos);  
       }  
       if (cc != null) {  
           email.addCc(cc);  
       }  
       if (ccs != null) {  
           email.addCc(ccs);  
       }  
       if (bcc != null) {  
           email.addBcc(bcc);  
       }  
       if (bccs != null) {  
           email.addBcc(bccs);  
       }  
       if (subject != null) {  
           email.setSubject(subject);  
       }  
       if (msg != null) {  
           email.setMsg(msg);  
       }  
       if (email.send() != null) {  
           System.out.println("發送郵件成功");  
           result = true;  
       } else {  
           System.out.println("發送郵件失敗");  
       }  
      

      } catch (EmailException e) {

       System.out.println("發送郵件失敗: " + e);  
      

      }
      return result;
      }

      /**

    • 發送帶有附件的郵件. 支持一個/多個接收方,一個/多個抄送方,一個/多個秘密抄送方.
    • @return boolean */
      public boolean sendWithMsgAndAttachment() {
      boolean result = false;
      MultiPartEmail email = new MultiPartEmail();
      email.setSmtpPort(port);
      EmailAttachment attachment = new EmailAttachment();
      try {
       email.setHostName(hostName);  
       email.setAuthenticator(new DefaultAuthenticator(  
               defaultAuthenticatorUsername, defaultAuthenticatorPassword));  
       email.setCharset(charset);  
       email.setSocketConnectionTimeout(timeout);  
       email.setFrom(from);  
       if (to != null) {  
           email.addTo(to);  
       }  
       if (tos != null) {  
           email.addTo(tos);  
       }  
       if (cc != null) {  
           email.addCc(cc);  
       }  
       if (ccs != null) {  
           email.addCc(ccs);  
       }  
       if (bcc != null) {  
           email.addBcc(bcc);  
       }  
       if (bccs != null) {  
           email.addBcc(bccs);  
       }  
       if (subject != null) {  
           email.setSubject(subject);  
       }  
       if (msg != null) {  
           email.setMsg(msg);  
       }  
       if (localAttachmentPath != null) {  
           attachment.setPath(localAttachmentPath);  
           attachment.setDisposition(EmailAttachment.ATTACHMENT);  
       }  
       if (remoteAttachmentPath != null) {  
           attachment.setURL(new URL(remoteAttachmentPath));  
           attachment.setDisposition(EmailAttachment.ATTACHMENT);  
       }  
       if(attachmentName != null) {  
           attachment.setName(attachmentName);  
       }  
       if(attachDescription != null) {  
           attachment.setDescription(attachDescription);  
       }  
       email.attach(attachment);  
       if (email.send() != null) {  
           System.out.println("發送郵件成功");  
           result = true;  
       } else {  
           System.out.println("發送郵件失敗");  
       }  
      
      } catch (EmailException e) {
       System.out.println("發送郵件失敗: " + e);  
      
      } catch (Exception e) {
       System.out.println("發送郵件失敗: " + e);  
      
      }
      return result;
      }
      } </pre>
      3.測試類如下:
      import com.ilucky.util.email.EmailUtil;  

/**

  • @author IluckySi
  • @since 20150108
  • 注意:以后可以擴展回復郵件,定時發送等. */
    public class MainTest {

    public static void main(String[] args) {

     //發送普通郵件.  
     //post1();  
     //post2();  
     post3();  
    

    }

    public static void post1() {

     EmailUtil eu = new EmailUtil();  
     eu.setHostName("smtp.sohu.com");  
     eu.setPort(25);  
     eu.setDefaultAuthenticatorUsername("sidongxue");  
     eu.setDefaultAuthenticatorPassword("123456");  
     eu.setCharset("GB2312");  
     eu.setTimeout(16000);  
     eu.setFrom("sidongxue@sohu.com");  
     eu.setTo("sidongxue@sohu.com");  
     eu.setSubject("測試郵件");  
     eu.setMsg("親,這是一封測試郵件!");  
     System.out.println("發送郵件結果: " + eu.sendWithMsg());  
    

    }

    public static void post2() {

     EmailUtil eu = new EmailUtil();  
     eu.setHostName("smtp.sohu.com");  
     eu.setPort(25);  
     eu.setDefaultAuthenticatorUsername("sidongxue");  
     eu.setDefaultAuthenticatorPassword("123456");  
     eu.setCharset("GB2312");  
     eu.setTimeout(30000);  
     eu.setFrom("sidongxue@sohu.com");  
     String[] tos = new String[]{"1151262684@qq.com", "570258762@qq.com"};  
     eu.setTos(tos);  
     String[] ccs = new String[]{"1016336364@qq.com", "pengzhongchen@gmail.com"};  
     eu.setCcs(ccs);  
     String[] bccs = new String[]{"1198377646@qq.com", "sidongxue@sohu.com"};  
     eu.setBccs(bccs);  
     eu.setSubject("測試郵件");  
     eu.setMsg("親,這是一封測試郵件,如果收到郵件,請及時回復,十分感謝,如果回復晚了,后果自負!");  
     System.out.println("發送郵件結果: " + eu.sendWithMsg());  
    

    }

    public static void post3() {

     EmailUtil eu = new EmailUtil();  
     eu.setHostName("smtp.sohu.com");  
     eu.setPort(25);  
     eu.setDefaultAuthenticatorUsername("sidongxue");  
     eu.setDefaultAuthenticatorPassword("123456");  
     eu.setCharset("GB2312");  
     eu.setTimeout(16000);  
     eu.setFrom("sidongxue@sohu.com");  
     eu.setTo("sidongxue@sohu.com");  
     eu.setSubject("測試郵件");   
     eu.setMsg("親,這是一封測試郵件!");  
     eu.setAttachmentName("哈哈");  
     eu.setAttachDescription("這是一個文件,點我!");  
     eu.setLocalAttachmentPath("D:\\ilucky\\新建文本文檔22.txt");  
     System.out.println("發送郵件結果: " + eu.sendWithMsgAndAttachment());  
    

    }
    } </pre>
    注意:

        eu.setDefaultAuthenticatorUsername("sidongxue");  
     eu.setDefaultAuthenticatorPassword("123456");此處用自己的郵箱用戶名和密碼.  

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!