• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

JSP与JavaMail (三)

servlet/jsp 搞代码 7年前 (2018-06-18) 228次浏览 已收录 0个评论

4.试着编写第一个发送程序

在前面我们已对javaMail作了一些介绍,下面我们可试着写自己的程序了.

首先,我们先写一个撰写邮件的html程序index.htm,如下:
——————————————————————————————-

http://www.gaodaima.com/41989.htmljsP与JavaMail (三)

<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>
<title>撰写邮件</title>
</head>

<body>
<form name=”form1″ method=”post” action=”testmail.jsp“>
<table width=”75″ border=”0″ align=”center” cellspacing=”1″ bgcolor=”#006600″ class=”black”>
<tr bgcolor=”#FFFFFF”>
<td width=”24%”>收信人地址:</td>
<td width=”76%”>
<input name=”to” type=”text” id=”to”></td>
</tr>
<tr bgcolor=”#FFFFFF”>
<td>主题:</td>
<td>
<input name=”title” type=”text” id=”title”></td>
</tr>
<tr>
<td height=”107″ colspan=”2″ bgcolor=”#FFFFFF”>
<textarea name=”content” cols=”50″ rows=”5″ id=”content”></textarea></td>
</tr>
<tr align=”center”>
<td colspan=”2″ bgcolor=”#FFFFFF”>
<input type=”submit” name=”Submit” value=”发送”>
<input type=”reset” name=”Submit2″ value=”重置”>
</td>
</tr>
</table>
</form>
</body>
</html>

接着,我们再写一个处理程序testmail.jsp,如下:
—————————————————————————————–
<%@ page contentType=”text/html;charset=GB2312″ %>
<%request.setCharacterEncoding(“gb2312”);%><!–中文处理代码–>

<!–引入要用到的类库–>
<%@ page import=”java.util.*,javax.mail.*”%>
<%@ page import=”javax.mail.internet.*”%>

<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>
<title>发送成功</title>
</head>

<body>
<%
try{

//从html表单中获取邮件信息
String tto=request.getParameter(“to”);
String ttitle=request.getParameter(“title”);
String tcontent=request.getParameter(“content”);

Properties props=new Properties();//也可用Properties props = System.getProperties();
props.put(“mail.smtp.host”,”smtp.163.net”);//存储发送邮件服务器的信息
props.put(“mail.smtp.auth”,”true”);//同时通过验证
Session s=Session.getInstance(props);//根据属性新建一个邮件会话
s.setDebug(true);

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

//设置邮件
InternetAddress from=new InternetAddress(“[email protected]”);
message.setFrom(from);//设置发件人
InternetAddress to=new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);//设置收件人,并设置其接收类型为TO
message.setSubject(ttitle);//设置主题
message.setText(tcontent);//设置信件内容
message.setSentDate(new Date());//设置发信时间

//发送邮件
message.saveChanges();//存储邮件信息
Transport transport=s.getTransport(“smtp”);
transport.connect(“smtp.163.net”,”boy”,”iloveyou”);//以smtp方式登录邮箱
transport.sendMessage(message,message.getAllRecipients());//发送邮件,其中第二个参数是所有
//已设好的收件人地址
transport.close();

%>
<div align=”center”>
<p><font color=”#FF6600″>发送成功!</font></p>
<p><a href=”recmail.jsp”>去看看我的信箱</a><br>
<br>
<a href=”index.htm”>再发一封</a> </p>
</div>
<%
}catch(MessagingException e){
out.println(e.toString());
}
%>
</body>
</html>

**********************************注意***************************************

有好多书上和网上的文章在关键部分都是这样写testmail.jsp的,如下:

String tto=request.getParameter(“to”);
String ttitle=request.getParameter(“title”);
String tcontent=request.getParameter(“content”);
Properties props=new Properties();
props.put(“mail.smtp.host”,”smtp.163.net”);
Session s=Session.getInstance(props);
MimeMessage message=new MimeMessage(s);

InternetAddress from=new InternetAddress(“[email protected]”);
message.setFrom(from);
InternetAddress to=new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);

message.setSubject(ttitle);
message.setText(tcontent);
message.setSentDate(new Date());

Store store=s.getStore(“pop3”);
store.connect(“pop.163.net”,”boy”,”iloveyou”);//以pop3的方式登录邮箱
Transport transport=s.getTransport(“smtp”);
transport.send(message);
store.close();

事实上,这种方式并不可靠,因为很多电子邮局的smtp服务器要求我们通过验证,所以用这种方式发邮件时,只能发给同类邮箱(即相同smtp的邮箱),甚至有时同类邮箱也发不出去.以上两种方式我试过很多次,结果证明第一种方式是最可靠的.

好了,我相信你应该会写最简单的Email发送程序了.OK,下一次我们将说说怎样写发送HTML格式的邮件.

(待续)

欢迎大家阅读《JSP与JavaMail (三)》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:JSP与JavaMail (三)

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址