使用servlet发送邮件时显示错误(在eclispe中)。我在类路径中也包含mail.jar和activation.jar。
我的servlet代码看起来像
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class Controller extends HttpServlet
{
private static final long serialVersionUID = 1L;
public Controller() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
doProcess(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
doProcess(request,response);
}
protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
final String user = request.getParameter("email");
final String pwd = request.getParameter("pwd");
String sub = request.getParameter("subject");
String body = request.getParameter("msg");
String to = "shiladitya1093@gmail.com";
//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host","localhost");
props.put("mail.smtp.auth","true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,pwd);
}
});
//Compose message
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(sub);
message.setText(body);
//Send message
Transport.send(message);
}catch(MessagingException e){
throw new RuntimeException(e);
}
request.setAttribute("sendMsg","Message successfully send.");
RequestDispatcher rd = request.getRequestDispatcher("contactus.jsp");
rd.forward(request, response);
}
}
错误如下:
HTTP状态500 - javax.mail.AuthenticationFailedException:535未定义SMTP服务器。在您的帐户中使用真实服务器地址而不是127.0.0.1。
答案 0 :(得分:0)
您可以查看以下内容吗? 1)您的SMTP服务器是否在本地计算机上配置。 2)尝试使用您要发送邮件的机器的机器名称或IP地址而不是localhost。
还要提供一些有关邮件目标的详细信息(即此邮件的目的地在何处)。在此基础上你可以得到更好的回应。