`
DAOException
  • 浏览: 120885 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

commons-email发送邮件

    博客分类:
  • java
阅读更多

      好久没有更新自己的博客了,我胡汉三又回来啦。今天说一说使用commons-email来实现简单的发送邮件程序。这里可以发 送纯文本的,也可以发送带附件的,还可以发送HTML格式的邮件。废话少说,开始我们的邮件之旅。

      好吧,想发送邮件,我们第一步要干嘛呢?你说呢?我说啊,第一步当然要找到commons-email相关的类库了啊。这里具体要用到三个类库。mail.jar   commons-email-1.0.jar   activation.jar。这三个库在哪里下载呢?告诉你一个好的网址啊www.google.cn。绝对可以找到你想要的。^_^

       具体我也不细说,这里操作和引用其他第三方类库差不多。下面我贴三段代码,我想大家一看就会明白了。

 

public String send(){
		String result = null;
		SimpleEmail email = new SimpleEmail();
		//设置主机名称
		email.setHostName(rc.read("send"));
		//设置用户名,密码
		email.setAuthentication(rc.read("username"), rc.read("password"));
		//设置字符编码方式
		email.setCharset("GB2312");
		try {
			//设置发送源地址
			email.setFrom(from);
			//设置目标地址
			email.addTo(to);
			//设置主题
			email.setSubject(subject);
			//设置邮件内容
			email.setMsg(msg);
			//发送邮件
			email.send();
			result = "successful";
		} catch (Exception e) {
			e.printStackTrace();
			result = "not successful";
		}
		
		return result;
	}

 

 上面这段代码是发送纯文本文件的邮件。rc.read方法是自己自定义的从资源文件当中读取的相关邮件参数。

该项目还支持带附件的邮件发送。如下代码所示:

 

public String sendEnclosure() throws EmailException{
		String result = null;
		EmailAttachment emailattachment = new EmailAttachment();
           //设置附件路径
	       emailattachment.setPath(file);
	       //System.out.println(path);
	       emailattachment.setDisposition(EmailAttachment.ATTACHMENT);
	       //附件描述
	       emailattachment.setDescription("This is Smile picture");
	       /* 
            * 设置附件的中文乱码问题,解决附件的中文名称 乱码问题 
            */  
           BASE64Encoder enc = new BASE64Encoder();  
           //this.getName().getBytes()使用的是系统缺省的编码处理,这里是GBK  
           emailattachment.setName("=?GBK?B?"+enc.encode(file.getBytes())+"?=");  
             
           //attachment.setName(this.getName()); //不处理字符集的话,发送的附件中文名称是乱码 
	       
	       
	       //创建一个email
	       MultiPartEmail multipartemail = new MultiPartEmail();
	       //设置主机名称
	       multipartemail.setHostName(UserInfo.sendHost);
	       //设置字符编码
	       multipartemail.setCharset("GB2312");
	       //设置发送邮件目的地址
	       multipartemail.addTo(to);
	       //设置发送又见源地址
	       multipartemail.setFrom(from);
	       //设置用户名和密码
	       multipartemail.setAuthentication(UserInfo.username, UserInfo.password);
	       //设置主题
	       multipartemail.setSubject(subject);
	       //设置邮件内容
	       multipartemail.setMsg(msg);
	       //添加附件
	       multipartemail.attach(emailattachment);
	       //发送邮件
	       multipartemail.send();

	       result = "The attachmentEmail send sucessful!!!";

	       return result;
	}

 

 同样的,发用的邮件格式依然支持HTML格式,如下代码所示:

 

public String sendHtml() throws EmailException, MalformedURLException{
		// 创建邮件信息
		String result = null;
		HtmlEmail email = new HtmlEmail();
		email.setHostName(UserInfo.sendHost);
		email.addTo(to);
		email.setFrom(from);
		email.setSubject(subject);
		// embed the image and get the content id
		URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
		String cid = email.embed(url, "Apache logo");
		// set the html message
		email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");
		// set the alternative message
		email.setTextMsg("Your email client does not support HTML messages");
		// send the email
		email.send();
		
		return result;
	}

 好吧,三段代码都差上去了,其实发送起来很简单,大家一定都能看懂,你说对吧。看不懂的,告诉你啊,去google。hoho。

分享到:
评论
2 楼 alenstudent 2013-05-17  
最近在做一个发内嵌图片的程序,看了你这篇文章后,知道怎么做了,非常感谢
1 楼 kevinwong 2010-06-22  
emailattachment.setName("=?GBK?B?"+enc.encode(file.getBytes())+"?=");

请教里面的"=?GBK?B?"  +"?="是依据什么规则拼起来的?谢谢

相关推荐

Global site tag (gtag.js) - Google Analytics