一段可以使用的JSP发邮简代码

最近学习用JSP做通过邮箱找回密码的方法

看了很多资料 也写了很多代码

但一直出错 后来还是一位朋友和我一起做出来了

现在共享给大家 希望对大家学习有帮助

 

<%@ 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.*"%>

<jsp:useBean id="userInfo" class="www.UserInfo" scope="page">
<jsp:setProperty name="userInfo" property="*"/> 
</jsp:useBean>
<jsp:useBean id="MD5" class="www.MD5" scope="session"/>
<jsp:useBean id="regist" class="www.UserRegist" scope="page"/>


<%!
     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>
<body>
<%
    regist.setUserInfo(userInfo);
    String email = request.getParameter("email");
    String yanzhengma = request.getParameter("yanzhengma");
    String tupianyanzhengma = session.getAttribute("rand").toString();
   if(yanzhengma.equals(tupianyanzhengma))
    {
     regist.regist();
     
    }
    else
    {
     response.sendRedirect("register.jsp?email="+email);
     
    }
 
    try{
           String to_mail=codeToString(request.getParameter("email"));
           String to_title="标题";
           String to_content=" 内容 &nbsp;&nbsp;&nbsp;<a href='http://192.168.1.115:8888/Mzjkw3/mzjkwqt/active.jsp?jihuo="+ to_mail +"'>激活</a> &nbsp;&nbsp;&nbsp;<a href='http://192.168.1.115:8888/Mzjkw3/mzjkwqt/active1.jsp?jihuo="+ to_mail +"'>取消激活</a>";
           Properties props=new Properties();
           props.put("mail.smtp.host","smtp.163.com");
           props.put("mail.smtp.auth","true");
           Session s=Session.getInstance(props);
           s.setDebug(true);
          
           MimeMessage message=new MimeMessage(s);
          
           //给消息对象设置发件人、收件人、主题、发信时间
           InternetAddress from=new InternetAddress("liumin1182005@163.com");
           message.setFrom(from);
           InternetAddress to =new InternetAddress(to_mail);
           message.setRecipient(Message.RecipientType.TO,to);
           message.setSubject(to_title);
           message.setSentDate(new Date());
          
           Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放多个BodyPart对象
          
           //设置信件文本内容
           BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
           mdp.setContent(to_content,"text/html;charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
           mm.addBodyPart(mdp);//将含有信件的内容的BodyPart加入到MimeMultipart对象中
          
         
          
           message.setContent(mm);//把mm作为消息对象的内容
          
           message.saveChanges();
           Transport transport=s.getTransport("smtp");
           //以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址,第二个参数为用户名,第三个参数为密码
           transport.connect("smtp.163.com","你的163油箱","密码");
           transport.sendMessage(message,message.getAllRecipients());
           transport.close();
          
    }catch(MessagingException e){
          out.println(e.toString());
          }
%>
<br><br>
<a href="index.jsp">注册成功!<br>请您注意查收激活邮件,我们将在24小时内发送激活邮件到您的注册信箱!点击此处返回主页面!</a>
</body>
</html>

写此代码时 用了mail.jar和activation.jar 两个包

下载他们然后加入环境变量

后者扶植到/jre/lib/ext/目录下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值