在这里,我想使用Java和JSP发送邮件。

index.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="mailJSP.jsp">
        <table>
            <tr><td><b><font color="red">To:
                </td>
                <td><b><b><input type="text" name="mail" value="Enter sender 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>


mailJSP.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*" %>
<%
//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 = "my mail";
final String pass = "my pass";

// 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.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.user", from);
props.put("mail.password", pass);
props.put("mail.port", "465");

// 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.send(message);
    result = "Your mail sent successfully....";
} catch (MessagingException mex) {
    mex.printStackTrace();
    result = "Error: unable to send mail....";
}
%>`enter code here`
 <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>


我还想补充一点,我使用Netbeans IDE和Apache Server,并且已经添加了它所需的所有jar文件,但是仍然出现Exception并以“ Unable to send”结尾。所以我很困惑我在哪里犯错了?

堆栈跟踪:

Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:476)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


例外

exception

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 74 in the jsp file: /mailJSP.jsp
The local variable result may not have been initialized
71: %>
72: <title>Sending Mail in JSP</title>
73: <h1><center><font color="blue">Sending Mail Using JSP</font></h1>
74: <b><center><font color="red"><% out.println(result);%></b>


希望它能增加更多信息!

最佳答案

An error occurred at line: 74 in the jsp file: /mailJSP.jsp


局部变量结果可能尚未初始化

String result = new String();


初始化变量

对于第二个例外

private final String SSL_FACTORY  =  "javax.net.ssl.SSLSocketFactory";
int port = 465;
if ("smtp.gmail.com".equals(host)) {
    props.setProperty("mail.smtp.socketFactory.class",SSL_FACTORY);
    props.setProperty("mail.smtp.socketFactory.fallback","false");
    props.setProperty("mail.smtp.socketFactory.port",String.valueOf(port));
  }

10-01 17:51