java发送带附件的邮件

java发送带附件的邮件


import org.apache.commons.collections4.CollectionUtils;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.URLDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.util.ByteArrayDataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.Security;
import java.util.*;

import static com.hronline.sys.utils.Itext5HtmlToPDF.getHtml;



public class MailTest {
    /**
     * 发送附件邮箱
     *
     * @param type           邮箱类型
     * @param emailUsername  发送人邮箱账号
     * @param password       发送人邮箱密码
     * @param recipientEmail 接收人邮箱
     * @param theme          主题
     * @param text           内容
     * @param attachurl      附件地址
     * @throws Exception
     */
    public Integer sendEmailWithAtta(Integer type, String emailUsername, String password, String recipientEmail, String theme, String text, List<Map<String,String>> attachurl) {
        String host = "";
        Properties props = new Properties();
        if (type == 1) {
            //163
            props = getSend163();
            host = "smtp.163.com";
        } else if (type == 2) {
            //126
            props = getSend126();
            host = "smtp.126.com";
        } else if (type == 3) {
            //qq
            props = getSendQQ();
            host = "smtp.qq.com";
        } else if (type == 4) {
            //139
            props = getSend139();
            host = "smtp.139.com";
        } else if (type == 5) {
            //aliyun
            props = getSendALI();
            host = "smtp.aliyun.com";
        }
        try {
            Session session = Session.getInstance(props);
            //创建邮件对象
            MimeMessage message = new MimeMessage(session);
            //指明邮件的发件人
            message.setFrom(new InternetAddress(emailUsername));
            //指明邮件的收件人
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipientEmail));
            //邮件的标题
            message.setSubject(theme);

            // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
            Multipart multipart = new MimeMultipart();

            // 添加邮件正文
            BodyPart contentPart = new MimeBodyPart();
            contentPart.setContent(text, "text/html;charset=UTF-8");
            multipart.addBodyPart(contentPart);

            //添加附件
            if(CollectionUtils.isNotEmpty(attachurl)){
                for (Map s : attachurl) {

                    String fileUrl = s.get("url").toString();
                    String name = s.get("name").toString();
                    System.out.println("-----------------------附件地址:" + s);
                    MimeBodyPart messageBodyPart=new MimeBodyPart();
                    URL url;
                    try {

                        url = new URL(fileUrl);
                        System.out.println("-----------------------添加" + s);
                        DataSource dataSource = new URLDataSource(url);
                        DataHandler dataHandler = new DataHandler(dataSource);

                        messageBodyPart.setDataHandler(dataHandler);
                        messageBodyPart.setFileName(MimeUtility.encodeWord(name));
                        multipart.addBodyPart(messageBodyPart);
                        System.out.println("-----------------------处理完毕:" + s);
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                }
            }

            System.out.println("----------------------设置内容");
            // 将multipart对象放到message中
            message.setContent(multipart);
            System.out.println("-----------------------内容设置完毕");
            // 保存邮件
            message.saveChanges();
            System.out.println("-----------------------保存邮件");


            Transport ts = session.getTransport();
            ts.connect(host, emailUsername, password);
            System.out.println("-----------------------发送邮件");
            ts.sendMessage(message, message.getAllRecipients());
            ts.close();
            System.out.println("----------------------发送完毕");
            
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 163发送配置
     */
    private Properties getSend163() {
        Properties p = new Properties();
        //设置发送邮件的服务器
        p.setProperty("mail.smtp.host", "smtp.163.com");
        p.put("mail.smtp.ssl.enable", true);
        //设置发送的协议
        p.setProperty("mail.smtp.protocol", "smtp");
        p.setProperty("mail.transport.protocol", "smtp");
        p.setProperty("mail.debug", "true");
        // 指定验证为true,这样才能通过验证
        p.setProperty("mail.smtp.auth", "true");
        // SSL安全连接参数
        p.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        p.setProperty("mail.smtp.socketFactory.fallback", "false");
        p.setProperty("mail.smtp.port", "465");
        p.setProperty("mail.smtp.socketFactory.port", "465");
        return p;
    }



    /**
     * qq发送配置
     */
    private Properties getSendQQ() {
        Properties p = new Properties();
        //设置发送邮件的服务器
        p.setProperty("mail.smtp.host", "smtp.qq.com");
        //设置发送的协议
        p.setProperty("mail.smtp.protocol", "smtp");
        p.setProperty("mail.transport.protocol", "smtp");
        p.setProperty("mail.debug", "true");
        // 指定验证为true,这样才能通过验证
        p.setProperty("mail.smtp.auth", "true");
        // SSL安全连接参数
        p.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        p.setProperty("mail.smtp.socketFactory.fallback", "false");
        p.setProperty("mail.smtp.port", "465");
        p.setProperty("mail.smtp.socketFactory.port", "465");
        return p;
    }

    /**
     * 126发送配置
     */
    private Properties getSend126() {
        Properties p = new Properties();
        //设置发送邮件的服务器
        p.setProperty("mail.smtp.host", "smtp.126.com");
        //设置发送的协议
        p.setProperty("mail.smtp.protocol", "smtp");
        p.setProperty("mail.transport.protocol", "smtp");
        p.setProperty("mail.debug", "true");
        // 指定验证为true,这样才能通过验证
        p.setProperty("mail.smtp.auth", "true");
        // SSL安全连接参数
        p.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        p.setProperty("mail.smtp.socketFactory.fallback", "false");
        p.setProperty("mail.smtp.port", "994");
        p.setProperty("mail.smtp.socketFactory.port", "994");
        return p;
    }

    /**
     * 139发送配置
     */
    private Properties getSend139() {
        Properties p = new Properties();
        //设置发送邮件的服务器
        p.setProperty("mail.smtp.host", "smtp.139.com");
        //设置发送的协议
        p.setProperty("mail.smtp.protocol", "smtp");
        p.setProperty("mail.transport.protocol", "smtp");
        p.setProperty("mail.debug", "true");
        // 指定验证为true,这样才能通过验证
        p.setProperty("mail.smtp.auth", "true");
        // SSL安全连接参数
        p.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        p.setProperty("mail.smtp.socketFactory.fallback", "false");
        p.setProperty("mail.smtp.port", "465");
        p.setProperty("mail.smtp.socketFactory.port", "465");
        return p;
    }

    /**
     * 阿里发送配置
     */
    private Properties getSendALI() {
        Properties p = new Properties();
        //设置发送邮件的服务器
        p.setProperty("mail.smtp.host", "smtp.aliyun.com");
        //设置发送的协议
        p.setProperty("mail.smtp.protocol", "smtp");
        p.setProperty("mail.transport.protocol", "smtp");
        p.setProperty("mail.debug", "true");
        // 指定验证为true,这样才能通过验证
        p.setProperty("mail.smtp.auth", "true");
        // SSL安全连接参数
        p.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        p.setProperty("mail.smtp.socketFactory.fallback", "false");
        p.setProperty("mail.smtp.port", "465");
        p.setProperty("mail.smtp.socketFactory.port", "465");
        return p;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值