首页

关于JAVA实现公司邮件发送组件,并提供源码分析下载及使用说明

标签:开源,源码,邮件,maven,java,commons-email,组件,依赖库     发布时间:2015-05-22   

使用说明:

1.下载配置项目资源,具体如下:

Web项目:直接下载所有依赖jar包(xwood-email-all.ra),请点击下载 ,解压后把其中的所有jar包部署到项目路径(默认为lib下)

Maven项目:首先下载并配置公司资源库组件包(xwood-common-email.rar),请点击下载,然后在POM项目中的pom.xml文件添加如例1-1的依赖关系

<dependency>@b@              <groupId>com.xwood</groupId>@b@              <artifactId>xwood-common-email</artifactId>@b@              <version>1.4</version>@b@</dependency>

   (例1-1)

2.  在项目classes目录下配置mail.properties文件,内容如下: 

#配置发送邮箱服务器@b@smtpHost=mail.xwood.net@b@#配置发送邮箱账号@b@authUser=system@xwood.net@b@#配置发送邮箱账号密码@b@authPass=123456@b@@b@#配置收件人邮箱@b@SendTo=to@xwood.net@b@#发送邮箱服务器端口@b@smtpPort=25

3. 接口调用示例

public  class  Test{@b@@b@    @b@    public  static  void   main(String[]  args){@b@    @b@        /**调用接口1;title-邮件主题;content-正文*/@b@        MailUtil.send("title", "content");     @b@        @b@        @b@        @b@        /**调用接口2;先定义发送邮件实体*/@b@        MailEntity e1=new MailEntity();@b@        e1.setSendSubject("tt333");@b@        e1.setSendContent("c3333");@b@        MailUtil.send(e1);     @b@    @b@    }    @b@    @b@}

源码说明及下载

该项目为Maven项目源码(xwood-common-email-src.rar),请点击下载,首先配置上传maven资源库所需资源(见使用说明1),对于

Web项目请下载导入上文源码及依赖jar包

源码分析说明:

a. 通用邮件发布工具包类MailUtil.java,同步发送邮件内容,具体代码如下:

package com.xwood.common.email; @b@@b@import java.io.IOException;@b@import java.util.Properties;@b@import java.util.concurrent.BlockingQueue; @b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@import org.apache.commons.mail.DefaultAuthenticator;@b@import org.apache.commons.mail.EmailException;@b@import org.apache.commons.mail.HtmlEmail;@b@ @b@/**@b@ *  类描述@b@ * @author nijun@b@ * @version 版本创建于  2015-4-8@b@ */@b@ @b@public class MailUtil { @b@    private  static HtmlEmail client = new HtmlEmail();  @b@    private static boolean debug = false;@b@    private  static boolean ssl = false;@b@     @b@    public  static int mailSize=1000;@b@    public  static long outTime=60000L;@b@     @b@    private static final String sslSmtpPort="465";@b@    private static final String charset="UTF-8";@b@     @b@    static final Log log = LogFactory.getLog(MailUtil.class); @b@    static{@b@         Properties property = new Properties();@b@         try {@b@             property.load(MailEntity.class.getResourceAsStream("/mail.properties"));@b@             client.setHostName(property.getProperty("smtpHost").trim());@b@             String getPropAuthUser=property.getProperty("authUser").trim();@b@             String getPropAuthPass=property.getProperty("authPass").trim();@b@             client.setAuthenticator(new DefaultAuthenticator(getPropAuthUser,getPropAuthPass));@b@             client.setDebug(debug);@b@             if (ssl) {@b@                 client.setSSL(ssl);@b@                 client.setSslSmtpPort(sslSmtpPort);@b@             } @b@            client.setCharset(charset);@b@            client.setFrom(property.getProperty("authUser").trim(), "System");@b@            client.addTo(property.getProperty("SendTo").trim(), "Receiver");@b@                     @b@            if(property.getProperty("mailSize").trim()!=null)@b@            mailSize=Integer.parseInt(property.getProperty("mailSize").trim());@b@                     @b@            if(property.getProperty("outTime").trim()!=null)@b@            outTime=Long.parseLong(property.getProperty("outTime").trim());@b@                @b@            }catch(NullPointerException ee){@b@              log.warn(" mail.properties  has  not  finded  ");@b@            } catch (IOException e) {@b@                log.warn(" mail.properties  can t  be  openned   ");@b@            }catch(Exception eee){@b@                    log.error("mail.properties configure err:"+eee.getMessage());@b@            }@b@             @b@    }@b@     @b@    /**@b@     * 发送邮件主题内容,只能用固定配置邮箱参数进行发送@b@     * @param subject@b@     * @param message@b@     */@b@    public synchronized static void send(String subject,String message){@b@        MailEntity mail=new MailEntity();@b@        mail.setSendSubject(subject);@b@        mail.setSendContent(message);@b@        send(mail);@b@    }@b@     @b@     @b@    /**@b@     * 可以发送自定义邮箱信息内容@b@     * @param m@b@     */@b@    public synchronized static void  send(MailEntity m){@b@        @b@        try {@b@            client.setSubject(m.getSendSubject());@b@            client.setHtmlMsg(m.getSendContent());@b@            client.send();@b@            log.info("about  "+m.getSendSubject()+"   mail is sended successed!");@b@        } catch (EmailException ee) {@b@            log.error("about  "+m.getSendSubject()+"  mail is sended error", ee);@b@        }@b@    }@b@     @b@    public synchronized static void  send(BlockingQueue<MailEntity> ms){@b@        StringBuilder  mstrs=new StringBuilder("");@b@        String subject=null;@b@        while(ms.size()>0){@b@            if(subject==null){@b@                MailEntity m=ms.poll();@b@                subject=m.getSendSubject();@b@                mstrs.append("<p>"+m.getSendContent()+"</p>");@b@            }else@b@                mstrs.append("<p>"+ms.poll().getSendContent()+"</p>");@b@        }@b@        send(subject,mstrs.toString());@b@    }@b@     @b@     @b@     @b@     @b@    public static void main(String[] args){@b@        MailUtil.send("测试主题","测试内容....");@b@    }@b@     @b@  @b@ @b@}

b. 邮件内容实体类(现只定义了邮箱标题和正文,如需自定义收件人,请改写该类),代码如下:

 package com.xwood.common.email;@b@@b@import java.io.IOException;@b@import java.util.Properties;@b@@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@@b@/**@b@ * 类描述@b@ * @b@ * @author nijun@b@ * @version 版本创建于 2015-4-8@b@ */@b@@b@@SuppressWarnings("unused")@b@public class MailEntity {@b@@b@    private String SendSubject;@b@    private String SendContent;@b@@b@    public MailEntity() {@b@    }@b@@b@    public MailEntity(String sendSubject, String sendContent) {@b@        super();@b@        this.SendSubject = sendSubject;@b@        this.SendContent = sendContent;@b@    }@b@@b@    public String getSendSubject() {@b@        return SendSubject;@b@    }@b@@b@    public String getSendContent() {@b@        return SendContent;@b@    }@b@@b@    public void setSendSubject(String sendSubject) {@b@        SendSubject = sendSubject;@b@    }@b@@b@    public void setSendContent(String sendContent) {@b@        SendContent = sendContent;@b@    }@b@@b@    public static void main(String[] args) {@b@@b@        MailEntity entity = new MailEntity("TITLE", "MESS");@b@    }@b@@b@}
  • ◆ 相关内容