欢迎光临
我们一直在努力

使用JAVAMAIL发邮件的一个例子-JSP教程,邮件相关

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

首先引入j2ee的包路径到classpath,已经安装jbuilder的可以在它的安装路径lib目录下找

到mail.jar,activation.jar,将这两个包加入系统的环境变量,那么即使不用jbuilder开发的话也可以

使用了.

程序:

import java.io.*;

import java.util.*;

import javax.mail.*;

import javax.mail.internet.*;

/**

* <p>title: 简易mailbean</p>

* <p>description: 提供简便的mail发送功能(/p>

* <p>copyright: copyright (c) 2003</p>

* @version 1.0

*/

class extendstring {

public extendstring() {

}

/**

去掉字符串两端的空白字符,并将字符串转化为中国的标准字符gb2312的字符串.

*/

public string cs(string str) { //去掉字符串2端的空白字符

try {

if (str == null)

return "";

str = str.trim();

if (str == null)

return "";

str = new string(str.getbytes("8859_1"), "gbk");

}

catch (exception e) {

system.out.println(e);

}

return str;

}

}

public class sendmail

{

private string errmsg = "";

private extendstring exstr = new extendstring();

private string sender = "";//发件人地址

private string smtphost = "";//邮件发送服务器(smtp)

private string user = ""; //登录用户名

private string password = "";//登录密码

private string subject = "";//mail主题

public sendmail()

{

this.setpropertiesattri();

}

private void setpropertiesattri()

{

try

{

inputstream is = getclass().getresourceasstream("mailserver.properties");

properties prop = new properties();

prop.load(is);

this.setsmtphost(prop.get("smtphost").tostring());

this.setuser(prop.get("user").tostring());

this.setpassword(prop.get("password").tostring());

this.setsender(prop.get("sender").tostring());

this.setsubject(exstr.cs(prop.get("subject").tostring()));

}

catch(exception ex)

{

system.err.println("ex1 in sendmail.java:"+ex.tostring());

}

}

/** 设置发件人地址 */

public void setsender(string sender)

{

this.sender = sender;

}

public string getsender()

{

return sender;

}

/** 设置邮件发送服务器(smtp) */

public void setsmtphost(string smtphost) {this.smtphost = smtphost;}

public string getsmtphost() {return smtphost;}

/** 设置登录用户名 */

public void setuser(string user)

{

this.user = user;

}

public string getuser()

{

return user;

}

/** 设置登录密码 */

public void setpassword(string password)

{

this.password = password;

}

public string getpassword()

{

return password;

}

/** 设置mail主题 */

public void setsubject(string subject)

{

this.subject = subject;

}

public string getsubject()

{

return subject;

}

/**

* 使用smtp发送邮件 主程序

* @throws messagingexception mail发送失败

*/

public void smtp(string receiver,string content) throws messagingexception

{

if (smtphost == null) throw new messagingexception("smtphost not found");

if (user == null) throw new messagingexception("user not found");

if (password == null) throw new messagingexception("password not found");

properties properties = new properties();

properties.put("mail.smtp.host", smtphost);//设置smtp主机

properties.put("mail.smtp.auth", "true");//使用smtp身份验证

session session = session.getdefaultinstance(properties,

new authenticator(){

public passwordauthentication getpasswordauthentication(){

return new passwordauthentication(user, password);

}

});

//获得邮件会话对象

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

if (sender != null)//设置发件人地址

{

mimemsg.setfrom(new internetaddress(sender));

}

if (receiver != null)//设置收件人地址

{

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

}

if (subject != null)//设置邮件主题

{

mimemsg.setsubject(subject, "gbk");

}

mimebodypart part = new mimebodypart();//mail内容部分

part.settext(content == null ? "" : content, "gbk");

//设置邮件格式为html cqc

part.setcontent(content.tostring(),"text/html;charset=gbk");

multipart multipart = new mimemultipart();

multipart.addbodypart(part);//在 multipart 中增加mail内容部分

mimemsg.setcontent(multipart);//增加 multipart 到信息体

mimemsg.setsentdate(new date());//设置发送日期

transport.send(mimemsg);//发送邮件

}

/** 解析地址集合字符串 */

private internetaddress[] parse(string addressset) throws addressexception

{

arraylist list = new arraylist();

stringtokenizer tokens = new stringtokenizer(addressset, ";");

while (tokens.hasmoretokens())

{

list.add(new internetaddress(tokens.nexttoken().trim()));

}

internetaddress[] addressarray = new internetaddress[list.size()];

list.toarray(addressarray);

return addressarray;

}

/**

* 供外部调用的接口

*/

public boolean sendmails(string mail,string content)

{

int maillen = 0 ;

int contentlen= 0;

if (mail == null||content==null)

{

return false;

}

try

{

this.smtp(mail,content);

}

catch(exception ex)

{

system.err.println("ex2 in sendmail.java:"+ex.tostring());

}

return true;

}

public static void main (string[] args)

{

sendmail mail = new sendmail();

string email = "feng_lei@ecfounder.com;ladofwind@163.com";

string content = "账号:123 密码:123 <br/>感谢您注册!<br/><a href=http://www.xxxx.com.cn target=_blank>www.xxxx.com.cn</a><br/>此致<br/>xxxx <br/>即日";

try

{

mail.sendmails(email,content);

}

catch (exception ex)

{

system.err.println("ex33:"+ex.tostring());

}

}

}

编译通过后,在class文件的目录下建立一个文本文件mailserver.properties,

格式:

smtphost=smtp.163.com

user=user

password=pwd

sender=csdn@csdn.com

subject=hello

运行程序即可实现邮件发送!

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

相关推荐

  • 暂无文章