1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| HtmlEmail email = new HtmlEmail();
try {
email.setCharset("UTF-8");
email.setFrom("xxx@yahoo.com", MimeUtility.encodeText("我就是寄信的人", "UTF-8", "B"));
email.setHostName("ms1.hinet.net");
email.addTo("xxx@yahoo.com", MimeUtility.encodeText("我是收信人", "UTF-8", "B"));
email.setSubject("This is Test!");
MimeMultipart mimeMultipart = new MimeMultipart();
MimeBodyPart content = new MimeBodyPart();
content.setContent(Content, "text/html; charset=UTF-8");
content.setDisposition(Part.INLINE);
mimeMultipart.addBodyPart(content);
email.addPart(mimeMultipart);
EmailAttachment attachment = null;
File files = dir.list(new WildcardFileFilter("*"));
File attachFile = null;
for (int i = 0; i < files.length; i++) {
attachFile = new File(dir + "/" + files[i]);
attachment = new EmailAttachment();
attachment.setPath(attachFile.getAbsolutePath());
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setName(MimeUtility.encodeText(attachFile.getName(), "UTF-8", "B"));
email.attach(attachment);
}
email.send();
} catch (EmailException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
|