如何解决邮件概念的javax.mail.AuthenticationFailedException

时间:2015-07-07 06:05:33

标签: jsp javamail

执行下面提到的代码时,我收到错误javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful.虽然用户名和密码是正确的,我使用了mail.jar和`activation.jar。我正在使用eclipse来运行这个程序,我想要在gmail.pls发送邮件帮助我。谢谢。

  index.html

       <!DOCTYPE html>
                <html>
                    <head>
                        <title>Sending Mail Through JSP</title>
                        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                        <meta name="viewport" content="width=device-width">
                    </head>
                    <body bgcolor="khaki">
                        <form action="maildemo.jsp">
                            <table>
                                <tr><td><b><font color="red">To:
                                    </td>
                                    <td><b><b><input type="text" name="mail" value="Enter receiver mail-id"/><br/>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <b><font color="red">Subject:
                                    </td>
                                    <td>
                                        <input type="text"  name="sub" value="Enter Subject Line"><br/>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <b><font color="red">Message Text:
                                    </td>
                                    <td>
                                        <textarea rows="12" cols="80" name="mess"></textarea><br/>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <input type="submit" value="Send">
                                    </td>
                                    <td>
                                        <input type="reset" value="Reset">
                                    </td>
                                </tr>
                            </table>
                        </form>
                    </body>
                </html>


        maildemo.jsp

                <%@ page import="java.io.*,java.util.*,javax.mail.*,javax.mail.Service"%>
                <%@ page import="javax.mail.internet.*,javax.activation.*"%>
                <%@ page import="javax.servlet.http.*,javax.servlet.*"%>

                <%
                    //Creating a result for getting status that messsage is delivered or not!
                    String result;
                    // Get recipient's email-ID, message & subject-line from index.html page
                    final String to = request.getParameter("mail");
                    final String subject = request.getParameter("sub");
                    final String messg = request.getParameter("mess");

                    // Sender's email ID and password needs to be mentioned
                    final String from = "username";
                    final String pass = "password";

                    // Defining the gmail host
                    String host = "smtp.gmail.com";           

                    // Creating Properties object
                    Properties props = new Properties();

                    // Defining properties
                    props.put("mail.smtp.host", host);
                    props.put("mail.transport.protocol", "smtp");
                    props.put("mail.smtp.starttls.enable", "true");
                    props.put("mail.smtp.auth", "true");
                    props.put("mail.user", from);
                    props.put("mail.password", pass);
                    props.put("mail.smtp.port", "465");
                    props.put("mail.smtp.socketFactory.port", "465");//465
                    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

                    // Authorized the Session object.
                    Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() {
                        @Override
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(from, pass);
                        }
                    });

                    try {
                        // Create a default MimeMessage object.
                        MimeMessage message = new MimeMessage(mailSession);
                        // Set From: header field of the header.
                        message.setFrom(new InternetAddress(from));              

                        // Set To: header field of the header.
                        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
                        // Set Subject: header field
                        message.setSubject(subject);
                        // Now set the actual message                                             

                        message.setText(messg);
                        // Send message

                        Transport trans = mailSession.getTransport("smtp");

                        trans.send(message);
                        System.out.println("Sent message successfully....");

                        trans.close();

                        result = "Your mail sent successfully....";
                    } catch (MessagingException mex)
                    {
                        mex.printStackTrace();
                        result = "Error: unable to send mail....";
                    }
                %>
                <title>Sending Mail in JSP</title>
                <h1>
                    <center>
                        <font color="blue">Sending Mail Using JSP</font>
                </h1>
                <b><center>
                        <font color="red"> <% out.println(result);%>

                </b>

0 个答案:

没有答案