使用jsp发送邮件

时间:2014-12-25 13:29:08

标签: java html jsp

以下代码之前有效,但现在显示错误,如javax.mail.AuthenticationFalied

请帮我添加库mail.jar和activation.jar

我从mysql数据库收到注册用户的电子邮件。

<%@page import="javax.mail.internet.InternetAddress"%>
<%@page import="java.util.*" %>
<%@page import="javax.mail.internet.MimeMessage"%>
<%@page import="javax.mail.Session" %>
<%@page import="java.util.Properties"%>
<%@page import="javax.mail.Message"%>
<%@page import="javax.mail.MessagingException"%>
<%@page import="javax.mail.Transport"%>
<%@page import="javax.mail.PasswordAuthentication"%>

     <%@ page language="java" import ="java.sql.*" %>
     <%@ page import="java.io.*" %>
     <%@ page import="java.text.ParseException" %>
     <%@ page import="java.text.SimpleDateFormat" %>
     <%@ page import="java.util.Date" %>

        <%

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

        Connection con=null;
        Statement stmt=null;
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        con=DriverManager.getConnection("jdbc:mysql://localhost:3306/discussion","root","root");
        Statement st1=con.createStatement();
        ResultSet rs1=st1.executeQuery("select uname from userreg where email='"+email+"'");

        if(rs1.next())
        {

        final String username="headind14@gmail.com";
        final String password="password";

        Random ran=new Random();
        String str=new String();
        for(int i=1;i<=5;i++)
            {
            str+=ran.nextInt(10);
            }


        Properties props=new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host","smtp.gmail.com");
        props.put("mail.smtp.port", 587);

        Session session1=Session.getInstance(props,
        new javax.mail.Authenticator() {
        public PasswordAuthentication getPasswordAuthentication(){
        return new PasswordAuthentication(username,password);
        }});    
        try
            {
             stmt=con.createStatement();
             ResultSet rs=stmt.executeQuery("select * from userreg");
             rs.next();
             Statement st=con.createStatement();
             st.executeUpdate("insert into forgot(uname,code) values('"+rs1.getString(1)+"','"+str+"')");


            Message message=new MimeMessage(session1);
            message.setFrom(new InternetAddress("headind14@gmail.com"));

            message.setRecipients( Message.RecipientType.TO,InternetAddress.parse(""+email+""));
            message.setSubject("chakrika infosolution");
            message.setSubject("Forgot Password");
            message.setContent("Hello "+rs1.getString(1)+",<br>Your Secret Code is: "+str+"<br/><a href='http://localhost:8084/Board/board/forgotpasswordd.jsp?uname="+rs1.getString(1)+"&code="+str+"'>Click Here</a> to reset your Password<br/><br/>","text/html; charset=UTF-8");
            Transport.send(message);
            Transport.send(message);

           %>
           <script>
              alert("Email send Successfully");
              window.location="../board/forgotpassword.jsp";
           </script>

           <%

            }
        catch(MessagingException e)
                               {
            out.println(e);
        }

        }
        else
        {        %>
           <script>
              alert("Invalid Email");
              window.location="../board/forgotpassword.jsp";
           </script>

        <%}
        %>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

我看到你的代码是正确的。使用 JavaMail Api 1.4.5 jar 或最新版本代替您使用的jar。