简单邮件发送功能

实现简单邮件发送

配置信息

    public static void config(Email email,String from ,String[] to,String[] cc, String[] bcc,String subject) throws EmailException{
   		 //设置传出邮件服务器的主机名
        email.setHostName("设置传出邮件服务器的主机名");
        // 设置邮箱地址 邮箱密码
        email.setAuthentication("邮箱","邮箱密码");
        email.setSmtpPort(Integer.valueOf("端口"));
        email.setCharset("UTF-8");
        //当部分邮箱无效,依然支持剩余部分发送
        email.setSendPartial(true);
        //发件人
        if(from != null && !StringUtils.isEmpty(from)){
            email.setFrom(from);
        }else{
            return ;
        }
        // 设置收件人邮箱地址
        if(to!= null && StringUtils.isNotEmpty(to.toString())){
            email.addTo(to);
        }
        //抄送
        if(null!=cc && StringUtils.isNotEmpty(cc.toString())){
            email.addCc(cc);
        }
        //密送
        if(null!=bcc && StringUtils.isNotEmpty(bcc.toString())){
            email.addBcc(bcc);
        }
        //主题
        email.setSubject(subject);

    }

邮件发送

    public static String send(String from,String[] to,String subject,String content,String[] cc,List<File> fileList,String[] fileName, String[] bcc){
        // 设置邮件信息
        MultiPartEmail email = new MultiPartEmail();

        try {
            config(email,from,to,cc,bcc,subject);
            //内容
            email.setMsg(content);
            //附件
            if(fileList!=null){
                for(int i=0;i<fileList.size();i++){
                    File file = fileList.get(i);
                    EmailAttachment attachment = new EmailAttachment();
                    attachment.setPath(file.getPath());
                    attachment.setDisposition(EmailAttachment.ATTACHMENT);
                    attachment.setDescription("file");
                    attachment.setName(fileName[i]);
                    email.attach(attachment);
                }
            }
            email.send();
        } catch (EmailException e) {
            e.printStackTrace();
            return "false";
        }catch(Exception e){
            e.printStackTrace();
            return "false";
        }finally{
            if(fileList!=null){
                for(int i=0;i<fileList.size();i++){
                    fileList.get(i).delete();
                }
            }
        }
        return "true";
    }

邮件发送 删除临时文件

    /**
     * 发送带附件的邮件,可判断是否
     * */
    public static String send1(String from,String[] to,String subject,String content,String[] cc,List<File> fileList,String[] fileName, String[] bcc,boolean deleteFile){
        // Create the email message
        // 设置邮件信息
        MultiPartEmail email = new MultiPartEmail();

        try {
            config(email,from,to,cc,bcc,subject);
            //内容
            email.setMsg(content);
            //附件
            if(fileList!=null){
                for(int i=0;i<fileList.size();i++){
                    File file = fileList.get(i);
                    EmailAttachment attachment = new EmailAttachment();
                    attachment.setPath(file.getPath());
                    attachment.setDisposition(EmailAttachment.ATTACHMENT);
                    attachment.setDescription("file");
                    attachment.setName(fileName[i]);
                    email.attach(attachment);
                }
            }
            email.send();
        } catch (EmailException e) {
            e.printStackTrace();
            return "false";
        }catch(Exception e){
            e.printStackTrace();
            return "false";
        }finally{
            if(deleteFile){
                if(fileList!=null){
                    for(int i=0;i<fileList.size();i++){
                        fileList.get(i).delete();
                    }
                }
            }
        }
        return "true";
    }

Html 邮件发送

    public static String sendHtmlEmail(String from,String[] to,String[] cc, String[] bcc,String subject,String content,List<File> fileList,String[] fileName) {
        // Create the email message
        // 设置邮件信息
        ImageHtmlEmail email = new ImageHtmlEmail();

        try {
            config(email,from,to,cc,bcc,subject);

            DataSourceResolver[] dataSourceResolvers =
                    new DataSourceResolver[]{new DataSourceFileResolver()};
            email.setDataSourceResolver(new DataSourceCompositeResolver(dataSourceResolvers));
            // set the html message
            email.setHtmlMsg(content);

            // set the alternative message
            email.setTextMsg("Your email client does not support HTML messages");

            if(fileList!=null){
                for(int i=0;i<fileList.size();i++){
                    File file = fileList.get(i);
                    EmailAttachment attachment = new EmailAttachment();
                    attachment.setPath(file.getPath());
                    attachment.setDisposition(EmailAttachment.ATTACHMENT);
                    attachment.setDescription(fileName[i]);
                    attachment.setName(fileName[i]);
                    email.attach(attachment);
                }
            }

            email.send();
        }
        catch (EmailException e) {
            e.printStackTrace();
            return "false";
        }catch(Exception e){
            e.printStackTrace();
            return "false";
        }finally{
            if(fileList!=null){
                for(int i=0;i<fileList.size();i++){
                    fileList.get(i).delete();
                }
            }
        }
        return "true";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值