Question

I have written a simple program to send email. The servlet program and JSP code is below. I get the following error message in jsp page. I am using glassfish server. I have added following jar file to my class path.

  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

I am working in windows 7 platform and using netbeans IDE.

sender email id=someone@mohp.gov.np reciever email id=someone@gmail.com

Error Message

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

SERVLET CODE in doPOST() method

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 CODE(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 CODE (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>
Était-ce utile?

La solution

you must set your email authentication credentials to session object.use the below code to set authentication credentials

 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();

It should work.

Autres conseils

I thing you are not setting all the mandatory properties to send email and mail.smpts.host is the port number.please change localhost to 465 and please check the below properties are in your code

Properties 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");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top