2012-04-16 179 views
1

我試圖通過使用javamail發送電子郵件,但即時通訊新的這個主題。到目前爲止,我沒有收到編譯錯誤,也沒有運行錯誤,但電子郵件從未發送。我使用助手類發送電子郵件與本地SMTP服務器(MailUtilLocal.java)和發送電子郵件(EmailServlet)的servlet。下面是代碼:javamail電子郵件不發送

` //EmailServlet.java:

import Beans.Usuario; 
import IOBD.InputOutputBD; 
import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
import javax.mail.MessagingException; 
import Servlets.MailUtilLocal; 
import java.util.*; 

public class EmailServlet extends HttpServlet { 

protected void doPost(HttpServletRequest request, 
     HttpServletResponse response) 
     throws ServletException, IOException { 
    // get parameters from the request 
    String userId = request.getParameter("userId"); 
    String password = request.getParameter("password"); 
    String nombre = request.getParameter("nombre"); 
    String tipo = request.getParameter("tipo"); 
    String emailAddress = request.getParameter("emailAddress"); 
// create the User object and write it to a file 
    Usuario user = new Usuario(userId, password, nombre, tipo, emailAddress); 
    ServletContext sc = getServletContext(); 

// store the User object in the session 
    HttpSession session = request.getSession(); 
    session.setAttribute("user", user); 


// send email to user 
    String to = emailAddress; 
    String from = "[email protected]"; 
    String subject = "Welcome to our email list"; 
    String body = "Dear " + nombre + ",\n\n" 
      + "Thanks for joining our email list. " 
      + "We'll make sure to send you announcements " 
      + "about new products and promotions.\n" 
      + "Have a great day and thanks again!\n\n" 
      + "Kelly Slivkoff\n" + "Mike Murach & Associates"; 
    boolean isBodyHTML = false; 
    try { 
     MailUtilLocal.sendMail(to, from, subject, body, isBodyHTML); 
    } catch (MessagingException e) { 
     String errorMessage = 
       "ERROR: Unable to send email. " 
       + "Check Tomcat logs for details.<br>" 
       + "NOTE: You may need to configure your " 
       + "system as described in chapter 15.<br>" 
       + "ERROR MESSAGE: " + e.getMessage(); 
     request.setAttribute("errorMessage", errorMessage); 
     this.log("Unable to send email. \n" 
       + "Here is the email you tried to send: \n" 
       + "=====================================\n" 
       + "TO: " + emailAddress + "\n" 
       + "FROM: " + from + "\n" 
       + "SUBJECT: " + subject + "\n" 
       + "\n" 
       + body + "\n\n"); 


    } 
    // forward request and response to JSP page 
    String url = "/index.jsp"; 
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url); 
    dispatcher.forward(request, response); 
} 
} 

//MailUtilLocal.java:

import java.util.Properties; 
import javax.mail.*; 
import javax.mail.internet.*; 

public class MailUtilLocal { 

public static void sendMail(String to, String from, String subject, String body, boolean bodyIsHTML) 
     throws MessagingException { 
    // 1 - get a mail session 
    Properties props = new Properties(); 
    props.put("mail.transport.protocol", "smtp"); 
    props.put("mail.smtp.host", "localhost"); 
    props.put("mail.smtp.port", 25); 
    Session session = Session.getDefaultInstance(props); 
    session.setDebug(true); 
    // 2 - create a message 
    Message message = new MimeMessage(session); 
    message.setSubject(subject); 
    if (bodyIsHTML) { 
     message.setContent(body, "text/html"); 
    } else { 
     message.setText(body); 
    } 
    // 3 - address the message 
    Address fromAddress = new InternetAddress(from); 
    Address toAddress = new InternetAddress(to); 
    message.setFrom(fromAddress); 
    message.setRecipient(Message.RecipientType.TO, toAddress); 
    // 4 - send the message 
    Transport.send(message); 
} 
} 

我刪除了這兩條線,因爲我得到一個錯誤的沒有在UserIO上addRecord方法,我不知道應該在它裏面

String path = sc.getRealPath("/WEB-INF/EmailList.txt"); 
UserIO.addRecord(user, path); 
+0

你應該使用一個輔助庫如[常見的電子郵件(http://commons.apache.org/email/)或[春天電子郵件] (http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mail.html)而不是編寫樣板代碼。其次,你沒有捕捉到所有的例外情況,可能有一個未被注意到的情況。 – CoolBeans 2012-04-16 03:16:07

+0

爲什麼你在這行代碼props.put(「mail.smtp.host」,「localhost」);'中指定了** localhost **。你需要在這裏指定一個特定的郵件服務器。 – Lion 2012-04-16 03:16:09

+0

@Lion OP提到它是本地郵件服務器。 – 2012-04-16 03:18:43

回答

0

what somet當您使用本地smtp服務器時發生imes是您的ISP將阻止端口25,以便您的SMTP服務器無法與遠程服務器通信。

,你可以在這種情況下,做的是使用不同的端口,從您的SMTP服務器發送電子郵件,或使用接受來自超過25個其他的,一般587和465

有一個端口連接的第三方服務看Sendgridmailjetpostmarkapp,因爲它們是一些最知名的供應商

相關問題