java代码发邮件和附件 java发送邮件附件( 四 )


partSet = new ArrayListMailPart();
to = new ArrayListString();
cc = new ArrayListString();
bcc = new ArrayListString();
}
private String getPartContentType(String fileName) {
String ret = null;
if (null != fileName) {
int flag = fileName.lastIndexOf(".");
if (0 = flagflagfileName.length() - 1) {
fileName = fileName.substring(flag + 1);
}
ret = contentTypeMap.get(fileName);
}
if (null == ret) {
ret = defaultAttachmentContentType;
}
return ret;
}
private String toBase64(String str, String charset) {
if (null != str) {
try {
return toBase64(str.getBytes(charset));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return "";
}
private String toBase64(byte[] bs) {
return new BASE64Encoder().encode(bs);
}
private String toBase64(String str) {
return toBase64(str, Charset.defaultCharset().name());
}
private String getAllParts() {
int partCount = partSet.size();
StringBuilder sbd = new StringBuilder(LINE_END);
for (int i = partCount - 1; i = 0; i--) {
Mail attachment = partSet.get(i);
String attachmentContent = attachment.getContent();
if (null != attachmentContent0attachmentContent.length()) {
sbd.append(getBoundaryNextPart()).append(LINE_END);
sbd.append("Content-Type: ");
sbd.append(attachment.getContentType());
sbd.append(LINE_END);
sbd.append("Content-Transfer-Encoding: ");
sbd.append(attachment.getContentTransferEncoding());
sbd.append(LINE_END);
if (i != partCount - 1) {
sbd.append("Content-Disposition: ");
sbd.append(attachment.getContentDisposition());
sbd.append(LINE_END);
}
sbd.append(LINE_END);
sbd.append(attachment.getContent());
sbd.append(LINE_END);
}
}
sbd.append(LINE_END);
sbd.append(LINE_END);
partSet.clear();
return sbd.toString();
}
private void addContent() {
if (null != content) {
MailPart part = new MailPart();
part.setContent(toBase64(content));
part.setContentType("text/plain;charset=\"" + charset + "\"");
partSet.add(part);
}
}
private String listToMailString(ListString mailAddressList) {
StringBuilder sbd = new StringBuilder();
if (null != mailAddressList) {
int listSize = mailAddressList.size();
for (int i = 0; ilistSize; i++) {
if (0 != i) {
sbd.append(";");
}
sbd.append("").append(mailAddressList.get(i)).append("");
}
}
return sbd.toString();
}
private ListString getrecipient() {
ListString list = new ArrayListString();
list.addAll(to);
list.addAll(cc);
list.addAll(bcc);
return list;
}
public void addAttachment(String filePath) {
addAttachment(filePath, null);
}
public void addTo(String mailAddress) {
this.to.add(mailAddress);
}
public void addCc(String mailAddress) {
this.cc.add(mailAddress);
}
public void addBcc(String mailAddress) {
this.bcc.add(mailAddress);
}
public void addAttachment(String filePath, String charset) {
if (null != filePathfilePath.length()0) {
File file = new File(filePath);
try {
addAttachment(file.getName(), new FileInputStream(file),
charset);
} catch (FileNotFoundException e) {
System.out.println("错误:" + e.getMessage());
System.exit(1);
}
}
}
java代码怎么发送HTML页面和附件到邮件,注意不是html格式,是一个已经写好的.html页面,求代码 。发送java代码发邮件和附件的邮件内容Tabletrtd邮件内容/td/tr/table 接收java代码发邮件和附件的时候会自动转成HTML页面的发送的时候就像页面排版的内容一样的发过去就好java代码发邮件和附件了

推荐阅读