jsp的email功能

jsp的email功能就是利用某些邮箱的服务器进行发送,需要将mail.jar包和activation.jar包放入项目的lib文件夹中,然后运行下面代码就行了,驱动在附件中
红色为需要修改的部分:


<%@ 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","[color=red][b]smtp.163.com[/b][/color]");//存储发送邮件服务器的信息//[color=red][b]注意邮箱的smtp已开启,没有开启可以去邮箱开启。[/b][/color]props.put("mail.smtp.auth","true");//同时通过验证
Session s=Session.getInstance(props);//根据属性新建一个邮件会话
s.setDebug(true);

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

//设置邮件
InternetAddress from=new InternetAddress("[color=red][b]lzy600@163.com[/b][/color]");//[color=red]这里是邮箱账号,要有163的账号才行[/color]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[color=red]("smtp.163.com","lzy600","password");//[/color]以smtp方式登录邮箱
transport.sendMessage(message,message.getAllRecipients());//发送邮件,其中第二个参数是所有
//已设好的收件人地址
transport.close();

%>
<div align="center">
<p><font color="#FF6600">发送成功!</font></p>
<p><a href="http://mail.163.com">去看看我的信箱</a><br>
<br>
<a href="index.htm">再发一封</a> </p>
</div>
<%
}catch(MessagingException e){
out.println(e.toString());
}
%>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值