通过Servlet发送电子邮件时,在消息正文中添加链接

时间:2018-07-12 07:32:45

标签: email jsp servlets mysql-workbench reset-password

在我使用jsp和servlet的Web申请人中,有一个成员注册,但是当成员忘记密码时,我想进行帐户恢复,当他们单击该链接并且我拥有此servlet时,它将发送一个链接以重置其密码。可以在需要时向用户发送电子邮件,但我不知道设计电子邮件的技术,可以像其他所有重置密码电子邮件一样在邮件正文中附加链接和图像。

package members;

import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.System.console;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(name = "ForgotPassword", urlPatterns = {"/ForgotPassword"})
public class ForgotPassword extends HttpServlet {

    /**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out=response.getWriter();

    String email= request.getParameter("email");


    members s= new members();

    s.setMemberEmail(email);

    members status=membersDAO.getMemberByEmail(s);

    if(status != null){
        try{
        String host ="smtp.gmail.com" ;
        String user = "xx";
        String pass = "xx";
        String to = email;
        String from = "xx";
        String subject = "Reset Password";
        String messageText = ""; //the email body
        boolean sessionDebug = false;

        Properties props = System.getProperties();

        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.required", "true");

        java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        Session mailSession = Session.getDefaultInstance(props, null);
        mailSession.setDebug(sessionDebug);
        Message msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress(from));
        InternetAddress[] address = {new InternetAddress(to)}; //address of sender
        msg.setRecipients(Message.RecipientType.TO, address); //receiver to email
        msg.setSubject(subject); msg.setSentDate(new Date()); //message send date
        msg.setText(messageText); //actual message

       Transport transport=mailSession.getTransport("smtp");
       transport.connect(host, user, pass);
       transport.sendMessage(msg, msg.getAllRecipients());
       transport.close();
       System.out.println("Message send successfully");
    }catch(Exception ex)
    {
        System.out.println(ex);
    }
        request.getRequestDispatcher("thanks.jsp").include(request, response);
    }else{
        response.sendRedirect("error.jsp");
    }

    out.close();

}
}

something like this..

我所能做的就是使用此代码输入一些短信。

0 个答案:

没有答案