2017-07-27 2 views
1

特定のjspページが実行されたときにメールを送信します。私はjavamail jarファイルとjava activation framework jarファイルの両方を含んでいます。しかし、このコードは、パスワードが指定されていないというエラーを出していますが、私は以下のコードに示すように正しいパスワードを提供しています。どんな助けも高く評価されるでしょう!全く新しいJSPです。JavaメールAPIを使用してjsp webappからメールを送信できません

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
    <!DOCTYPE html> 
    <%@ page import = "java.io.*,java.util.*,javax.mail.*"%> 
    <%@ page import = "javax.mail.internet.*,javax.activation.*"%> 
    <%@ page import = "javax.servlet.http.*,javax.servlet.*" %> 

    <% 
    String result; 

    // Recipient's email ID needs to be mentioned. 
    String to = "[email protected]"; 

    // Sender's email ID needs to be mentioned 
    String from = "[email protected]"; 

    // Assuming you are sending email from localhost 
    String host = "localhost"; 

    // Get system properties object 
    Properties properties = System.getProperties(); 

    // Setup mail server 
    properties.setProperty("mail.user", "[email protected]"); 
    properties.setProperty("mail.password", "xzu"); 
    properties.setProperty("mail.smtp.host", host); 

    // Get the default Session object. 
    Session mailSession = Session.getDefaultInstance(properties); 

    try { 
    // Create a default MimeMessage object. 
    MimeMessage message = new MimeMessage(mailSession); 

    // Set From: header field of the header. 
    message.setFrom(new InternetAddress(from)); 

    // Set To: header field of the header. 
    message.addRecipient(Message.RecipientType.TO, 
          new InternetAddress(to)); 
    // Set Subject: header field 
    message.setSubject("This is the Subject Line!"); 

    // Now set the actual message 
    message.setText("This is actual message"); 

    // Send message 
    Transport.send(message); 
    result = "Sent message successfully...."; 
    } catch (MessagingException mex) { 
    mex.printStackTrace(); 
    result = mex.getMessage(); 
    } 
    %> 

    <html> 
    <head> 
    <title>Send Email using JSP</title> 
    </head> 

    <body> 
    <center> 
    <h1>Send Email using JSP</h1> 
    </center> 

    <p align = "center"> 
    <% 
     out.println("Result: " + result + "\n"); 
    %> 
    </p> 
    </body> 
    </html> 
+1

final String username = "[email protected]"; final String password = "password"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); 

その後で、あなたの電子メールを送信し、その後

Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); 

セッションを作成して:あなたはのようなものを必要とするこれを行うには

。それだけで十分です。 http://www.codejava.net/java-ee/jsp/sending-e-mail-with-jsp-servlet-and-javamail –

+0

これらの[よくある間違い](https://javaee.github.io/javamail)を修正してください。/FAQ#commonmistakes)、JavaMail FAQの[Gmailの例](https://javaee.github.io/javamail/FAQ#gmail)に従ってください。それでも動作しない場合は、[JavaMailデバッグ出力](https://javaee.github.io/javamail/FAQ#debug)を投稿してください。 –

答えて

0

私はその後、ホスト= localhostのは間違っている

...あなたはあなたのマシン上で実行されているメールサーバーを持っていけないとは、Googleを使用して電子メールを送信しようとすると仮定します。これをチェックしてください

Message message = new MimeMessage(session); 
message.setFrom(new InternetAddress(from)); 
message.setRecipients(Message.RecipientType.TO, 
      InternetAddress.parse(to)); 
message.setSubject(subject); 
message.setText(messageText); 
Transport.send(message); 
関連する問題