java Send Email
来源:互联网 发布:低智商证明知乎 编辑:程序博客网 时间:2024/04/30 03:16
今天在Apache网站上下载commons-lang jar文件时突然看到commons-email-1.1.jar这个项目jar文件,看了看user guide确实比JDK提供的好多了,简单的几行代码就实现了发邮件的功能,以前实现过一个纯JavaMail带附件发邮件功能,代码复杂不说,现在回过头来都懒得看(主要是WEB项目注释少)。
贴上TEST代码看看,简单、清晰。只要稍加修改(邮件服务器地址、名称、密码)就可以了
简单邮件的发送:
package com.bulktree.mail;
import java.util.Date;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
publicclass SimpleMailTest {
publicstaticvoid main(String[] args) throws EmailException {
SimpleEmail email =new SimpleEmail();
//设置发送主机的服务器地址
email.setHostName("smtp.163.com");
//设置收件人邮箱
email.addTo("bulktree@126.com","bulktree");
//发件人邮箱
email.setFrom("bulktree@163.com","bulktree");
//如果要求身份验证,设置用户名、密码,分别为发件人在邮件服务器上注册的用户名和密码
email.setAuthentication("bulktree","123456");
//设置邮件的主题
email.setSubject("Hello, This is My First Email Application");
//邮件正文消息
email.setMsg("I am bulktree This is JavaMail Application");
email.send();
System.out.println("The SimpleEmail send sucessful!!!");
}
}
带附件邮件发送:
package com.bulktree.mail;
import java.net.MalformedURLException;
import java.net.URL;
importorg.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
publicclass AttachmentMailTest {
publicstaticvoid main(String[] args) throws EmailException, MalformedURLException {
// 创建一个Email附件
EmailAttachment emailattachment = newEmailAttachment();
emailattachment.setPath("/biao_05.jpg");
// emailattachment.setURL(new URL("http://www.blogjava.net/bulktree/picture/bulktree.jpg"));
emailattachment.setDisposition(EmailAttachment.ATTACHMENT);
emailattachment.setDescription("This is Smile picture");
emailattachment.setName("bulktree");
// 创建一个email
MultiPartEmail multipartemail =new MultiPartEmail();
multipartemail.setHostName("smtp.163.com");
multipartemail.addTo("bulktree@126.com","bulktree");
multipartemail.setFrom("bulktree@163.com","bulktree");
multipartemail.setAuthentication("bulktree","123456");
multipartemail.setSubject("This is a attachment Email");
multipartemail.setMsg("this a attachment Eamil Test");
//添加附件
multipartemail.attach(emailattachment);
//发送邮件
multipartemail.send();
System.out.println("The attachmentEmail send sucessful!!!");
}
}
- Java Mail send email
- java Send Email
- Java Send email
- send email Java发送邮件
- Java send email(No attachment)
- Java Send EMail with Attachment
- send email
- send email
- Email--Send
- Send Email
- HelloWorld系列-在Java中Send Email
- Use Java Mail API to Send Email
- How can I send an email by Java application?
- Send Email 发送邮件
- C# 2.0 send email
- Oracle send Email
- Send email in ABAP
- Send html Email
- 简单工厂模式SimpleFactory
- Chukwa 0.4.0 详细安装流程
- 权责发生制(Ledger accrual) in AX 2012
- windows server 2008 r2 共享文件夹
- linux和windows下,C/C++的sleep函数
- java Send Email
- tinyMCE 使用汇总
- ubuntu 服务管理运行程序 sysv-rc-conf
- oracle出错表
- PackageInstaller 原理简述 & PackageManagerService注记 & PackageParser和AndroidManifest.xml注记
- android system权限
- 比较两种成员变量赋值方式的优缺点
- [探讨]敏捷开发原则
- RMAN学习3——其他备份(控制文件、归档日志、spfile、备份集)