2012-04-16 17 views
1

私はこのテーマに新しいjavamailを使用してメールを送信しようとしています。これまでのところ、コンパイルエラーやエラーは発生しませんが、電子メールは送信されません。ローカルSMTPサーバー(MailUtilLocal.java)と電子メールを送信するサーブレット(EmailServlet)を使用して電子メールを送信するヘルパークラスを使用しています。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); 
} 
} 

私はこれらの2行を削除しましたUserIOのメソッドaddRecordとは何であるべきかわかりません

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

(http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mail.html)、定型コードを書くのではなく、第二に、あなたはすべての例外をキャッチしていない、気づかれないことがあるかもしれません。 – CoolBeans

+0

なぜこの行のコード 'props.put(" mail.smtp.host "、" localhost ");'に** localhost **を指定しましたか?ここに特定のメールサーバーを指定する必要があります。 – Lion

+0

@Lion OPはローカルメールサーバーだと言っていました。 –

答えて

0

どのようなsomet SMTPサーバーがリモートサーバーと通信できないように、ISPがポート25をブロックすることがローカルsmtpサーバーを使用している場合に発生します。あなたが、その場合には何ができるか

は、SMTPサーバーから電子メールを送信するために別のポートを使用するか、または一般的に587と

465を持って、25以外のポートからの接続を受け入れ、サードパーティのサービスを使用しています[よくあるメール](http://commons.apache.org/email/)または[spring email]のようなヘルパーライブラリを使用する必要があります。Sendgridmailjetおよびpostmarkappは、最もよく知られているプロバイダの一部です。

関連する問題