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>
檢查了這一點。這就是你需要的一切。 http://www.codejava.net/java-ee/jsp/sending-e-mail-with-jsp-servlet-and-javamail –
修復這些[常見錯誤](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)。 –