2011-10-13 240 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"

相關問題