private void addContent(String text, TextType type) { if (null != text) { MailPart part = new MailPart(); part.setContent(toBase64(text)); part.setContentType("text/" + type.getValue() + ";charset=\"" + charset + "\""); alternativeList.add(part); } }
/** 添加邮件正文单元 */ private void addContent() { if (null != content) { MailPart part = new MailPart(); part.setContent(toBase64(content)); part.setContentType("text/plain;charset=\"" + charset + "\""); alternativeList.add(part); } }
/** * 添加一个附件单元 * * @param fileName 文件名 * @param attachmentStream 文件流 * @param charset 文件编码格式 */ public void addAttachment(String fileName, InputStream attachmentStream, String charset) { try { byte[] bs = null; if (null != attachmentStream) { int buffSize = 1024; byte[] buff = new byte[buffSize]; byte[] temp; bs = new byte[0]; int readTotal = 0; while (-1 != (readTotal = attachmentStream.read(buff))) { temp = new byte[bs.length]; System.arraycopy(bs, 0, temp, 0, bs.length); bs = new byte[temp.length + readTotal]; System.arraycopy(temp, 0, bs, 0, temp.length); System.arraycopy(buff, 0, bs, temp.length, readTotal); } } if (null != bs) { MailPart attachmentPart = new MailPart(); charset = null != charset ? charset : Charset.defaultCharset().name(); String contentType = getPartContentType(fileName) + ";name=\"=?" + charset + "?B?" + toBase64(fileName) + "?=\""; attachmentPart.setCharset(charset); attachmentPart.setContentType(contentType); attachmentPart.setContentDisposition( "attachment;filename=\"=?" + charset + "?B?" + toBase64(fileName) + "?=\""); attachmentPart.setContent(toBase64(bs)); partSet.add(attachmentPart); } } catch (Exception e) { e.printStackTrace(); } finally { if (null != attachmentStream) { try { attachmentStream.close(); attachmentStream = null; } catch (IOException e) { e.printStackTrace(); } } Runtime.getRuntime().gc(); Runtime.getRuntime().runFinalization(); } }