日期:2014-05-18 浏览次数:20637 次
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();
}
}
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("mypictures/john.jpg");//指定附件在本地的路径
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");//附件描述
attachment.setName("John");//附件名称
email.attach(attachment);