2017-07-27 54 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);