【问题标题】:configure localhost to send email using servlet配置 localhost 以使用 servlet 发送电子邮件
【发布时间】:2014-04-15 10:45:00
【问题描述】:

我编写了一个简单的程序来发送电子邮件。 servlet 程序和 JSP 代码如下。我在 jsp 页面中收到以下错误消息。我正在使用 glassfish 服务器。我已将以下 jar 文件添加到我的类路径中。

  1. lib/mail.jar
  2. lib/dsn.jar
  3. lib/imap.jar
  4. lib/mailapi.jar
  5. lib/pop3.jar
  6. lib/smtp.jar
  7. lib/activation.jar

我在 windows 7 平台上工作并使用 netbeans IDE。

发件人电子邮件 id=someone@mohp.gov.np 收件人电子邮件 id=someone@gmail.com

错误信息

Couldn't connect to host, port: localhost, 25; timeout -1 

doPOST() 方法中的 SERVLET 代码

String err="";

        //reciever email ID
//reciever email id=someone@gmail.com
        String to=request.getParameter("reciever");

        //sender emai ID
//sender email id=someone@mohp.gov.np

        String from=request.getParameter("sender");

        //Assuming sending email from localhost
        String host="localhost";

        //Subject of the email;
        String sub=request.getParameter("subject");

        //message of the email
        String msg=request.getParameter("message");

        //get system properties
        Properties properties=System.getProperties();

        //Setup mail server
        properties.setProperty("mail.smtp.host", host);

        //get the default Session object
        Session session=Session.getDefaultInstance(properties);

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

            //set FROM: header field of the header.
            message.setFrom(new InternetAddress(from));

            //set TO: header field of the header.
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

            //set subject:
            message.setSubject(sub);

            //set message:
            message.setText(msg);

            //Send message

            Transport.send(message);


        }catch (MessagingException mex) {
            mex.printStackTrace();
            err=mex.getMessage();
        }

        request.setAttribute("err", err);
        RequestDispatcher rd=request.getRequestDispatcher("/newjsp.jsp");
        rd.forward(request, response);

JSP 代码(index.jsp)

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="css/email.css" type="text/css">
        <title>E-Mail</title>
    </head>
    <body>
        <form action="EMail" method="post" name="myform">
            <table border="1" width="100%" height="600px" cell-padding="0">
                <tr>
                    <td><table>
                    <tr height="30px">
                        <td width="5%" align="left">From :</td>
                        <td width="80%"><input type="text" name="sender" value="" size="80" /></td>
                    </tr>

                    <tr height="30px">
                        <td width="5%">To :</td>
                        <td width="80%"><input type="text" name="reciever" value="" size="80" /></td>
                    </tr>

                    <tr height="30px">
                        <td width="5%">Subject :</td>
                        <td width="80%"><input type="text" name="subject" size="100" /></td>
                    </tr>

                    <tr height="30px">
                        <td width="5%">Message :<br></td>
                    </tr>
                    <tr>
                    <table>
                        <tr height="30px">
                            <td width="5%"><textarea name="message" rows="20" cols="85"></textarea>"</td>
                        </tr>
                    </table>
                    </tr>
                    <tr>
                        <td><input type="Submit" value="Send"</td>
                    </tr>
                </table></td>
                </tr>
            </table>
        </form>
    </body>
</html>

JSP 代码 (newjsp.jsp)

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        ${err}
    </body>
</html>

【问题讨论】:

  • 您正在尝试在 localhost 上使用 smtp 服务器。本地真的有 SMTP 服务器吗?

标签: java jsp email servlets ejb


【解决方案1】:

您必须将您的电子邮件身份验证凭据设置为会话对象。使用以下代码设置身份验证凭据

 Session mailSession = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("yourmail@gmail.com", "yourpassword");
                    }
                });
        mailSession.setDebug(true);
        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject(subject);
        message.setContent(content, "text/html");

        message.addRecipient(Message.RecipientType.TO,
                new InternetAddress(emailid));

        transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, "yourmail@gmail.com", "yourpassword");

        transport.sendMessage(message,
                message.getRecipients(Message.RecipientType.TO));

        transport.close();

它应该可以工作。

【讨论】:

    【解决方案2】:

    我觉得你没有设置发送电子邮件和邮件的所有强制属性。smpts.host 是端口号。请将 localhost 更改为 465,请检查以下属性是否在你的代码中

    属性 props = new Properties();

    props.put("mail.transport.protocol", "smtps");
    props.put("mail.smtps.host", 465);
    props.put("mail.smtps.auth", "true");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.setProperty("mail.smtp.quitwait", "false");
    

    【讨论】:

    • 谢谢@Selva Kumar 先生,我添加了上面的代码及其工作,但它要求输入密码。这是我现在收到的消息。 '连接失败,没有指定密码?'我还添加了以下代码来提供用户名和密码,但它仍然无法正常工作。 'properties.setProperty("mail.user", "me@domain");' ' properties.setProperty("mail.password", "myPassword");'
    • 您必须在两行中提供您的电子邮件 ID 和密码。即返回 new PasswordAuthentication("yourmail@gmail.com", "yourpassword");和 transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, "yourmail@gmail.com", "yourpassword");请在这两行中提供您的用户名和密码,不要在属性对象中设置密码和电子邮件
    猜你喜欢
    • 2010-10-10
    • 2010-12-19
    • 2016-06-08
    • 2012-05-07
    • 1970-01-01
    • 2017-01-25
    • 2014-05-20
    • 2016-09-20
    • 2016-11-02
    相关资源
    最近更新 更多