簡體   English   中英

使用Java Servlet發送電子郵件時顯示錯誤?

[英]Showing Error while sending email using java servlet?

使用servlet發送郵件時出現錯誤(在eclispe中)。我在類路徑中還包含了mail.jar和activation.jar。

我的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 = "shiladitya1093@gmail.com";

        //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)是否在本地計算機上配置了SMTP服務器。 2)嘗試使用要將郵件發送到的計算機的計算機名稱或IP地址,而不是localhost。

還要提供一些有關您的郵件預期目標的詳細信息(即,該郵件打算在何處傳遞)。 基於此,您可能會得到更好的響應。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM