0

我正在创建一个表单,它将在用户完成详细信息并单击提交后通过电子邮件发送详细信息。

使用 JavaMail 提交邮件:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

    PrintWriter out = response.getWriter();
    try {
        String host = "localhost";
        String from = "root@localhost.localdomain";

        try {
        Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.debug", "true");

        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(true);
        Transport transport = session.getTransport("smtp");

        MimeMessage message = new MimeMessage(session);
        Address fromAddress = new InternetAddress("root@localhost.localdomain");

        message.setFrom(fromAddress);

        InternetAddress to = new InternetAddress("sendToAliases@localhost.localdomain");
        message.addRecipient(Message.RecipientType.TO, to);

        message.setSubject("Email Details Sending");
        message.setText("This is my testing content.");

        transport.connect(host, from);
        message.saveChanges();
        Transport.send(message);
        transport.close();
    } finally { 
        out.close();
    }
} 

我正在为 sendToAliases@localhost.localdomain 使用电子邮件别名,这意味着我可以有 4 个来自 sendToAliases 的电子邮件别名。但是,在部署和运行上述邮件文件时,我无法访问任何电子邮件。谁能给我建议?

谢谢你。

4

1 回答 1

2
  • 你检查过日志文件吗?
  • 运行程序时是否出现异常或错误?
  • 你有在 localhost 中运行的SMTP服务器吗?
  • SMTP 服务器是否接受来自 localhost 的连接?
  • 您可以使用普通电子邮件客户端通过该服务器发送电子邮件并以某种方式接收它们吗?
  • 尝试使您的程序成为独立的命令行程序并尝试执行它

You seem to have a missing quote in message.setSubject("Email Details Sending);. Are you sure that your servlet actually compiles?

于 2010-01-18T19:21:21.190 回答