• javaMail邮件发送功能(多收件人,多抄送人,多密送人,多附件)


    1. private Session session;  
    2.    private Transport transport;  
    3.   
    4.    private String mailHost = "";  
    5.    private String sender_username = "";  
    6.    private String sender_password = "";  
    7.    private String contentPart_Type = "";  
    8.   
    9.    private Properties properties = new Properties();  
    10.      
    11.    /* 
    12.     * 初始化方法 
    13.     */  
    14.    public JavaMailWithAttachment() {  
    15.        try {  
    16.         FileInputStream is=new FileInputStream("E:/workspace1.6/ICRM-H5/src/mailServer.properties");  
    17.            properties.load(is);  
    18.            this.mailHost = properties.getProperty("mail.smtp.host");  
    19.            this.sender_username = properties.getProperty("mail.sender.username");  
    20.            this.sender_password = properties.getProperty("mail.sender.password");  
    21.            this.contentPart_Type = properties.getProperty("mail.contentPart.type");  
    22.        } catch (IOException e) {  
    23.            e.printStackTrace();  
    24.        }  
    25.   
    26.        session = Session.getInstance(properties);  
    27.        session.setDebug(true); //开启后有调试信息,发布时关闭  
    28.        message = new MimeMessage(session);  
    29.    }  
    30.   
    31.      
    32.    /** 
    33.     * 发送邮件 
    34.     * @param subject 
    35.     *               邮件主题 
    36.     * @param sendHtml 
    37.     *               邮件内容 
    38.     * @param toUser 
    39.     *               收件人  多个时参数形式  :  "xxx@xxx.com,xxx@xxx.com,xxx@xxx.com" 
    40.     * @param ccUser 
    41.     *               抄送人   同上 
    42.     * @param bccUser 
    43.     *               密送人   同上 
    44.     * @param attachment 
    45.     *               附件 
    46.     */  
    47. public void doSendHtmlEmail(String subject, String sendHtml,  
    48.         String  toUser, String ccUser, String bccUser, File [] attachment){  
    49.     try {  
    50.         // 发件人  
    51.            InternetAddress from = new InternetAddress(sender_username);  
    52.            message.setFrom(from);  
    53.              
    54.            // 设置多个收件人地址  
    55.            if(null != toUser && !toUser.isEmpty()){  
    56.             @SuppressWarnings("static-access")  
    57.             InternetAddress[] internetAddressTo = new InternetAddress().parse(toUser);  
    58.             message.setRecipients(Message.RecipientType.TO, internetAddressTo);  
    59.            }  
    60.              
    61.            // 设置多个抄送地址  
    62.            if(null != ccUser && !ccUser.isEmpty()){  
    63.             @SuppressWarnings("static-access")  
    64.             InternetAddress[] internetAddressCC = new InternetAddress().parse(ccUser);  
    65.             message.setRecipients(Message.RecipientType.CC, internetAddressCC);  
    66.            }  
    67.              
    68.            // 设置多个密送地址  
    69.            if(null != bccUser && !bccUser.isEmpty()){  
    70.             @SuppressWarnings("static-access")  
    71.             InternetAddress[] internetAddressBCC = new InternetAddress().parse(bccUser);  
    72.             message.setRecipients(Message.RecipientType.BCC, internetAddressBCC);  
    73.            }  
    74.              
    75.            // 发送日期  
    76.            message.setSentDate(new Date());   
    77.              
    78.            // 邮件主题  
    79.            message.setSubject(subject);  
    80.              
    81.            // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件  
    82.            Multipart multipart = new MimeMultipart();  
    83.              
    84.            // 添加邮件正文  
    85.            BodyPart contentPart = new MimeBodyPart();  
    86.            contentPart.setContent(sendHtml, contentPart_Type);  
    87.            multipart.addBodyPart(contentPart);  
    88.              
    89.            BodyPart attachmentBodyPart = null;  
    90.            // 添加附件的内容  
    91.            if (null != attachment && attachment.length != 0) {  
    92.             for (File file : attachment) {  
    93.                 attachmentBodyPart = new MimeBodyPart();  
    94.                   
    95.                 DataSource source = new FileDataSource(file);  
    96.                 attachmentBodyPart.setDataHandler(new DataHandler(source));  
    97.                 //MimeUtility.encodeWord可以避免文件名乱码  
    98.                    attachmentBodyPart.setFileName(MimeUtility.encodeWord(file.getName()));  
    99.                    multipart.addBodyPart(attachmentBodyPart);  
    100.             }  
    101.            }  
    102.              
    103.            // 将multipart对象放到message中  
    104.            message.setContent(multipart);  
    105.              
    106.            // 保存邮件  
    107.            message.saveChanges();  
    108.              
    109.            // smtp验证  
    110.            transport = session.getTransport("smtp");  
    111.            transport.connect(mailHost, sender_username, sender_password);  
    112.              
    113.            // 发送  
    114.            transport.sendMessage(message, message.getAllRecipients());  
    115.            System.out.println("发送成功!");  
    116.     } catch (Exception e) {  
    117.         e.printStackTrace();  
    118.     } finally {  
    119.         if (transport != null) {  
    120.                try {  
    121.                    transport.close();  
    122.                } catch (MessagingException e) {  
    123.                    e.printStackTrace();  
    124.                }  
    125.            }  
    126.     }  
    127.    }  
  • 相关阅读:
    fastdfs 外网映射问题记录
    fastdfs-nginx下载报错 400
    nginx 代理 websocket
    Jenkins 安装
    实验四.2
    实验四.1
    实验三
    shiyan2
    shiyan1
    作业
  • 原文地址:https://www.cnblogs.com/liubaihui/p/8458457.html
Copyright © 2020-2023  润新知