public static HashMap<String,String> sendEmailToPartner(Object[] toAddress){ HashMap<String,String> map=new HashMap<String,String>(); String message="邮件发送成功!"; map.put("state", "success"); map.put("message", message); Message msg =null; try { //GET PARTNER MAILCONN Properties props = new Properties(); props.put("mail.smtp.host", ChannelConfig.getValue("mail.smtp.host")); String port = ChannelConfig.getValue("mail.smtp.port"); String auth = ChannelConfig.getValue("mail.smtp.auth"); //IF SERVER NEED AUTH if (auth != null && !"".equals(auth.trim()) && "true".equals(auth)){ props.put("mail.smtp.auth", auth); if (port != null && !"".equals(port.trim())){ props.put("mail.smtp.port", port); } } else{ props.put("mail.smtp.auth", "false"); props.put("mail.smtp.timeout", "0"); } Session mailConnection = Session.getDefaultInstance(props); Transport transport = mailConnection.getTransport("smtp"); if (auth != null && !"".equals(auth.trim()) && "true".equals(auth)){ transport.connect(ChannelConfig.getValue("mail.smtp.host"), ChannelConfig.getValue("mail.smtp.username"), ChannelConfig.getValue("mail.smtp.password")); } else{ transport.connect(); } //ATTACH FILE msg = new MimeMessage(mailConnection); String subject="HP EG 通路計劃管理平台 即日起正式啟用"; msg.setSubject(MimeUtility.encodeText(subject,MimeUtility.mimeCharset("UTF-8"), null)); String from=ChannelConfig.getValue("mail.smtp.username"); Address fromAdd = new InternetAddress(from); msg.setFrom(fromAdd); if(toAddress == null || toAddress.length == 0) { return null; } Address[] addressList = new Address[toAddress.length]; for (int i = 0; i < toAddress.length; i++) { addressList[i] = new InternetAddress(toAddress[i].toString()); } /*Address[] addressList= new Address[]{new InternetAddress(toAddress.toString())};*/ msg.setRecipients(Message.RecipientType.BCC, addressList); addressList= new Address[]{new InternetAddress("[email protected]")}; msg.setRecipients(Message.RecipientType.TO, addressList); File currPathFile=new File(Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath()); File attFile=new File(currPathFile.getParentFile().getParentFile().getPath() + File.separator + "template"+File.separator+"html_tw.html"); Multipart multipart = new MimeMultipart(); BodyPart body = new MimeBodyPart(); DataSource source = new FileDataSource(attFile); body.setDataHandler(new DataHandler(source)); body.setFileName(MimeUtility.encodeText(attFile.getName())); multipart.addBodyPart(body); msg.setContent(multipart); msg.setSentDate(new Date()); transport.sendMessage(msg, msg.getAllRecipients()); transport.close(); return map; } catch(AuthenticationFailedException e){ message="邮件发送失败!错误原因:\n"+"身份验证错误!"; map.put("state", "failed"); map.put("message", message); e.printStackTrace(); } catch(AddressException e){ try { message="邮件发送失败!错误原因:\n"+msg.getRecipients(Message.RecipientType.BCC)[0]+"地址错误!"; } catch (MessagingException e1) { e1.printStackTrace(); } map.put("state", "failed"); map.put("message", message); e.printStackTrace(); }catch (MessagingException e) { message="邮件发送失败!错误原因:\n"+e.getMessage(); map.put("state", "failed"); map.put("message", message); e.printStackTrace(); Exception ex = null; if ((ex = e.getNextException()) != null) { System.out.println(ex.toString()); ex.printStackTrace(); } } catch (Exception e) { message="邮件发送失败!错误原因:\n"+e.getMessage(); map.put("state", "failed"); map.put("message", message); e.printStackTrace(); } return map; }
private static Address[] buildEmaiAddress(String[] addressArray) throws Exception { if(addressArray == null || addressArray.length == 0) { return null; } Address[] addressList = new Address[addressArray.length]; for (int i = 0; i < addressArray.length; i++) { addressList[i] = new InternetAddress(addressArray[i]); } return addressList; }