JSP 发送电子邮件
-
-
发送一个简单的电子邮件
这是从您的计算机发送简单电子邮件的示例。假定您的本地主机已连接到Internet,并且能够发送电子邮件。确保CLASSPATH中提供了Java Email API软件包和JAF软件包中的所有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; // 需要提到收件人的电子邮件ID。 String to = "abcd@gmail.com"; // 需要提到发件人的电子邮件ID String from = "mcmohd@gmail.com"; // 假设您正在从本地主机发送电子邮件 String host = "localhost"; // 获取System.getProperties对象 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); // 获取默认会话对象。 Session mailSession = Session.getDefaultInstance(properties); try { // 创建默认的MimeMessage对象。 MimeMessage message = new MimeMessage(mailSession); // 设置 From:标题的标题字段。 message.setFrom(new InternetAddress(from)); // 设置 To: 标题的标题字段。 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 设置 Subject: 标题字段。 message.setSubject("This is the Subject Line!"); // 现在设置实际的消息 message.setText("This is actual message"); // 发送消息 Transport.send(message); result = "Sent message successfully...."; } catch (MessagingException mex) { mex.printStackTrace(); result = "错误:无法发送消息…"; } %> <html> <head> <title>使用JSP发送电子邮件</title> </head> <body> <center> <h1>使用JSP发送电子邮</h1> </center> <p align = "center"> <% out.println("结果: " + result + "\n"); %> </p> </body> </html>
现在让我们将上面的代码放入SendEmail.jsp文件中,并使用URL http://localhost:8080/SendEmail.jsp调用此JSP 。如果要将电子邮件发送给多个收件人,请使用以下方法指定多个电子邮件ID-void addRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException
这是参数的描述-- type - 将其设置为TO,CC或BCC。此处CC代表复写,而BCC代表黑抄。示例Message.RecipientType.TO
- addresses -这是电子邮件ID的数组。您需要在指定电子邮件ID时使用InternetAddress()方法
-
发送HTML电子邮件
这是从您的计算机发送HTML电子邮件的示例。假定您的本地主机已连接到Internet,并且能够发送电子邮件。确保CLASSPATH中提供了Java Email API软件包和JAF软件包中的所有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 + "\n"); %> </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 multipart 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 Attachment Email using JSP</title> </head> <body> <center> <h1>Send Attachment Email using JSP</h1> </center> <p align = "center"> <%out.println("Result: " + result + "\n");%> </p> </body> </html>
现在,让我们运行上面的JSP,将文件作为附件发送,并在给定的电子邮件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");
获得所有信息后,您可以使用上述程序发送电子邮件。