日期:2014-05-18  浏览次数:20637 次

【java】菜鸟提问:怎么给邮件添加附件?
写java程序发邮件的时候添加附件的代码怎么写呢?
发邮件的程序是这样的:
package Chapter02;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.SimpleEmail;

public class TestMail {

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Email email = new SimpleEmail();
email.setHostName("mail.bnu.edu.cn");
email.setSmtpPort(25);
email.setAuthenticator(new DefaultAuthenticator("aaa",
"password"));
email.setFrom("aaa@bbb.com");
email.setSubject("Task_WK09");
email.setMsg("hello world");
email.addTo("aaa@bbb.com");
email.send();
}

}
java mail 添加附件

------解决方案--------------------
 
 EmailAttachment attachment = new EmailAttachment();
  attachment.setPath("mypictures/john.jpg");//指定附件在本地的路径
  attachment.setDisposition(EmailAttachment.ATTACHMENT);
  attachment.setDescription("Picture of John");//附件描述
  attachment.setName("John");//附件名称
  email.attach(attachment);