|
- 发送简单的文本邮件。注意setHostName 与setAuthenticator 中的信息必须对应
public static void main(String[] args) {
try {
Email email = new SimpleEmail();
email.setHostName("smtp.yeah.net");// 发送方的服务器地址
email.addTo("ping198909@126.com");// 接受方的邮箱地址
// 验证发送人信息
email.setAuthenticator(new DefaultAuthenticator(
"username", "password"));
// 设置发送人
email.setFrom("ping198909@yeah.net");
// 设置邮件标题
email.setSubject("TestMail");
// 设置邮件内容
email.setMsg("This is a test mail ... :-)");
email.send();
} catch (Exception e) {
e.printStackTrace();
}
}
- 发送带附件的邮件
public static void main(String[] args) {
//创建附件信息
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("f:\\b1737e2dd042fd0d1f3089f4.gif");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
attachment.setName("b1737e2dd042fd0d1f3089f4.gif");
// 创建邮件信息
MultiPartEmail email = new MultiPartEmail();
try{
email.setDebug(true);
email.setHostName("smtp.yeah.net");
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.addTo("ping198909@yeah.net");
email.setFrom("ping198909@yeah.net");
email.setSubject("The picture");
email.setMsg("Here is the picture you wanted");
// 添加附件
email.attach(attachment);
// 发送邮件
email.send();
}catch(EmailException e){
e.printStackTrace();
}
}
|
|
|
|
|
|
|