JSP發送電子郵件Email
要使用JSP發送郵件是很簡單的,需要在您的機器上安裝JavaMail API和Java激活框架(JAF)。
-
您可以從Java標準網站上下載的JavaMail(版本1.2)的最新版本。
-
您可以從Java的標準的網站下載JavaBeans激活框架JAF(版本1.0.2)的最新版本。
下載並解壓縮這些文件,在新創建的頂層目錄,你會發現一些jar文件同時為應用。你需要添加了mail.jar和activation.jar文件的文件在你的CLASSPATH中。
發送一個簡單的電子郵件:
下麵是一個例子,從你的機器發送一個簡單的電子郵件。這裡假設你的本地主機連接到互聯網,並有足夠的能力來發送電子郵件。同時要確保從Java電子郵件API包和CLASSPATH中可用JAF包ARA的所有jar文件。
<%@ 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 = "abcd@gmail.com"; // Sender's email ID needs to be mentioned String from = "mcmohd@gmail.com"; // Assuming you are sending email from localhost String host = "localhost"; // Get system properties object Properties properties = System.getProperties(); // Setup mail server 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 = "Error: unable to send message...."; } %> <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 + " "); %> </p> </body> </html>
現在,讓我們把上麵的代碼寫到SendEmail.jsp文件,並使用URL http://localhost:8080/SendEmail.jsp調用這個JSP這將發送一封電子郵件到指定的電子郵件ID abcd@gmail.comand會顯示以下回應:
Send Email using JSPResult: Sent message successfully.... |
如果您想發送電子郵件給多個收件人,然後下麵的方法將被用來指定多個電子郵件ID:
void addRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException
這裡的參數的說明:
-
type: 這將被設定為TO,CC或BCC。在這裡,CC代表抄送和密件抄送代表黑碳複製。例如Message.RecipientType.TO
-
addresses: 這是電子郵件的ID的數組。需要使用 InternetAddress()方法,同時指定電子郵件ID。
發送HTML Email:
下麵是一個例子,從機器發送的HTML電子郵件。這裡假設你的本地主機連接到互聯網,並有足夠的能力來發送電子郵件。同時要確保從Java電子郵件API包和CLASSPATH中可用JAF包ARA的所有jar文件。
這個例子非常相似,前一個,除了在這裡我們使用的是使用setContent()方法來設置內容的第二個參數為“text / html類型”指定的HTML內容被包含在消息中。
通過這個例子,可以發送你喜歡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 = "abcd@gmail.com"; // Sender's email ID needs to be mentioned String from = "mcmohd@gmail.com"; // Assuming you are sending email from localhost String host = "localhost"; // Get system properties object Properties properties = System.getProperties(); // Setup mail server 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!"); // Send the actual HTML message, as big as you like message.setContent("<h1>This is actual message</h1>", "text/html" ); // Send message Transport.send(message); result = "Sent message successfully...."; }catch (MessagingException mex) { mex.printStackTrace(); result = "Error: unable to send message...."; } %> <html> <head> <title>Send HTML Email using JSP</title> </head> <body> <center> <h1>Send Email using JSP</h1> </center> <p align="center"> <% out.println("Result: " + result + " "); %> </p> </body> </html>
現在,嘗試使用上麵的JSP在給定的電子郵件ID發送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 = "abcd@gmail.com"; // Sender's email ID needs to be mentioned String from = "mcmohd@gmail.com"; // Assuming you are sending email from localhost String host = "localhost"; // Get system properties object Properties properties = System.getProperties(); // Setup mail server 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!"); // Create the message part BodyPart messageBodyPart = new MimeBodyPart(); // Fill the message messageBodyPart.setText("This is message body"); // Create a multipar message Multipart multipart = new MimeMultipart(); // Set text message part multipart.addBodyPart(messageBodyPart); // Part two is attachment messageBodyPart = new MimeBodyPart(); String filename = "file.txt"; DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); // Send the complete message parts message.setContent(multipart ); // Send message Transport.send(message); String title = "Send Email"; result = "Sent message successfully...."; }catch (MessagingException mex) { mex.printStackTrace(); result = "Error: unable to send message...."; } %> <html> <head> <title>Send Attachement Email using JSP</title> </head> <body> <center> <h1>Send Attachement Email using JSP</h1> </center> <p align="center"> <% out.println("Result: " + result + " "); %> </p> </body> </html>
現在試著上麵的JSP運行到一個文件作為附件發送以及在給定的電子郵件ID的消息。
用戶認證部分:
如果需要提供用戶ID和密碼的電子郵件服務器進行身份驗證的目的,那麼你可以按如下方法設置這些屬性:
props.setProperty("mail.user", "myuser"); props.setProperty("mail.password", "mypwd");
郵件發送機製的其餘部分將繼續如上麵所述。
使用表單發送電子郵件:
您可以使用HTML表單接受電子郵件參數,然後你可以使用request對象如下,讓所有的信息:
String to = request.getParameter("to"); String from = request.getParameter("from"); String subject = request.getParameter("subject"); String messageText = request.getParameter("body");
一旦你把所有的信息設置完成,就可以使用上麵提到的程序來發送電子郵件。