一段可以使用的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("[email protected]");
           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/目錄下

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章