2011-10-13 1150 views
1

Possible Duplicate:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;javax.mail.MessagingException的:无法连接到SMTP主机

我试图通过JSP页面发送一封电子邮件,但它显示了以下错误:

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

这是我的代码m用于发送电子邮件:

<% 
    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.setSentDate(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(); 
    } 
%> 

有人能告诉我是什么原因导致了这个错误?

+0

[报价IETF - RFC 2821(HTTP:// datatracker。 ietf.org/doc/rfc2821/):'421:服务不可用,关闭传输通道 – Jasper

回答

0

您需要指向Gmail邮件服务器,而不是Web服务器。

因此改变

String host = "www.gmail.com"; 

String host = "smtp.gmail.com"; 

编辑:您还需要:的String port = "587";代替...port = "25"

相关问题