public void postMail( String[] recipients, String subject, String message, String from, String[] cc) throws MessagingException { sender.setUsername((String) properties.getProperty("mail.user")); sender.setPassword((String) properties.getProperty("mail.password")); sender.setHost((String) properties.getProperty("mail.smtp.host")); String portString = (String) properties.getProperty("mail.smtp.port"); sender.setPort(Integer.valueOf(portString)); sender.setProtocol((String) properties.getProperty("mail.transport.protocol")); sender.setJavaMailProperties(properties); MimeMessage mimeMessage = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, false, "UTF-8"); helper.setFrom(from); helper.setText(message); helper.setSubject(subject); if (cc != null) { helper.setCc(cc); } for (String receiver : recipients) { if (receiver != null) { helper.setTo(receiver); sender.send(mimeMessage); } } }
/** * 发送邮件 * * @param senderTitle 发送人名称, * @param senderMail 发送人邮箱地址, * @param userTitles 收件人名称列表, * @param userMails 收件人邮件地址列表, * @param mailCCs 抄送列表, * @param subject 邮件标题, * @param content 邮件内容, * @param isHtml 邮件内容是否为HTML格式 * @param fileTable * @throws MessagingException * @throws UnsupportedEncodingException */ private void send( String senderTitle, String senderMail, String[] userTitles, String[] userMails, String[] mailCCs, String[] mailBCCs, String subject, String content, boolean isHtml, Hashtable<String, File> fileTable) throws MessagingException, UnsupportedEncodingException { /* * if send mail is disabled, then return from this method. */ if (!active) return; // ------------------------------------------------------------------------------------------------------------------------------------------ MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true, DEFAULT_ENCODING); // ------------------------------------------------------------------------------------------------------------------------------------------ if (senderMail != null) { if (senderTitle != null) { mimeMessageHelper.setFrom(senderMail, senderTitle); } else { mimeMessageHelper.setFrom(senderMail); } } // ------------------------------------------------------------------------------------------------------------------------------------------ if (StringUtil.isEmpty(mailTo)) { if (userMails != null) { if (userTitles == null) { mimeMessageHelper.setTo(userMails); } else { for (int i = 0; i < userMails.length; i++) { if (i > userTitles.length || userTitles.length == 0) { mimeMessageHelper.addTo(userMails[i]); } else { mimeMessageHelper.addTo(userMails[i], userTitles[i]); } } } } if (mailCCs != null) mimeMessageHelper.setCc(mailCCs); if (mailBCCs != null) mimeMessageHelper.setBcc(mailBCCs); } else { if (log.isDebugEnabled()) { log.debug( "Option 'MailTo' is settled, mail '" + subject + "' will be sent to '" + mailTo + "'."); } mimeMessageHelper.addTo(mailTo); } // ------------------------------------------------------------------------------------------------------------------------------------------ if (subject != null) mimeMessageHelper.setSubject(subject); if (content != null) mimeMessageHelper.setText(content, isHtml); if (fileTable != null) { Enumeration<String> e = fileTable.keys(); while (e.hasMoreElements()) { String key = e.nextElement(); File file = fileTable.get(key); mimeMessageHelper.addAttachment(key, file); } } mailSender.send(mimeMessageHelper.getMimeMessage()); // ------------------------------------------------------------------------------------------------------------------------------------------ message = null; mimeMessageHelper = null; }