欢迎光临
我们一直在努力

javamail发送邮件-JSP教程,邮件相关

建站超值云服务器,限时71元/月

/*

在java版经常看到有人问如何用javamail发送邮件?如何接收邮件?如何访问多个文件夹等。问题零散,而历史的回复早已经淹没在问题的海洋之中。

本人之前所做过一个java项目,其中包含有webmail功能,当初为用java实现而对javamail摸索了一段时间,总算有点收获。看到论坛中的经常有此方面的问题,因此把我的一些经验帖出来,希望对大家有些帮助。

此篇仅介绍用javamail实现发送邮件功能,其中涉及smtp认证,邮件附件发送,及html内容邮件等。

其它有关多邮箱的实现,接收pop3邮件及imap等内容,将在后续文章中介绍。

如下程序需要:javamail,jaf包,j2ee.jar包含了上述两个包,建议大家安装j2sdkee或直接拷贝j2ee.jar,将其添加到jbuilder的library中,或系统classpath中

*/

package com.me.util.mail;

/**

* @author zhangkun aistill@msn.com

* @version 1.0

*/

import java.util.*;

import javax.mail.*;

import javax.mail.internet.*;

import java.util.date;

import javax.activation.*;

import java.io.*;

import com.me.util.*;

public class sendmail {

private mimemessage mimemsg; //mime邮件对象

private session session; //邮件会话对象

private properties props; //系统属性

private boolean needauth = false; //smtp是否需要认证

private string username = ""; //smtp认证用户名和密码

private string password = "";

private multipart mp; //multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成mimemessage对象

/**

*

*/

public sendmail() {

setsmtphost(getconfig.mailhost);//如果没有指定邮件服务器,就从getconfig类中获取

createmimemessage();

}

public sendmail(string smtp){

setsmtphost(smtp);

createmimemessage();

}

/**

* @param hostname string

*/

public void setsmtphost(string hostname) {

system.out.println("设置系统属性:mail.smtp.host = "+hostname);

if(props == null)props = system.getproperties(); //获得系统属性对象

props.put("mail.smtp.host",hostname); //设置smtp主机

}

/**

* @return boolean

*/

public boolean createmimemessage()

{

try{

system.out.println("准备获取邮件会话对象!");

session = session.getdefaultinstance(props,null); //获得邮件会话对象

}

catch(exception e){

system.err.println("获取邮件会话对象时发生错误!"+e);

return false;

}

system.out.println("准备创建mime邮件对象!");

try{

mimemsg = new mimemessage(session); //创建mime邮件对象

mp = new mimemultipart();

return true;

}

catch(exception e){

system.err.println("创建mime邮件对象失败!"+e);

return false;

}

}

/**

* @param need boolean

*/

public void setneedauth(boolean need) {

system.out.println("设置smtp身份认证:mail.smtp.auth = "+need);

if(props == null)props = system.getproperties();

if(need){

props.put("mail.smtp.auth","true");

}else{

props.put("mail.smtp.auth","false");

}

}

/**

* @param name string

* @param pass string

*/

public void setnamepass(string name,string pass) {

username = name;

password = pass;

}

/**

* @param mailsubject string

* @return boolean

*/

public boolean setsubject(string mailsubject) {

system.out.println("设置邮件主题!");

try{

mimemsg.setsubject(mailsubject);

return true;

}

catch(exception e) {

system.err.println("设置邮件主题发生错误!");

return false;

}

}

/**

* @param mailbody string

*/

public boolean setbody(string mailbody) {

try{

bodypart bp = new mimebodypart();

bp.setcontent("<meta http-equiv=content-type content=text/html; charset=gb2312>"+mailbody,"text/html;charset=gb2312");

mp.addbodypart(bp);

return true;

}

catch(exception e){

system.err.println("设置邮件正文时发生错误!"+e);

return false;

}

}

/**

* @param name string

* @param pass string

*/

public boolean addfileaffix(string filename) {

system.out.println("增加邮件附件:"+filename);

try{

bodypart bp = new mimebodypart();

filedatasource fileds = new filedatasource(filename);

bp.setdatahandler(new datahandler(fileds));

bp.setfilename(fileds.getname());

mp.addbodypart(bp);

return true;

}

catch(exception e){

system.err.println("增加邮件附件:"+filename+"发生错误!"+e);

return false;

}

}

/**

* @param name string

* @param pass string

*/

public boolean setfrom(string from) {

system.out.println("设置发信人!");

try{

mimemsg.setfrom(new internetaddress(from)); //设置发信人

return true;

}

catch(exception e)

{ return false; }

}

/**

* @param name string

* @param pass string

*/

public boolean setto(string to){

if(to == null)return false;

try{

mimemsg.setrecipients(message.recipienttype.to,internetaddress.parse(to));

return true;

}

catch(exception e)

{ return false; }

}

/**

* @param name string

* @param pass string

*/

public boolean setcopyto(string copyto)

{

if(copyto == null)return false;

try{

mimemsg.setrecipients(message.recipienttype.cc,(address[])internetaddress.parse(copyto));

return true;

}

catch(exception e)

{ return false; }

}

/**

* @param name string

* @param pass string

*/

public boolean sendout()

{

try{

mimemsg.setcontent(mp);

mimemsg.savechanges();

system.out.println("正在发送邮件….");

session mailsession = session.getinstance(props,null);

transport transport = mailsession.gettransport("smtp");

transport.connect((string)props.get("mail.smtp.host"),username,password);

transport.sendmessage(mimemsg,mimemsg.getrecipients(message.recipienttype.to));

//transport.send(mimemsg);

system.out.println("发送邮件成功!");

transport.close();

return true;

}

catch(exception e)

{

system.err.println("邮件发送失败!"+e);

return false;

}

}

/**

* just do it as this

*/

public static void main(string[] args) {

string mailbody = "<meta http-equiv=content-type content=text/html; charset=gb2312>"+

"<div align=center><a href=http://www.csdn.net> csdn </a></div>";

sendmail themail = new sendmail("smtp.msn.com");

themail.setneedauth(true);

if(themail.setsubject("标题") == false) return;

if(themail.setbody(mailbody) == false) return;

if(themail.setto("gates@msn.com") == false) return;

if(themail.setfrom("bill@msn.com") == false) return;

if(themail.addfileaffix("c:\\boot.ini") == false) return;

themail.setnamepass("user","password");

if(themail.sendout() == false) return;

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » javamail发送邮件-JSP教程,邮件相关
分享到: 更多 (0)

相关推荐

  • 暂无文章