博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSP发送电子邮件
阅读量:4617 次
发布时间:2019-06-09

本文共 6527 字,大约阅读时间需要 21 分钟。

以下内容引用自:

发送一个简单的电子邮件

给出一个简单的例子,从机器上发送一个简单的邮件。假设本地主机连接到互联网,能够足以发送一封电子邮件。

<%@ 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....";   }%>Send Email using JSP

Send Email using JSP

<% out.println("Result: " + result + "\n");%>

现在将上面的代码放在SendEmail.jsp文件,并且使用URL:http://localhost:8080/SendEmail.jsp来调用此JSP,将电子邮件发送到给定的邮箱ID为abcd@gmail.com中,显示的响应结果如下所示。

如果想给多个收件人发送邮件,下面的方法可用于发送邮件给指定的多个邮箱IDs:

void addRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException

下面是对参数的描述:

  • type:可设置为TO,CC或者BCC。其中,CC表示Carbon Copy,BCC表示Black Carbon Copy。例如,Message.RecipientType.TO。

  • addresses:它是邮箱ID的数组。能使用InternetAddress()方法,同时指定邮箱IDs。

发送HTML邮件

这个例子与之前给出的例子非常相似,除了在这个例子中使用setContent()方法设置第二个参数为“text/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("

This is actual message

", "text/html" ); // Send message Transport.send(message); result = "Sent message successfully...."; }catch (MessagingException mex) { mex.printStackTrace(); result = "Error: unable to send message...."; }%>Send HTML Email using JSP

Send Email using JSP

<% out.println("Result: " + result + "\n");%>

现在可以使用上述JSP发送网页信息给指定的邮箱ID。

在电子邮件中发送附件:

给定一个例子,从机器上发送一个带附件的邮箱:

<%@ 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....";   }%>Send Attachement Email using JSP

Send Attachement Email using JSP

<% out.println("Result: " + result + "\n");%>

现在可以运行上述JSP发送一个文件作为信息的附件给指定邮箱ID。

用户身份验证部分

为了身份验证的目的,如果需要提供用户ID和密码给邮箱服务器,可以设置这些属性如下:

props.setProperty("mail.user", "myuser");props.setProperty("mail.password", "mypwd");

邮件发送机制的设置和上述的解释一致。

用表单发送邮件

可以使用网页表单接受邮件参数,然后可以使用Request对象获得如下的所有信息:

String to = request.getParameter("to");String from = request.getParameter("from");String subject = request.getParameter("subject");String messageText = request.getParameter("body");

 

测试工程:

转载于:https://www.cnblogs.com/EasonJim/p/6954664.html

你可能感兴趣的文章
每天CookBook之Python-036
查看>>
Django 之 cookie & session
查看>>
反转字符串
查看>>
CRM客户关系管理系统(十二)
查看>>
洛谷P2776 [SDOI2007]小组队列 链表 + 模拟
查看>>
ORA-39006错误原因及解决办法
查看>>
linux常用目录与作用
查看>>
PHP 后台定时循环刷新某个页面 屏蔽apache意外停止
查看>>
codeforces 622B B. The Time
查看>>
个人日报0628
查看>>
BeanDefinition的Resource定位——2
查看>>
学习记事
查看>>
java 子类重写父类的方法应注意的问题
查看>>
[LevelDB] LevelDB理论基础
查看>>
如果部署Excel 加载项?
查看>>
【codecombat】 试玩全攻略 第一关kithguard地牢
查看>>
【DP】 POJ 1191 棋盘分割 记忆化搜索
查看>>
自动化测试 Appium之Python运行环境搭建 Part2
查看>>
说说DBA职责和目标
查看>>
VsCode插件与Node.js交互通信
查看>>