2014-07-26 50 views
0

使用servlet發送郵件時出現錯誤(在eclispe中)。我還在我的類路徑中包含mail.jar和activation.jar。使用java servlet發送電子郵件時顯示錯誤?

我的servlet代碼看起來像

import java.util.Properties; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 
public class Controller extends HttpServlet 
{ 
    private static final long serialVersionUID = 1L; 
    public Controller() { 
     super(); 
    } 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ 
     doProcess(request,response); 
    } 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ 
     doProcess(request,response); 
    } 
    protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    { 
     final String user = request.getParameter("email"); 
     final String pwd = request.getParameter("pwd"); 
     String sub = request.getParameter("subject"); 
     String body = request.getParameter("msg"); 
     String to = "[email protected]"; 

     //Get the session object 
     Properties props = new Properties(); 
     props.put("mail.smtp.host","localhost"); 
     props.put("mail.smtp.auth","true"); 
     Session session = Session.getDefaultInstance(props, 
       new javax.mail.Authenticator() { 
        protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(user,pwd); 
        } 
       }); 

     //Compose message 
     try{ 
      MimeMessage message = new MimeMessage(session); 
      message.setFrom(new InternetAddress(user)); 
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
      message.setSubject(sub); 
      message.setText(body); 

     //Send message 
      Transport.send(message); 
     }catch(MessagingException e){ 
      throw new RuntimeException(e); 
     } 

     request.setAttribute("sendMsg","Message successfully send."); 
     RequestDispatcher rd = request.getRequestDispatcher("contactus.jsp"); 
     rd.forward(request, response); 
    } 
} 

和錯誤是這樣的:

HTTP狀態500 - javax.mail.AuthenticationFailedException:535沒有定義SMTP服務器。在您的帳戶中使用真實的服務器地址而不是127.0.0.1。

+1

在您的帳戶中使用真實服務器地址而不是127.0.0.1(仔細閱讀您的錯誤? –

+0

郵件服務器需要正確配置。您有smtp主機作爲本地主機,它應該是smtp服務器ip。沒有smtp服務器,使用google提供的默認smtp配置,可以測試郵件api。 – Amz

+0

我該如何在eclispe中設置smtp服務器? – Aditya

回答

0

您可以檢查以下內容。 1)您的SMTP服務器是否在本地機器上配置。 2)嘗試使用計算機的計算機名稱或IP地址來發送郵件,而不是本地主機。

此外,請提供一些關於您的郵件預期目標的詳細信息(例如,此郵件意圖在何處發送)。基於此,您可能會得到更好的迴應。

+0

我想從gmail發送消息到gmail,我也嘗試「smtp.gmail .com「而不是本地主機,但它顯示相同的問題 – Aditya

+0

嘗試通過設置屬性」mail.smtp.port「來明確定義端口587和465到gmail smtp服務器。你也可以檢查你是否能夠到達服務器可以通過在cmd提示符下使用telnet或者使用像putty這樣的一些telnet客戶端(如果在windows上)來解決。這樣你可以確保您試圖訪問的服務器可以從您的計算機上訪問,然後再開始 – ganaraj

+0

看看下面的帖子http://stackoverflow.com/questions/15372803/sending-gmail-error可能有助於確定問題的位置由於防病毒屏蔽了smtp端口,無論如何,simpel telnet測試都會顯示您是否可以訪問。 – ganaraj

相關問題