针对发送网络附件的java方法(使用Apache的jar包调用)

1、先要在pom.xml文件中引入对应的jar包

 <!--添加邮件的网络附件 start-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.3.2</version>
        </dependency>
<!--添加邮件的网络附件 end-->

2、发送网路哦邮件的方法

import org.apache.commons.mail.*;
import org.apache.commons.mail.resolver.DataSourceCompositeResolver;
import org.apache.commons.mail.resolver.DataSourceFileResolver;
import org.apache.commons.mail.resolver.DataSourceUrlResolver;

 

 /**
     * 为邮件添加附件
     * @param email 邮件
     * @param urls 附件链接
     * @param names 附件名称,最好有后缀名才能预览文件
     */
    public void addAttachment(ImageHtmlEmail email, List<String> urls, List<String> names) {
        // 保证附件名的长度和链接一一对应
        while (urls.size() > names.size()) {
            names.add("");
        }

        for (int i = 0; i < urls.size(); i++) {
            EmailAttachment attachment = new EmailAttachment();
            try {
                attachment.setURL(new URL(urls.get(i)));
                attachment.setDisposition(EmailAttachment.ATTACHMENT);
//                attachment.setDescription("Apache logo");
                attachment.setName(names.get(i));
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                email.attach(attachment); // 将附件添加到邮件中
            } catch (EmailException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
  //添加网络附件
    public  boolean sendHtmlEmailWithImg(IMailService mailService, String huiyi_id, String toMail, String subject, String content, List<String> urls, List<String> names) throws Exception {
//        public  boolean sendHtmlEmailWithImg(ExHashMap mail, String huiyi_id, String toMail, String subject, String content, List<String> urls, List<String> names) throws Exception {
        //获取发送邮箱
        System.setProperty("java.net.preferIPv4Stack", "true");
        System.setProperty("mail.smtp.ssl.enable", "true");
        ExHashMap mail = getMail(mailService, huiyi_id);
        String htmlEmailContent = content;
        ImageHtmlEmail email = new ImageHtmlEmail();//用ImageHtmlEmail来发送
        email.setSSLOnConnect(true);
        email.setDebug(true);//可以看到执行过程的debug信息
        email.setCharset("UTF-8");//防止乱码
        email.setSSLCheckServerIdentity(true);
        email.setSslSmtpPort(mail.getString("port")); // 设定SSL端口
        email.setSmtpPort(mail.getInt("port"));
        //解析本地图片和网络图片都有的html文件重点就是下面这两行;
        //ImageHtmlEmail通过setDataSourceResolver来识别并嵌入图片
        //查看DataSourceResolver的继承结构发现有几个好用的子类
        DataSourceResolver[] dataSourceResolvers =
                new DataSourceResolver[]{new DataSourceFileResolver(),//添加DataSourceFileResolver用于解析本地图片
                        new DataSourceUrlResolver(new URL("http://"))};//添加DataSourceUrlResolver用于解析网络图片,注意:new URL("http://")
        //DataSourceCompositeResolver类可以加入多个DataSourceResolver,
        //把需要的DataSourceResolver放到一个数组里传进去就可以了;
        email.setDataSourceResolver(new DataSourceCompositeResolver(dataSourceResolvers));
        // 邮箱登陆信息
        email.setHostName(mail.getString("smtp"));//email.setHostName("smtp.qq.com");
        email.addTo(toMail, toMail);//email.addTo("*@qq.com", "Jo");
        email.setFrom(mail.getString("username"), mail.getString("username")); //email.setFrom("*@qq.com", "Me"); // 必须和登陆邮箱名一致,否则会报错
        email.setAuthenticator(new DefaultAuthenticator(mail.getString("username"), mail.getString("password"))); // 邮箱名和登陆密码
        email.setSubject(subject);//email.setSubject("发送一张图片,看看是否可以收到。");
        //添加附件
        addAttachment(email,urls,names);
        email.setHtmlMsg(htmlEmailContent);

        //如果客户端不去持HTML格式会显示这句话,不过应该很少有不支持HTML格式的客户端了吧
        email.setTextMsg("你的邮箱客户端不支持HTML格式邮件");
        String res = email.send();
        return true;
    }
    public static void main(String[] args){
        ExHashMap email = new ExHashMap();
        email.put("smtp","smtp.263.net");
        email.put("port","465");
        email.put("username","conference@ciccst.org.cn");
        email.put("password","Mail_Sent2017");
        List<String> urls = new ArrayList<String>();
        urls.add("https://img1.360buyimg.com/imgb/s250x250_jfs/t6391/84/85826224/305484/7812e706/59390161N9b653d78.jpg");
        List<String> names = new ArrayList<String>();
        names.add("hahaha.jpg");

        MailUtil mailUtil = new MailUtil();
        try {
            mailUtil.sendHtmlEmailWithImg(email, "huiyi_id", "744924365@qq.com", "邮件标题", "邮件内容", urls, names);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

 

posted @ 2019-08-22 21:49  DHR~小白  阅读(355)  评论(0编辑  收藏  举报