WebService java 发送邮件接口 总结

java发送邮件,主要用的jar

使用的jar包

mail.jar

web service 用的xfire

大致思路及步骤

   1. 定义邮件发送接口

   2. 配置邮件线程池

   3. 记录日志

 

实体 MailBean

 代码如下 复制代码

package com.reagle.bean;
 
 
 import java.util.Date;
 import java.util.List;
 import java.util.Properties;
 
 import javax.activation.DataHandler;
 import javax.activation.FileDataSource;
 import javax.mail.Address;
 import javax.mail.BodyPart;
 import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.Part;
 import javax.mail.PasswordAuthentication;
 import javax.mail.Session;
 import javax.mail.Transport;
 import javax.mail.URLName;
 import javax.mail.internet.AddressException;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeBodyPart;
 import javax.mail.internet.MimeMessage;
 import javax.mail.internet.MimeMultipart;
 
 import org.apache.log4j.LogManager;
 import org.apache.log4j.Logger;
 
 public class MailBean {
     private Logger log = LogManager.getLogger(MailBean.class);
     /**
      * 主题
      */
     private String subject;   
     /**
      * 发送邮箱地址
      */
     private String sendMail;
     /**
      * 发送邮箱密码
      */
     private String sendMailPassword;
     /**
      * 收件人
      */
     private List<String> tos;
     /**
      * 抄送
      */
     private List<String> ccs;
     /**
      * 发送内容
      */
     private String content;
     /**
      * 内容编码
      */
     private String character;
     /**
      * 发送服务器地址
      */
     private String mailHost;
     /**
      *  附件
      */
     private List<String> accessories;
     /**
      * 发送是否需要校验
      */
     private String isCheckSend;
     /**
      *
      * @param subject 主题
      * @param sendMail 发件人
      * @param sendMailPassword 密码
      * @param tos 收件人
      * @param content 内容
      * @param character  内容字符集
      * @param mailHost SMTP主机
      * @param isCheckSend 是否需要验证
      */
     public MailBean(String subject, String sendMail, String sendMailPassword,
             List<String> tos, String content, String character,
             String mailHost, String isCheckSend) {
         this.subject = subject;
         this.sendMail = sendMail;
         this.sendMailPassword = sendMailPassword;
         this.tos = tos;
         this.content = content;
         this.character = character;
         this.mailHost = mailHost;
         this.isCheckSend = isCheckSend;
     }
     /**
      *
      * @param subject 主题
      * @param sendMail 发件人
      * @param sendMailPassword 密码
      * @param tos 收件人
      * @param content 内容
      * @param character  内容字符集
      * @param mailHost SMTP主机
      * @param isCheckSend 是否需要验证
      * @param accessories 附件
      */
     public MailBean(String subject, String sendMail, String sendMailPassword,
             List<String> tos, String content, String character,
             String mailHost, List<String> accessories, String isCheckSend) {
         this.subject = subject;
         this.sendMail = sendMail;
         this.sendMailPassword = sendMailPassword;
         this.tos = tos;
         this.content = content;
         this.character = character;
         this.mailHost = mailHost;
         this.accessories = accessories;
         this.isCheckSend = isCheckSend;
     }
     /**
      *
      * @param subject 主题
      * @param sendMail 发件人
      * @param sendMailPassword 密码
      * @param tos 收件人
      * @param content 内容
      * @param character  内容字符集
      * @param mailHost SMTP主机
      * @param isCheckSend 是否需要验证
      * @param accessories 附件
      * @param cc 抄送
      */
     public MailBean(String subject, String sendMail, String sendMailPassword,
             List<String> takeMail, String content, String character,
             String mailHost, List<String> accessories, String isCheckSend, List<String> cc) {
         this.subject = subject;
         this.sendMail = sendMail;
         this.sendMailPassword = sendMailPassword;
         this.tos = takeMail;
         this.content = content;
         this.character = character;
         this.mailHost = mailHost;
         this.accessories = accessories;
         this.isCheckSend = isCheckSend;
         this.ccs = cc;
     }
     public String getSubject() {
         return subject;
     }
     public void setSubject(String subject) {
         this.subject = subject;
     }
     public String getSendMail() {
         return sendMail;
     }
     public void setSendMail(String sendMail) {
         this.sendMail = sendMail;
     }
     public String getSendMailPassword() {
         return sendMailPassword;
     }
     public void setSendMailPassword(String sendMailPassword) {
         this.sendMailPassword = sendMailPassword;
     }
 
     public String getContent() {
         return content;
     }
     public void setContent(String content) {
         this.content = content;
     }
     public String getCharacter() {
         return character;
     }
     public void setCharacter(String character) {
         this.character = character;
     }
     public String getMailHost() {
         return mailHost;
     }
     public void setMailHost(String mailHost) {
         this.mailHost = mailHost;
     }
     public List<String> getAccessories() {
         return accessories;
     }
     public void setAccessories(List<String> accessories) {
         this.accessories = accessories;
     }
     public String getCheckSend() {
         return isCheckSend;
     }
     public void setCheckSend(String isCheckSend) {
         this.isCheckSend = isCheckSend;
     }
     public List<String> getTos() {
         return tos;
     }
     public void setTos(List<String> tos) {
         this.tos = tos;
     }
     public List<String> getCcs() {
         return ccs;
     }
     public void setCcs(List<String> ccs) {
         this.ccs = ccs;
     }
     public String getIsCheckSend() {
         return isCheckSend;
     }
     public void setIsCheckSend(String isCheckSend) {
         this.isCheckSend = isCheckSend;
     }
     // 获得所有收件人
     private  Address[] getAllTake() throws ArrayIndexOutOfBoundsException, AddressException{
         return valueOfAddress(tos);
     }
     private Address[] valueOfAddress(List<String> list) throws ArrayIndexOutOfBoundsException, AddressException{
         Address[] takes = new Address[0];
         if(null == list || list.size() == 0)
             throw new ArrayIndexOutOfBoundsException("list is null");
         takes = new Address[list.size()];
         for(int i = 0; i < list.size();  ++ i) {
             takes[i]  = InternetAddress.parse(list.get(i))[0];
         }
         return takes;
     }
     private Address[] getAllOtherTake() throws ArrayIndexOutOfBoundsException, AddressException {
         if (null == ccs || ccs.size() == 0)
             return new Address[0];   
         else            
             return valueOfAddress(getCcs());
     }
     // 添加所有附件
     private void addBodyPartByMimeMultipart(MimeMultipart mm,
             List<String> filePaths) throws MessagingException,
             ArrayIndexOutOfBoundsException, NullPointerException {
        
         if (null == filePaths || filePaths.size() == 0)
             return;
         if(null == mm)
             throw new NullPointerException("MimeMultipart is null");
         for (int i = 0; i < filePaths.size(); i++) {
             MimeBodyPart bp = new MimeBodyPart();
             FileDataSource fileds = new FileDataSource(filePaths.get(i));
             DataHandler dh = new DataHandler(fileds);
             bp.setDisposition(Part.ATTACHMENT);
             bp.setFileName(fileds.getName());
             bp.setDataHandler(dh);
             mm.addBodyPart(bp);
         }
     }
    
     public boolean send() throws Exception {
         boolean isSend = false;
        
         // 设置SMTP发送服务器
         Properties props = System.getProperties();
        
         // 设置SMTP的主机
         props.put("mail.smtp.host", mailHost);         
        
         // 需要经过验证
         props.put("mail.smtp.auth", isCheckSend);      
         //Authenticator auth = Authenticator
        
         // 获得邮件会话对象
         Session session = Session.getDefaultInstance(props,null);
         URLName url = new URLName(this.sendMail);
         PasswordAuthentication pw = new PasswordAuthentication(this.sendMail, this.sendMailPassword);
         session.setPasswordAuthentication(url, pw);
        
         // 邮件对象
         MimeMessage mimeMsg = new MimeMessage(session);
        
         MimeMultipart multipart = new MimeMultipart();
        
         // 发送邮件地址
         mimeMsg.setFrom(new InternetAddress(sendMail));
        
         // 收件人
         mimeMsg.setRecipients(Message.RecipientType.TO, getAllTake());
        
         mimeMsg.setSentDate(new Date());
         // 抄送人
         mimeMsg.setRecipients(Message.RecipientType.CC, getAllOtherTake());
        
         // 主题 与字符编码
         mimeMsg.setSubject(getSubject(), getCharacter());
        
         // 设置邮件体格式
         BodyPart bp = new MimeBodyPart();
         //bp.setContent("<meta http-equiv=Content-Type content=text/html; charset="+getCharacter()+">"
         //                + content, "text/html;charset="+getCharacter());
        
         bp.setText(content);
        
         log.info(bp.getContent().toString());
         multipart.addBodyPart(bp);
        
        
         // 添加附件
         addBodyPartByMimeMultipart(multipart, getAccessories());
        
         isSend = process(session, mimeMsg, multipart, props);
        
         return isSend;
     }
    
     private boolean process(Session session, MimeMessage mimeMsg, MimeMultipart multipart, Properties props) throws MessagingException {
         boolean isSuccessfully = false;
         mimeMsg.setContent(multipart);
         mimeMsg.saveChanges();
         Session mailSession = Session.getInstance(props, null);
         Transport transport = mailSession.getTransport("smtp");
         transport.connect((String) props.get("mail.smtp.host"), getSendMail(),
                 getSendMailPassword());
         transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
         log.info("Mail Successfully Sended!");
         transport.close();
         isSuccessfully = true;
         return isSuccessfully;
     }
 
 }

邮件工具类

/**
 *
 */
package com.reagle.util.others;

import java.io.IOException;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

/**
 * @Program Name : reaglews.com.reagle.util.others.MailUtil.java
 * @Written by : kzw
 * @version : v1.00
 * @Creation Date : 2012-7-17 上午10:40:40
 * @Description :
 * 邮件操作工具类
 * @ModificationHistory
 * Who             When         What
 * ------- -------------     -----------------
 * kzw    2012-7-17 上午10:40:40         create
 *
 **/
public class MailUtil {
    private static Logger log = LogManager.getLogger(MailUtil.class);
   
    private static final String CONFIGPATH = "mail.properties";
    private static final String SMTP = "smtp";
    private static final String EMAILREGEX = "emailRegex";
    private static Properties mailConfig;
   
    static {
        reloadPro();
    }
    /**
     * 获取邮箱对应SMTP服务器
         * 通过邮件来判断需要的SMTP主机,这里默认了使用了QQ的SMTP服务器
     * @Eclosing_Method  : getMail2SMTP
     * @author  by       : kzw
     * @creation Date    : 2012-7-17上午11:02:33
     * @param mail
     * @return
     *
     */
    public static String getMail2SMTP(String mail) {
        return (String)getConfigValue(SMTP);
    }
    public static String getConfigValue(String key) {
        if(null == mailConfig)
            reloadPro();
        return (String)mailConfig.get(key);
    }
    /**
     * 重新加载配置文件
     * @Eclosing_Method  : reloadPro
     * @author  by       : kzw
     * @creation Date    : 2012-7-17上午11:57:19
     *
     */
    public static void reloadPro(){
        mailConfig = new Properties();
        try {
            mailConfig.load(MailUtil.class.getClassLoader().getResourceAsStream(CONFIGPATH));
        } catch (IOException e) {
            log.error("加载邮件配置错误! e=" + e.getMessage());
        }
    }
    /**
     * 邮箱地址是否正确
     * @Eclosing_Method  : isEmail
     * @author  by       : kzw
     * @creation Date    : 2012-7-17下午12:17:28
     * @param email
     * @return
     *
     */
    public static boolean isEmail(String email) {
        if(StringUtils.isNotBlank(email))
            return email.matches(getConfigValue(EMAILREGEX));
        return false;
    }
}

邮件配置文件mail.properties

#
 # 邮件发送配置文件
 #
 # kzw create 2012-07-17
 #
 #
 #
 
 #邮件内容字符集
 character=UTF-8
 #SMTP
 smtp=smtp.exmail.qq.com
 #默认发送者
 defaultSend=XXXXX
 #密码
 password=XXXXXX
 #是否需要发送验证
 checkSend=true
 #邮箱校验正则
 emailRegex=^([a-z0-9A-Z]+[_|\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\.)+[a-zA-Z]{2,}$

邮件接口

package com.reagle.server;
 
 import java.util.List;
 
 /**
  * @Program Name : reaglews.com.reagle.server.IMailService.java
  * @Written by : kzw
  * @version : v1.00
  * @Creation Date : 2012-7-17 上午09:29:30
  * @Description :
  * 邮件接口
  * @ModificationHistory
  * Who             When         What
  * ------- -------------     -----------------
  * kzw    2012-7-17 上午09:29:30         create
  *
  **/
 public interface IMailService {
     /**
      * 字符集配置key,这里有点怪怪的, 以后优化//TODO....
      */
     public final String CHARACTERCONFIG = "character";
     public final String SMTPCONFIG = "smtp";
     public final String CHECKSEND = "checkSend";
     public final String DEFAULT_SEND = "defaultSend";
     public final String DEFAULT_SEND_PWD = "password";
     /**
      * 发送邮件
      * @Eclosing_Method  : sendMail
      * @author  by       : kzw
      * @creation Date    : 2012-7-17上午10:08:50
      * @param sendMailAddr 发送人邮箱
      * @param sendMailPwd 发送人密码
      * @param cc 抄送人
      * @param to 收件人
      * @param subject 邮件主题
      * @param content 邮件内容
      * @param accessories 附件
      * @param mailType 邮件类型
      * @param operMan 操作员
      * @param memo 备注
      *
      */
     void sendMail(String sendMailAddr, String sendMailPwd, List<String> cc,
             List<String> to, String subject, String content, List<String> accessories,
             String mailType, String operMan, String memo);
    
 }

邮件接口实现类

/**
  *
  */
 package com.reagle.server.impl;
 
 import java.math.BigDecimal;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Date;
 import java.util.List;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.LogManager;
 import org.apache.log4j.Logger;
 
 import com.reagle.bean.MailBean;
 import com.reagle.common.db.DbUtilsTemplate;
 import com.reagle.server.IMailService;
 import com.reagle.util.others.MailUtil;
 
 /**
  * @Program Name : reaglews.com.reagle.server.impl.MailServiceImpl.java
  * @Written by : kzw
  * @version : v1.00
  * @Creation Date : 2012-7-17 上午10:10:32
  * @Description : 邮件接口实现
  * @ModificationHistory Who When What ------- ------------- -----------------
  *                      kzw 2012-7-17 上午10:10:32 create
  *
  **/
 public class MailServiceImpl implements IMailService {
     private static Logger log = LogManager.getLogger(MailServiceImpl.class);
    
     /* pool config */
     private static final int COREPOOLSIZE = 10;
     private static final int MAXIMUMPOOLSIZE = 20; //最大连接数
    
     private static ThreadPoolExecutor pool;
     private static BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
    
     static {
         /**
          *  使用JDK中的线程池,线程执行采用无边界策略
          */
         pool = new ThreadPoolExecutor(COREPOOLSIZE, MAXIMUMPOOLSIZE, 2, TimeUnit.MINUTES, queue);
     }
     @Override
     public void sendMail(String sendMailAddr, String sendMailPwd,
             List<String> ccs, List<String> tos, String subject,
             String content, List<String> accessories, String mailType, String operMan, String memo) {
        MailThread mail = new MailThread();
        mail.init(sendMailAddr, sendMailPwd, ccs, tos, subject, content, accessories, mailType, operMan, memo);
        pool.execute(mail);
     }
     /**
      *
      * @Program Name : reaglews.com.reagle.server.impl.MailServiceImpl.java
      * @Written by : kzw
      * @version : v1.00
      * @Creation Date : 2012-7-18 下午04:14:23
      * @Description :
      * 发送邮件线程
      * @ModificationHistory
      * Who             When         What
      * ------- -------------     -----------------
      * kzw    2012-7-18 下午04:14:23         create
      *
      *
      */
     class MailThread implements Runnable {
         private Logger logger = LogManager.getLogger(MailThread.class);
        
         private List<String> ccs;
         private List<String> tos;
         private List<String> accessories;
        
         private String sendMailAddr;
         private String sendMailPwd;
         private String subject;
         private String content;
         private String mailType;
         private String operMan;
         private String memo;
        
         private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         /**
          *  初始化线程数据
          * @Eclosing_Method  : init
          * @author  by       : kzw
          * @creation Date    : 2012-7-17下午05:25:48
          * @param sendMailAddr
          * @param sendMailPwd
          * @param ccs 抄送号码
          * @param tos 收件人
          * @param subject 主题
          * @param content 内容
          * @param accessories 附件
          *
          */
         public void init(String sendMailAddr, String sendMailPwd,
                 List<String> ccs, List<String> tos, String subject,
                 String content, List<String> accessories, String mailType, String operMan, String memo){
             this.sendMailAddr = sendMailAddr;
             this.sendMailPwd = sendMailPwd;
             this.ccs = ccs;
             this.tos = tos;
             this.subject = subject;
             this.content = content;
             this.accessories = accessories;
             this.mailType = mailType;
             this.operMan = operMan;
             this.memo = memo;
         }
         @Override
         public void run() {
             logger.info("发送邮件开始" + sdf.format(new Date()));
             int id = 0;
             String flag = "0";  //待发送
             String msg = "";      //发送信息
             boolean res = false;// 发送结果
             try {
                 id = getSeqNo();
                 ccs.addAll(queryCCsByMailType(mailType)); // 添加需要抄送的邮箱地址
                 // 使用默认的邮箱
                 if(StringUtils.isBlank(sendMailAddr)) {
                     sendMailAddr = MailUtil.getConfigValue(DEFAULT_SEND);
                     sendMailPwd = MailUtil.getConfigValue(DEFAULT_SEND_PWD);
                 }
                
                 // 插入待发送邮件日志
                 saveLog(sendMailAddr, ccs, tos, subject, content, accessories, mailType, operMan, memo, id);
                
                 // 校验数据, 去掉错误邮箱地址
                 if(!validate(sendMailAddr, sendMailPwd, ccs, tos, subject, content, accessories)) {
                     logger.error("校验邮件数据失败");
                     return;
                 }
                 // 获取邮件对象
                 MailBean mail = getMailInstance(sendMailAddr, sendMailPwd, ccs, tos, subject, content, accessories);
                
                 // 发送邮件
                 res = mail.send();
                 flag = res ? "1" : "2";
                 if(!res) msg = "未知错误,发送失败";
                
                 // 更新发送记录
                 modifyLog(msg, flag, id);
                
             } catch (Exception e) {
                 e.printStackTrace();
                 logger.error(e.getMessage(), e);
                 modifyLog(e.getMessage(), "2", id);
             }
             logger.info("发送邮件结束" + sdf.format(new Date()));   
         }
        
         /**
          * 获取邮件对象实体
          * @Eclosing_Method  : getInstance
          * @author  by       : kzw
          * @creation Date    : 2012-7-17下午03:16:02
          * @param sendMailAddr 发件人
          * @param sendMailPwd 发件人密码
          * @param ccs 抄送
          * @param tos 收件人
          * @param subject 主题
          * @param content 内容
          * @param accessories 附件
          * @return
          *
          */
         private MailBean getMailInstance(String sendMailAddr, String sendMailPwd,
                 List<String> ccs, List<String> tos, String subject,
                 String content, List<String> accessories){
             // 获取SMTP地址
             String mailHost = MailUtil.getMail2SMTP(sendMailAddr);
            
             // 创建邮件
             MailBean mail = new MailBean(subject,
                     sendMailAddr,
                     sendMailPwd,
                     tos,  // 收件人
                     content,
                     MailUtil.getConfigValue(CHARACTERCONFIG),
                     mailHost,
                     accessories,    // 附件
                     MailUtil.getConfigValue(CHECKSEND),
                     ccs  //抄送
             );
            
             return mail;
         }
        
         private boolean validate(String sendMailAddr, String sendMailPwd,
                 List<String> transmitMails, List<String> takeMail, String subject,
                 String content, List<String> multiparts){
             boolean res = true;
             if(!MailUtil.isEmail(sendMailAddr)) res = false;
             // 清除错误号码
             cancelErrorMailAddr(transmitMails);
             cancelErrorMailAddr(takeMail);
            
             return res;
         }
        
         private void cancelErrorMailAddr(List<String> list) {
             if(null == list || list.isEmpty()) return;
             List<String> back = new ArrayList<String>(list);
             for (String email : back) {
                 if(!MailUtil.isEmail(email)) {
                     list.remove(email);
                     log.info("移除错误邮件:" + email);
                 }
             }
         }
 
         private int getSeqNo() {
             String seqSql = "select seq_mail.nextval from dual";
             BigDecimal b =  (BigDecimal) new DbUtilsTemplate().findBy(seqSql, 1);
             return b.intValue();
         }
        
         private boolean saveLog(String sendMail, List<String> ccs, List<String> tos, String subject,
                 String content, List<String> accessories, String mailType, String operman, String memo, int id) {
            
             StringBuffer sql = new StringBuffer();
             sql.append(" INSERT INTO mail_send_detail ");
             sql.append(" (ID, sendmail, senddate, tos, ccs, subject, CONTEXT, status, ");
             sql.append(" mailtype, accessories, operman, operdate,opermemo) values(?, ?, sysdate, ?, ?, ?, ?, '0', ?, ?, ?, sysdate, ?)");
            
             String[] params = {
                     String.valueOf(id),
                     sendMail,
                     Arrays.toString(ccs.toArray()).replace("[", "").replace("]", ""),
                     Arrays.toString(tos.toArray()).replace("[", "").replace("]", ""),
                     subject,
                     content,
                     mailType,
                     Arrays.toString(accessories.toArray()).replace("[", "").replace("]", ""),
                     operman,
                     memo
             };
            
             return (1 == new DbUtilsTemplate().update(sql.toString(), params));
         }
        
         private boolean modifyLog(String memo, String flag, int id){
             String sql ="update mail_send_detail set status=?, memo = ? where id = ?";
             return (1 == new DbUtilsTemplate().update(sql,
                     new String[]{flag, memo, String.valueOf(id)}));
         }
         // 查询邮件类型下, 需要抄送的
         private List<String> queryCCsByMailType(String mailType) {
             List<String> list = new ArrayList<String>();
             String sql = "select ccs from dm_mail_type where dm01=?";
             String emails =  (String) new DbUtilsTemplate().findBy(sql, 1, mailType);
             if(StringUtils.isNotBlank(emails)) {
                  list = Arrays.asList(emails.split(";"));
             }
             return list;
         }
     }
 }

单元测试

/**
 *
 */
package com.reagle.IMailService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import com.reagle.server.IMailService;
import com.reagle.server.impl.MailServiceImpl;

/**
 * @Program Name : reaglews.com.reagle.IMailService.TestMailService.java
 * @Written by : kzw
 * @version : v1.00
 * @Creation Date : 2012-7-18 上午09:07:25
 * @Description :
 *
 * @ModificationHistory
 * Who             When         What
 * ------- -------------     -----------------
 * kzw    2012-7-18 上午09:07:25         create
 *
 **/

public class TestMailService {
    IMailService service;
    List<String> transmitMails;
    List<String> takeMail;
    List<String> multiparts;
    @Before
    public void init(){
          service = new MailServiceImpl();
          transmitMails = new ArrayList<String>();
          takeMail = new ArrayList<String>();
          multiparts = new ArrayList<String>();
          takeMail.add("100086@qq.com");
    }
   
    @Test
    public void testThreadPool() throws InterruptedException{
        //  策略 1 同时提交1000份请求
        int size = 1000;
        long start = System.currentTimeMillis();
        for (int i = 0; i < size; i++)
            service.sendMail(null, null, transmitMails, takeMail, "sub"+ System.currentTimeMillis(),
                    "content"+System.currentTimeMillis(), multiparts, "预订取消","1872", "测试");   
        long end = System.currentTimeMillis();
        Thread.sleep(5000000);
        System.out.println("邮件数量:" + size +", 总耗时:" + (end - start));
    }
    @Test
    public void testMailSend() throws InterruptedException{
        String sendMailAddr = "kuangzhongwei@1872.net";
        String sendMailPwd = "XXX";
        List<String> transmitMails = new ArrayList<String>();
        //transmitMails.add("kuangzhongwei@gmail.com");
       
        List<String> takeMail = new ArrayList<String>();
        takeMail.add("346554535@qq.com");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String subject = sdf.format(new Date()) + " subject";
        String content = sdf.format(new Date()) + "content 中文内容";
       
        // HTML代码 注意编码
        /*File f = new File("G:\a.txt");
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
        System.out.println(new InputStreamReader(new FileInputStream(f)).getEncoding());
        String fileContent = null;
        while(null != (fileContent = in.readLine())) {
            content += fileContent;
        }
        in.close();
        */
        List<String> multiparts = new ArrayList<String>();
        //multiparts.add("G:/a.txt");
       
        new MailServiceImpl().sendMail(sendMailAddr, sendMailPwd, transmitMails, takeMail, subject, content, multiparts, "02","1872", "测试");
        Thread.sleep(200000);
    }
}

webservices配置

<!--  mail web service 配置-->
    <service>
        <name>mail</name>
        <namespace>http://com.reagle</namespace>
        <serviceClass>com.reagle.server.IMailService</serviceClass>
        <implementationClass>
            com.reagle.server.impl.MailServiceImpl
        </implementationClass>
    </service>

webservice client 调用

/**
  *
  */
 package com.reagle.test;
 
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 
 import org.junit.Before;
 import org.junit.Test;
 
 import reagle.com.ArrayOfString;
 
 /**
  * @Program Name : testMailClient.com.reagle.test.TestMailServiceClient.java
  * @Written by : kzw
  * @version : v1.00
  * @Creation Date : 2012-7-18 下午05:00:47
  * @Description :
  *
  * @ModificationHistory Who When What ------- ------------- -----------------
  *                      kzw 2012-7-18 下午05:00:47 create
  *
  **/
 public class TestMailServiceClient {
     private mailClient serivce;
     List<String> transmitMails;
     List<String> takeMail;
     List<String> multiparts;
     @Before
     public void testInit() {
         serivce = new mailClient();
         transmitMails = new ArrayList<String>();
         takeMail = new ArrayList<String>();
         multiparts = new ArrayList<String>();
         takeMail.add("100086@qq.com");
 
     }
 
     @Test
     public void testMailSend(){
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         String subject = sdf.format(new Date()) + " subject";
         String content = sdf.format(new Date()) + "content 中文内容";
         String sendMailAddr = "kuangzhongwei@1872.net";
         String sendMailPwd = "XXXXX";
         ArrayOfString ccs=new ArrayOfString();
         ArrayOfString tos=new ArrayOfString();
         ArrayOfString multipartss=new ArrayOfString();
         tos.getString().addAll(takeMail);
        
         serivce.getmailHttpPort().sendMail(null, null, ccs, tos,
                     subject, content, multipartss, "02","1872", "测试");
        
     }
 }

时间: 2024-09-29 19:30:21

WebService java 发送邮件接口 总结的相关文章

java发送邮件示例讲解_java

本文实例讲解了java发邮件的详细过程,供大家参考,具体内容如下 1.邮件协议 发邮件的:SMTP (Simple Mail Transport  Protocal) 收邮件的协议:pop3 (Post Office Protocal 3) IMAP 新协议 发邮件也可以收邮件. (一步步的与服务器交互) SMTP : 2.收发邮件的过程: 一般情况下,smtp和pop3是两个服务器(主机). Smtp邮件的端口为25. POP3 端口 为110. 发邮件示例 1).安装foxmail: 2).

如何用IOS调用WebService(SOAP接口)_IOS

在一次项目开发过程中,用到IOS调用WebService接口,所以抽个空把这方面的内容给大家整理出来,分享给大家. 方法一:使用WSDL2ObjC工具,将接口转成OC类. 1.在浏览器中输入webService接口地址(Safari不可用,我用的是Firefox),如:http://xxx.xxx.asmx, 地址后面添加上.wsdl成http://xxx.xxx.asmx.wsdl打开. 2.将页面另存为wsdl文件,保存的时候后缀加上.wsdl,保存成如xxxxService.asmx.xm

Java发送邮件

public class MailSender { /** * 发送单个邮件 * @throws Exception */ public void sendMail() throws Exception{ Properties props = new Properties();//创建属性对象 props.put("mail.smtp.host", getHost());//设置smtp服务器地址 props.put("mail.smtp.auth", "

Java中接口和抽象类的区别与相同之处

  这篇文章主要介绍了Java中接口和抽象类的区别与相同之处,本文讲解了抽象类的概念.接口的概念.接口和抽象类的区别与联系等内容,需要的朋友可以参考下 1. 抽象类: (1).概念:抽象类是对一种事物的抽象,即对类抽..抽象类是对整个类整体进行抽象,包括属性.行为.Java抽象类和Java接口一样,都用来声明一个新的类型.并且作为一个类型的等级结构的起点. (2).格式: 代码如下: public abstract class abstractDemo{ /**属性*/ private Stri

java中接口没有构造方法那子类怎么访问他的数据

问题描述 java中接口没有构造方法那子类怎么访问他的数据 感觉跟以往学过的知识冲突了?哪位大神能伸出援手解答下?这个问题困扰我好久了,希望走过路过的不要错过哈! 解决方案 接口有什么数据?接口就是一个定义,不存任何数据. 解决方案二: 接口中没有任何函数,接口不是类,不能构造一个接口.只能构造一个实现了这个接口的函数. 解决方案三: 接口就是公共方法的集合,只有公有常量和抽象方法,数据是类的属性,只有类才可以定义私有成员变量的. 解决方案四: 接口,不能实例化,没有构造方法 解决方案五: 接口

java中接口的实现类有的命名为XxxSupport,请问这个Support是什么意思,和XxxImpl意义相同吗

问题描述 如题:java中接口的实现类有的命名为XxxSupport,请问这个Support是什么意思,和XxxImpl意义相同吗,请问这是什么模式或者思想 解决方案 Support 一般是抽象类,里边存放子类可复用的东西 共性如 struts2的ActionSupport spring的 ***DaoSupporthttp://jinnianshilongnian.iteye.com/blog/1685963Impl表示实现:是个性 即细节实现,不同的Impl里边的算法可能不一样 但都是完成同

使用java发送邮件出错

问题描述 使用java发送邮件出错 之前测试能发送邮件的,今天又试了一下出问题了....不知道什么原因,求大神 DEBUG: setDebug: JavaMail version 1.4.1 DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] DEBUG SMTP: useEhlo true, useAuth

java发送邮件不能带&amp;amp;quot;&amp;amp;amp;&amp;amp;quot;

问题描述 java发送邮件不能带"&" 如题 , url中带了&自动把后面的字符截掉了 , 请问如何避免 解决方案 java发送邮件java 发送邮件Java发送邮件 解决方案二: html字符转义,用 %26 代替http://www.tuicool.com/articles/YzYfaa 解决方案三: 用encodeURI试试呢? 解决方案四: 原来不是邮件发不过去, 是UMEditor没有获取到&后面的东西 , <script type="

java的接口可以直接拿来声明变量?

问题描述 java的接口可以直接拿来声明变量? public interface IUserManage { public void addUser(User user); public void modifyUser(User user); public void deleteUser(User user); } 现在有这么个接口,然后在另一个没有实现此接口的类里看到如下这句: private IUserManage userManage: 在这个类里,直接声明了userManage,并且us