都是javax.mail 的使用,没有太多要讲的,代码贴上自己试验吧
private MimeMessage mailMessage; // 邮件对象 private Multipart mp;// 附件添加的组件 private List files = new LinkedList();// 存放附件文件 /** * 增加发送附件 * * @param filename * 邮件附件的地址,只能是本机地址而不能是网络地址,否则抛出异常 * @return */ public boolean addFileAffix(String filename) { try { BodyPart bp = new MimeBodyPart(); FileDataSource fileds = new FileDataSource(filename); bp.setDataHandler(new DataHandler(fileds)); bp.setFileName(MimeUtility.encodeText(fileds.getName(), "utf-8", null)); // 解决附件名称乱码 mp.addBodyPart(bp);// 添加附件 files.add(fileds); } catch (Exception e) { System.err.println("增加邮件附件:" + filename + "发生错误!" + e); return false; } return true; } /** * 设置邮件内容,并设置其为文本格式或HTML文件格式,编码方式为UTF-8 * * @param mailBody * @return */ public boolean setBody(String mailBody) { try { BodyPart bp = new MimeBodyPart(); bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=UTF-8>" + mailBody, "text/html;charset=UTF-8"); // 在组件上添加邮件文本 mp.addBodyPart(bp); } catch (Exception e) { System.err.println("设置邮件正文时发生错误!" + e); return false; } return true; } /** * * @param mailInfo * @param content * @param mailAddresses * @param filename * @return * @throws MessagingException */ public boolean sendTextMail(MailSenderInfo mailInfo, String content, List<String> mailAddresses, String filename) throws MessagingException { MyAuthenticator authenticator = null; Properties pro = mailInfo.getProperties(); if (mailInfo.isValidate()) { authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } Session sendMailSession = Session.getDefaultInstance(pro, authenticator); mailMessage = new MimeMessage(sendMailSession); mp = new MimeMultipart();// 生成附件组件的实例 Address from = new InternetAddress(mailInfo.getFromAddress()); mailMessage.setFrom(from); Address[] toMails = new Address[mailAddresses.size()]; int i = 0; for (String mail : mailAddresses) { if (Tools.isNotNull(mail)) { toMails[i] = new InternetAddress(mail); i++; } } mailMessage.setSubject(mailInfo.getSubject()); mailMessage.setSentDate(new Date()); mailMessage.setRecipients(Message.RecipientType.TO, toMails); setBody(content); if (Tools.isNotNull(filename)) {// 添加附件 addFileAffix(filename); mailMessage.setContent(mp); mailMessage.saveChanges(); } try { Transport.send(mailMessage); } catch (Exception e) { e.printStackTrace(); } return true; }
直接调用的函数:
/** * @throws MessagingException * @Title: mailsend * @Description: TODO * @param @param map * @return void * @throws */ public void mailsend(String content, List<String> mailAddresses, String mailTitle,String filename) throws MessagingException { MailSenderInfo mailinfo = new MailSenderInfo(); mailinfo.setMailServerHost("xxxx"); mailinfo.setMailServerPort("xx"); mailinfo.setFromAddress("xx"); mailinfo.setUserName("xx"); mailinfo.setPassword("xx"); mailinfo.setValidate(true); mailinfo.setSubject(mailTitle); SimpleMailSender send = new SimpleMailSender(); send.sendTextMail(mailinfo, content, mailAddresses,filename); }
时间: 2024-11-08 18:42:12