通过common-emai类库发送普通邮件和带有附件的邮件的完整示例

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

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

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
      <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>  

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

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

注意:
    eu.setDefaultAuthenticatorUsername("sidongxue");  
    eu.setDefaultAuthenticatorPassword("123456");此处用自己的邮箱用户名和密码.