2017-07-27 79 views
1

我想在給定的jsp頁面執行時發送郵件。我已經包含了javamail jar文件和java激活框架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

檢查了這一點。這就是你需要的一切。 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

我假設你沒有你的機器上運行的郵件服務器,並嘗試使用谷歌發送電子郵件......

然後主機=本地主機是錯誤的。

要做到這一點,你需要這樣的東西:

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); 
     } 
    }); 

然後通過發送電子郵件:

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);