QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 646|回复: 1

自己修改的一段mail发送程序!(ZT)

[复制链接]
发表于 2004-4-6 10:32:30 | 显示全部楼层 |阅读模式
自己修改的一段mail发送程序!
               
       
因需要基于WEB的MAIL发送程序(其实就是取回密码时发送密码),在网上看到一段MAIL程序,现在将其修改为WEB界面SERVLET处理并提交数据库寻找用户名的以发送回密码。大家可研究交流下。另外使用的时候建个表单,把用户名提交到getpost就可以了。

有三个类,一个是数据库类(dbconn.java),一个是MAIL发送类(mymail.java),还有一个是

SERVLET取得表单用户名及一些返回给用户的信息(getpost.java)。

调试此程序需要下载JAVAMAIL包与JAF包。可到SUN公司去下载。

dbconn.java类:

package webmail;
import javax.sql.*;
import java.sql.*;

/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/

public class dbconn {

String uname;//数据库帐号

String password;//数据库密码

String drivername;//驱动名称

String url;//数据库地址

Connection conn;//连接对象

public dbconn()

{

}

public Connection getconn() throws SQLException,Exception

{

username="root";

password="";

drivername="org.gjt.mm.mysql.Driver";

url="jdbc:mysql://localhost/news";

Class.forName( drivername ).newInstance();

conn=DriverManager.getConnection(url,username,password);

return conn;

}

}


getpost.java类:

package webmail;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
import java.sql.*;
import java.util.*;
import java.io.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/

public class getpost extends HttpServlet{

String uname="";//用户名

String pass=null;//用户密码

Connection conn=null;//连接对象

ResultSet rs=null;//结果集

String mail=null;//用户MAIL地址

public getpost()

{

}


public void doPost(HttpServletRequest request,HttpServletResponse response)

throws ServletException,IOException

{

PrintWriter out=response.getWriter();

this.username=(String)request.getParameter("username");

System.out.println("取得用户名");

try

{

Check(username);

}

catch(Exception e)

{

System.out.println("连接出错!");

}

if(pass!=null)

{

System.out.println("准备建立MAIL对象");

mymail themail = new mymail();

themail.setNeedAuth(true);//设置是否需要MAIL帐号与密码验证

themail.setSubject("你的密码");

themail.setBody("你的用户名是:"+this.username+"
你的密码是:"+this.pass);

themail.setTo(this.mail);

themail.setFrom("[email protected]");

themail.setNamePass("username","password");//在此输入你的MAIL帐号与密码

if(themail.sendout() == false)

{

return;

}

String ok=new String("发送成功,密码已发送到你注册时的MAIL信箱地址,请查收!".getBytes("GBK"),"ISO-8859-1");

out.println("<script language='javascript'>alert('"+ok+"');history.back();</script>");

}

else

{

System.out.println("对不起,发送失败!");


String no=new String("对不起,没有找到你的用户名!".getBytes("GBK"),"ISO-8859-1");

out.println("<script language='javascript'>alert('"+no+"');history.back();</script>");

return;

}

}

public void Check(String uname) throws Exception,SQLException

{

try

{

dbconn get=new dbconn();

conn=get.getconn();

String name=username;

Statement query=conn.createStatement();

String sql="select * from userlist where uname='"+name+"'";

rs=query.executeQuery(sql);

System.out.println("准备取得密码");

if(!rs.next())

{

System.out.println("对不起,没有找到你的用户名!");

this.pass=null;

return;

}

else

{

this.mail=rs.getString("f_email");

this.pass=rs.getString("password");

}

}


catch(Exception e)

{

System.out.println("数据出错!");

}

}

}


mymail.java类:

package webmail;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Date;
import javax.activation.*;
import java.io.*;
public class mymail {

private MimeMessage mimeMsg; //MIME邮件对象

private Session session; //邮件会话对象
private Properties props; //系统属性
private boolean needAuth = false; //smtp是否需要认证

private String uname = ""; //smtp认证用户名和密码
private String password = "";

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



/**
*
*/
public mymail() {
setSmtpHost("smtp.21cn.com");//SMTP服务器
createMimeMessage();
}

public mymail(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=??????>"+mailBody,"text/html;charset=??????");
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;
}
}

}
发表于 2004-4-11 10:07:58 | 显示全部楼层
有意思 ,好好玩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-8 14:07 , Processed in 0.048582 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表