开发者

javax.mail.MessagingException: Could not connect to SMTP host [duplicate]

This question already has answers here: Closed 10 years ago.

Possible Duplicate:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;

I tried to send an email through a jsp page but it shows the following error:

javax.mail.MessagingException: Could not connect to SMTP host: www.gmail.com, port: 25, response: 421

This is the code I'm using to send the email:

<%
    try
    {
        String host = "www.gmail.com";
        String to = request.getParameter("to");
        String from = request.getParameter("from");
        String subject = request.getParameter("subject");
        String messageText = request.getParameter("body");
        boolean sessionDebug = false;

        Properties props = System.getProperties();
        props.put("mail.host", host);
        props.put("mail.transport.protocol", "smtp");
        Session mailSession = Session.getDefaultInstance(props, null);

        mailSession.setDebug(sessionDebug);

        Message msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress(from));
        InternetAddress[] address = {new InternetAddress(to)};
        msg.setRecipients(Message.RecipientType.TO, address);
        msg.setSubject(subject);
        msg.setSentD开发者_运维知识库ate(new Date());
        msg.setText(messageText);

        Transport.send(msg);
        out.println("Mail was sent to " + to);
        out.println(" from " + from);
        out.println(" using host " + host + ".");
    }
    catch (MessagingException mex) 
    {
        System.out.println("Error: unable to send message....");
        mex.printStackTrace();
    }
%>

Could someone tell me what is causing this error?


You need to point to the gmail mail server, not the web server.

So change

String host = "www.gmail.com";

to

String host = "smtp.gmail.com";

EDIT: You'll also need: String port = "587"; instead of ...port = "25"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜