【问题标题】:Showing Error while sending email using java servlet?使用 java servlet 发送电子邮件时显示错误?
【发布时间】:2014-07-26 13:20:32
【问题描述】:

使用 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。

【问题讨论】:

  • 在您的帐户中使用真实的服务器地址而不是 127.0.0.1。(仔细阅读您的错误?
  • 邮件服务器需要正确配置。你的smtp主机为localhost,它应该是smtp服务器的ip。如果你没有smtp服务器,使用google提供的默认smtp配置,你可以测试邮件api。
  • 如何在 eclispe 中设置 smtp 服务器?

标签: java email servlets jakarta-mail


【解决方案1】:

您能否检查以下内容。 1) 您的 SMTP 服务器是否在您的本地计算机上配置。 2)尝试使用您打算发送邮件的机器的机器名称或IP地址而不是localhost。

还要提供一些关于您的邮件预期目标的详细信息(即该邮件打算投递到哪里)。基于此,您可能会得到更好的响应。

【讨论】:

  • 我想从 gmail 向 gmail 发送消息,我也尝试使用“smtp.gmail.com”而不是 localhost,但它显示了同样的问题
  • 尝试通过设置属性“mail.smtp.port”为 gmail smtp 服务器明确定义端口 587 和 465。您还可以通过在 cmd 提示符下使用 telnet 或使用一些 telnet 客户端(如 putty(如果在 Windows 上))来检查您是否能够访问有问题的服务器。这样,您可以确保在开始之前可以从您的计算机访问您尝试访问的服务器
  • 查看以下帖子 stackoverflow.com/questions/15372803/sending-gmail-error 可能有助于确定问题是由于防病毒软件阻止了 smtp 端口,在任何情况下,简单的 telnet 测试都会显示您是否能够访问或不是。
  • 查看sending to Gmail 的FAQ 条目和common mistakes 的FAQ 条目。
猜你喜欢
  • 2010-12-19
  • 2019-01-11
  • 2021-06-25
  • 2014-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-29
相关资源
最近更新 更多