冬日暖阳
只要开始就不会晚,只要进步就有空间...

需要用到的包:activation.jar,mail.jar(本人用的javamail1_4_4.zip中的mail.jar)

1  发送文本格式的邮件

例1  发送文本格式的邮件。

发送文本格式的邮件是比较简单的程序。为方便演示,先编写一个用来输入所发邮件相关内容的JSP页面,包括发往邮箱的地址、发送邮件的主题及邮件的内容。再用一个页面来接收输入数据并发送邮件。输入页面的代码如下:writemail.jsp

 

View Code
<%@ page contentType="text/html;charset=GB2312" %>

<html>

<head>

<title>写邮件内容</title>

</head>

<body>

<form name="form1" method="post" action="sendmail.jsp">

<table width="100" border="1" align="center" cellspacing="1">

<tr >

<td width="34%">收信人地址(邮箱):</td>

<td width="66%"><input name="to" type="text" id="to"></td>

</tr>

<tr >

<td>主题:</td>

<td><input name="title" type="text" id="title"></td>

</tr>

<tr>

<td height="107" colspan="2">

<textarea name="content" cols="50" rows="5" id="content">

</textarea></td>

</tr>

<tr align="center">

<td colspan="2">

<input type="submit" name="Submit" value="发送">

<input type="reset" name="Submit2" value="重输">

</td>

</tr>

</table>

</form>

</body>

</html>

 

输入完邮件内容后,将提交给发送邮件的处理页面sendmail.jsp,源代码如下所示:sendmail.jsp

View Code
<%@ page contentType="text/html;charset=gb2312" %>

<!--引入要用到的类库-->

<%@ page import="java.util.*,javax.mail.*"%>

<%@ page import="javax.mail.internet.*"%>

<%!

public String codeToString(String str)

{
//处理中文字符串的函数

String s
=str;

try

{

byte tempB[]=s.getBytes("ISO-8859-1");

s
=new String(tempB);

return s;

}

catch(Exception e)

{

return s;

}

}

%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title>发送邮件的结果</title>

</head>

<body>

<%

try{

//从html表单中接收邮件信息

String to_mail
=codeToString(request.getParameter("to"));

String to_title
=codeToString(request.getParameter("title"));

String to_content
=codeToString(request.getParameter("content"));

//--------建立邮件会话--------

Properties props
=new Properties();//也可用Properties props = System.

getProperties();

props.put(
"mail.smtp.host","smtp.126.com"); //存储发送邮件服务器的信息

props.put(
"mail.smtp.auth","true"); //同时通过验证

Session s
=Session.getInstance(props); //根据属性新建一个邮件会话

s.setDebug(
true);

//----由邮件会话新建一个消息对象----

MimeMessage message
=new MimeMessage(s); //由邮件会话新建一个消息对象

//--------设置邮件--------

InternetAddress from
=new InternetAddress("tougao-email@126.com");

message.setFrom(from);
//设置发件人

InternetAddress to
=new InternetAddress(to_mail);

message.setRecipient(Message.RecipientType.TO,to);

//设置收件人,并设置其接收类型为TO

message.setSubject(to_title);
//设置主题

message.setText(to_content);
//设置信件内容

message.setSentDate(
new Date()); //设置发信时间

//--------发送邮件--------

message.saveChanges();
//存储邮件信息

Transport transport
=s.getTransport("smtp");

//--以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址;第二个参数为用

户名;第3个参数为密码

transport.connect(
"smtp.126.com","tougao-email","8807698");

//发送邮件,其中第二个参数是所有已设好的收件人地址

transport.sendMessage(message,message.getAllRecipients());

transport.close();

%>

<div align="center">

<p><%=to_mail%>发送邮件成功!<br>

邮件主题:
<%=to_title%><br>

邮件内容:
<%=to_content%></p>

</div>

<%

}
catch(MessagingException e){

out.println(
"发送失败!");

}

%>

</body>

</html>

程序先要接收邮件的相关内容,用Properties类的对象来储存邮件会话的属性;用Session对象的getInstance()方法建立一个邮件会话,再设置邮件的相关内容;用Transport类对象transport来建立连接,连接时要给出SMTP服务器的用户名和密码,再发送邮件。如果在此过程中出现异常则报告发送邮件失败。本例的运行结果如图12-2所示。读者不妨登录接收邮件的邮箱试试看是否发送成功(当然前提是要可连接到 Internet)。

2  发送HTML邮件

现在的邮件服务器大都支持HTML格式的邮件,即超文本的邮件。HTML邮件比纯文本邮件更具表现力,内容既可以做得美观大方,又可以做得丰富多彩。

例2  发送HTML邮件。

继续利用例1中的输入邮件内容的JSP页面,只是修改表单的action属性值为处理邮件的页面,这里为sendmailHTML.jsp。sendmailHTML.jsp源代码如下所示。sendmailHTML.jsp

 

View Code
<%@ page contentType="text/html;charset=gb2312" %>

<%@ page import="java.util.*,javax.mail.*"%>

<%@ page import="javax.mail.internet.*"%>

<%!

public String codeToString(String str)

{
//处理中文字符串的函数

String s
=str;

try

{

byte tempB[]=s.getBytes("ISO-8859-1");

s
=new String(tempB);

return s;

}

catch(Exception e)

{

return s;

}

}

%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title>发送邮件处理结果</title>

</head>

<body>

<%

try{

//----接收邮件相关内容----

String to_mail
=codeToString(request.getParameter("to"));

String to_title
=codeToString(request.getParameter("title"));

String to_content
=codeToString(request.getParameter("content"));

Properties props
=new Properties();

//------建立邮件会话------

props.put(
"mail.smtp.host","smtp.126.com");

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

Session s
=Session.getInstance(props);

s.setDebug(
true);

MimeMessage message
=new MimeMessage(s);

//--给消息对象设置发件人、收件人、主题、发信时间--

InternetAddress from
=new InternetAddress("tougao-email@126.com");

message.setFrom(from);
//发件人

InternetAddress to
=new InternetAddress(to_mail);

message.setRecipient(Message.RecipientType.TO,to);

message.setSubject(to_title);

message.setSentDate(
new Date());

//------给消息对象设置内容------

//新建一个存放信件内容的BodyPart对象

BodyPart mdp
=new MimeBodyPart();

//给BodyPart对象设置内容和格式的编码方式

mdp.setContent(to_content,
"text/html;charset=gb2312");

//新建一个MimeMultipart对象用来存放BodyPart对象(事实上可以存放多个)

Multipart mm
=new MimeMultipart();

//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)

mm.addBodyPart(mdp);

//把mm作为消息对象的内容

message.setContent(mm);

message.saveChanges();

Transport transport
=s.getTransport("smtp");

//以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址;第二个参数为用户

名;第三个参数为密码

transport.connect(
"smtp.126.com","tougao-email","8807698");

transport.sendMessage(message,message.getAllRecipients());

transport.close();

%>

<div align="center">

<p>发送HTML邮件到<%=to_mail%>成功!</p>

</div>

<%

}
catch(MessagingException e){

out.println(
"发送邮件失败!");

}

%>

</body>

</html>

 

这里用的是MimeMultipart对象来存放BodyPart,即HTML文件的具体内容。在设置内容时要设置对象的格式,这里是"text/html;charset=gb2312"。

3  发送带附件的邮件

例3  发送带附件的邮件。

继续利用例1的输入邮件内容的JSP页面,只是修改表单的action属性值为处理邮件的页面,这里为sendmailFile.jsp。sendmailFile.jsp源代码如下所示。sendmailFile.jsp

 

View Code
<%@ page contentType="text/html;charset=gb2312" %>

<%@ page import="java.util.*,javax.mail.*"%>

<%@ page import="javax.mail.internet.*"%>

<%@ page import="javax.activation.*"%><!--要发送附件必须引入该库-->

<%@ page import="java.net.*"%><!--要用到URL类-->

<%!

public String codeToString(String str)

{
//处理中文字符串的函数

String s
=str;

try

{

byte tempB[]=s.getBytes("ISO-8859-1");

s
=new String(tempB);

return s;

}

catch(Exception e)

{

return s;

}

}

%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title>发送邮件结果</title>

</head>

<body>

<%

try{

String to_mail
=codeToString(request.getParameter("to"));

String to_title
=codeToString(request.getParameter("title"));

String to_content
=codeToString(request.getParameter("content"));

Properties props
=new Properties();

props.put(
"mail.smtp.host","smtp.126.com");

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

Session s
=Session.getInstance(props);

s.setDebug(
true);

MimeMessage message
=new MimeMessage(s);

//--给消息对象设置发件人、收件人、主题、发信时间--

InternetAddress from
=new InternetAddress("tougao-email@126.com");

message.setFrom(from);

InternetAddress to
=new InternetAddress(to_mail);

message.setRecipient(Message.RecipientType.TO,to);

message.setSubject(to_title);

message.setSentDate(
new Date());

//新建一个MimeMultipart对象用来存放多个BodyPart对象

Multipart mm
=new MimeMultipart();

//------设置信件文本内容------

//新建一个存放信件内容的BodyPart对象

BodyPart mdp
=new MimeBodyPart();

//给BodyPart对象设置内容和格式/编码方式

mdp.setContent(to_content,
"text/html;charset=gb2312");

//将含有信件内容的BodyPart加入到MimeMultipart对象中

mm.addBodyPart(mdp);

//--设置信件的附件1(自定义附件:直接将所设文本内容加到自定义文件中作为附件发送)

//新建一个存放附件的BodyPart

mdp
=new MimeBodyPart();

//新建一个DataHandler对象,并设置其内容和格式/编码方式

DataHandler dh
=new DataHandler("JavaMail附件测试","text/plain; charset= gb2312");

//加上这句将作为附件发送,否则将作为信件的文本内容

mdp.setFileName(
"fj.txt");

//给BodyPart对象设置内容为dh

mdp.setDataHandler(dh);

//将含有附件的BodyPart加入到MimeMultipart对象中

mm.addBodyPart(mdp);

//设置信件的附件2(用本地机上的文件作为附件)

mdp
=new MimeBodyPart();

FileDataSource fds
=new FileDataSource("d:/story.txt");

dh
=new DataHandler(fds);

mdp.setFileName(
"story.txt");//可以和原文件名不一致

mdp.setDataHandler(dh);

mm.addBodyPart(mdp);

message.setContent(mm);
//把mm作为消息对象的内容

message.saveChanges();

Transport transport
=s.getTransport("smtp");

//以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址;第二个参数为用户

名;第三个参数为密码

transport.connect(
"smtp.126.com","tougao-email","8807698");

transport.sendMessage(message,message.getAllRecipients());

transport.close();

%>

<div align="center">

<p><font color="#FF6600">发送带附件的邮件到<%=to_mail%>成功!</font></p>

</div>

<%

}
catch(MessagingException e){

out.println(e.toString());

}

%>

</body>

</html>
posted on 2011-03-04 17:49  zstudy  阅读(2994)  评论(2)    收藏  举报