您的位置:首页 > 编程语言 > Java开发

java 发送邮件

2011-07-01 13:51 274 查看
很早前我们用mail.jar
和 activation.jar
两个包编写发送Email
的代码,虽然不难,但麻烦。
现在 apache
站点有一个 jarkata/commons/email
子项目,也为我们实现了发送 Email
的功能,在
http://jakarta.apache.org/commons/email/
把包 commons-email-1.0.jar
下来,自己要写的代码就十分少了,并且非常明了。
这个包的大小只有23K
,也就是9
个类而已,却能让您省不少事。

commons-email
是 apache
提供的一个开源的API
,是对javamail
的封装,因此在使用时要将javamail.jar
加 到 class path
中.

JavaMailAPI
可以到http://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/VerifyItem-Start/javamail1_4_4.zip?BundledLineItemUUID=IMSJ_hCxMJUAAAEwXVkWsuOh&OrderID=KDuJ_hCx3.cAAAEwSlkWsuOh&ProductID=7YGJ_hCxJLwAAAEtqgQg.6ly&FileName=/javamail1_4_4.zip

进行下载,
并将mail.jar
添加到classpath
即可.

JAF
框架可以到http://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/VerifyItem-Start/jaf-1_1_1.zip?BundledLineItemUUID=6dmJ_hCxRsMAAAEwi.8WsuOr&OrderID=q7GJ_hCxPwMAAAEwfO8WsuOr&ProductID=zFnACUFBlVoAAAEYhxc5AXt.&FileName=/jaf-1_1_1.zip

进行下载,
并将activation.jar
添加到classpath
即可.

commons-email
主要包括SimpleEmail,MultiPartEmail,HtmlEmail,EmailAttachment
四个类。

SimpleEmail:
发送简单的email,
不能添加附件

MultiPartEmail:
文本邮件,可以添加多个附件

HtmlEmail:HTML
格式邮件,同时具有MultiPartEmail
类所有“
功能”

EmailAttchment:
附件类,可以添加本地资源,也可以指定网络上资源,在发送时自动将网络上资源下载发送。

发送基本文本格式邮件:

SimpleEmail email = new SimpleEmail();
//smtp host
email.setHostName("mail.test.com");
//登陆邮件服务器的用户名和密码
email.setAuthentication("test","testpassword");
//接收人
email.addTo("jdoe@somewhere.org", "John Doe");
//发送人
email.setFrom("me@apache.org", "Me");
//标题
email.setSubject("Test message");
//邮件内容
email.setMsg("This is a simple test of commons-email");
//发送
email.send();


发送文本格式,带附件邮件:

//附件,可以定义多个附件对象
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("e://1.pdf");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
//
MultiPartEmail email = new MultiPartEmail();
//smtp host
email.setHostName("mail.test.com");
//登陆邮件服务器的用户名和密码
email.setAuthentication("test","testpassword");
//接收人
email.addTo("jdoe@somewhere.org", "John Doe");
//发送人
email.setFrom("me@apache.org", "Me");
//标题
email.setSubject("Test message");
//邮件内容
email.setMsg("This is a simple test of commons-email");
//添加附件
email.attach(attachment);
//发送
email.send();


发送HTML格式带附件邮件:

//附件,可以定义多个附件对象
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("e://1.pdf");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
//
HtmlEmail email = new HtmlEmail ();
//smtp host
email.setHostName("mail.test.com");
//登陆邮件服务器的用户名和密码
email.setAuthentication("test","testpassword");
//接收人
email.addTo("jdoe@somewhere.org", "John Doe");
//发送人
email.setFrom("me@apache.org", "Me");
//标题
email.setSubject("Test message");
//邮件内容
email.setHtmlMsg("This is a simple test of commons-email");
//添加附件
email.attach(attachment);
//发送
email.send();


下面提供一个完整的程序示例:

import org.apache.commons.mail.*;
public class SendEMail
{
public static void main ( String[] arg ) throws Exception
{
SimpleEmail email = new SimpleEmail ( );

// smtp host
email.setHostName ( "smtp.sina.com" );
// 登陆邮件服务器的用户名和密码
email.setAuthentication ( "tomcat", "123456" );
// 接收人
email.addTo ( "tomcat@yahoo.com.cn", "Zieckey" );
// 发送人
email.setFrom ( "tocat@sina.com", "Me" );
// 标题
email.setSubject ( "hello" );
// 邮件内容
email.setMsg ( "This is a simple test of commons-email" );
// 发送
email.send ( );

System.out.println ( "Send email successful!" );
}
}


commons-email 提供了 SimpleEmail、MultiPartEmail、HtmlEmail、EmailAttachment
等类,只需要您按正常思维简单的写几行代码就能发各种类型的 Email,一般我们用 JavaMail 发送 Email
会碰到中文乱码问题,主要是出现在把代码放在英文系统中执行时,处理方法是主题和内容使用 GBK 或 UTF-8 字符集。

http://jakarta.apache.org/commons/email/userguide.html
有 commons-email 的使用示例,如果
直接用第一个例子放在英文环境中发送带中文主题或内容的邮件也会出现乱码,

这里对第一个例子稍做改造,可以让发出的 Email 中文不出现乱码,如下:

import org.apache.commons.mail.SimpleEmail;

public class MailTo
{
/**
* @Author Unmi
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
//发送简单邮件
SimpleEmail email = new SimpleEmail();

email.setHostName("mail.2911.net");

//需要邮件发送服务器验证,用户名/密码
email.setAuthentication("broodwar", "xxxxxx");
email.addTo("fantasia@sina.com", "fantasia");
email.setFrom("broodwar@2911.net", "Broodwar");

//设置主题的字符集为UTF-8
email.setCharset("UTF-8");
email.setSubject("测试邮件主题");

email.buildMimeMessage();

//设置内容的字符集为UTF-8,先buildMimeMessage才能设置内容文本
email.getMimeMessage().setText("测试邮件内容","UTF-8");
email.sendMimeMessage();
}
}


再发个简单的处理中文乱码的例子:

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public class Send
{
public Send()
{

}

public static void main(String[] args)
{
send();
}

public static void send()
{
SimpleEmail email = new SimpleEmail();
email.setTLS(true);
email.setHostName("smtp.gmail.com");
email.setAuthentication("lifangxin2050@gmail.com", "sdzzczqqyyxy");

try
{
email.addTo("lifangxin2050@163.com");
email.setFrom("lifangxin2050@gmail.com");       //我方

email.setSubject("Java发送邮件测试");                 //标题

email.setCharset("GB2312");                     //设置Charset

email.setMsg("这是一封Java程序发出的测试邮件。");     //内容

email.send();

} catch (EmailException e) {
e.printStackTrace();
}
}
}


在以后在java中发邮件就用这个 commons-email 组件就好了,如果要发送 HTML 邮件或者带附件的邮件就学着 http://jakarta.apache.org/commons/email/userguide.html
中的例子做即可.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: