javax.mail.SendFailedException:发送失败;嵌套异常是:class javax.mail.MessagingException:Unknown SMTP host:relay.jangosmtp.net;

时间:2014-04-23 20:48:58

标签: jsp email session tomcat smtp

我想将我的Jsp连接连接到电子邮件。我正在使用Apache Tomcat服务器。我引用了这个链接来完成它: - http://www.javabeat.net/send-mail-jsp/但我得到的错误是: -

javax.mail.SendFailedException: Sending failed;
  nested exception is:
    class javax.mail.MessagingException: Unknown SMTP host: relay.jangosmtp.net;
  nested exception is:
    java.net.UnknownHostException: relay.jangosmtp.net

我真的很担心问题出在哪里。请帮我修复此错误。提前谢谢!

这是我的index.html: -

<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
    <%@ page import="javax.mail.internet.*,javax.activation.*"%>
    <%@ page import="javax.servlet.http.*,javax.servlet.*"%>
    <%
        String result;
        //Recipient's email ID needs to be mentioned.
        String to = "abc@gmail.com";

        // Sender's email ID needs to be mentioned
        String from = "abc@gmail.com";
        final String username = "username";
        final String password = "251497PF";

        // Assuming you are sending email through relay.jangosmtp.net
        String host = "relay.jangosmtp.net";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", "25");

        //Get the Session object.
        Session mailSession = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username,
                                password);
                    }
                });

        try {
            // Create a default MimeMessage object.
            Message message = new MimeMessage(mailSession);

            // Set From: header field of the header.
            message.setFrom(new InternetAddress(from));

            // Set To: header field of the header.
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(to));

            // Set Subject: header field
            message.setSubject("Testing Subject");

            // Now set the actual message
            message.setText("Hello, this is sample for to check send "
                    + "email using JavaMailAPI in JSP ");

            // Send message
            Transport.send(message);

            System.out.println("Sent message successfully....");
            result = "Sent message successfully....";

        } catch (MessagingException e) {
            e.printStackTrace();
            result = "Error: unable to send message....";

        }
    %>
    <html>
    <head>
    <title>Send Email using JSP</title>
    </head>
    <body>
        <center>
            <h1>Send Email using JSP</h1>
        </center>
        <p align="center">
            <%
                out.println("Result: " + result + "\n");
            %>
        </p>
    </body>

还有一个问题: - 在JangoSMTP中建立免费帐户后,它说要为Web应用程序提供IP地址。当我在本地计算机上使用Tomcat服务器时如何做到这一点。这可能是我错误的原因吗?

0 个答案:

没有答案