我想通过表单发布方法发送邮件,但是我得到了
HTTP状态500?服务器内部错误
执行:javax.servlet.ServletException:Servlet执行引发异常 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
注意:我已经安装了库:javax.mail.jar
和activation.jar
在我的Mail.jsp
页面上
<form action="ServlateMail" method="post">
To:<input type="text" name="to" /><br/>
Subject:<input type="text" name="subject" /><br/>
Message:<input type="text" name="message" /><br/>
Your Email id:<input type="text" name="user" ><br/>
Password<;input type="password" name="pass" /><br/>
<input type="submit" value="send" />
</form>
在我的ServlateMail
中:
package controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utils.Sendmail;
public class ServletMail extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset-UTF-8");
PrintWriter out = response.getWriter();
String to = request.getParameter("to");
String subject = request.getParameter("subject");
String message = request.getParameter("message");
String user = request.getParameter("user");
String pass = request.getParameter("pass");
Sendmail.sendmailer(to,subject, message, user, pass);
System.out.println("Mail send successfully");
}
}
在我的Java课中:
package utils;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class Sendmail
{
public static void sendmailer(String to, String sub,
String msg, final String user,final String pass)
{
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props,new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user,pass);
}
});
try
{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(sub);
System.out.print("testing");
message.setText(msg);
Transport.send(message);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}