目录

  1. 发送邮件的工作原理
  2. 实现发送邮件的步骤
  1. 示例代码
  2. 参考资料

发送邮件的工作原理

在 JSP 中,发送邮件通常通过 JavaMail API 实现。JavaMail 提供了一套与邮件服务器交互的工具,JSP 页面通过脚本调用这些 API,将邮件内容发送到指定的 SMTP 服务器(如 Gmail、QQ 邮箱等),最终传递给收件人。


实现发送邮件的步骤

2.1 添加必要的依赖

  • Maven 依赖(JavaMail API):
  <dependency>
      <groupId>javax.mail</groupId>
      <artifactId>javax.mail-api</artifactId>
      <version>1.6.2</version>
  </dependency>
  <dependency>
      <groupId>com.sun.mail</groupId>
      <artifactId>javax.mail</artifactId>
      <version>1.6.2</version>
  </dependency>

2.2 配置邮件服务器参数

  • 常用参数
  • 邮件服务器主机(mail.smtp.host):如 smtp.gmail.com
  • 端口(mail.smtp.port):如 587(TLS)。
  • 用户名和密码:发送者的邮箱账号和授权码。
  • 示例(Gmail 配置):
  Properties props = new Properties();
  props.put("mail.smtp.host", "smtp.gmail.com");
  props.put("mail.smtp.port", "587");
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.starttls.enable", "true");

2.3 使用 JavaMail API 发送邮件

  • 步骤
  1. 创建 Session 对象。
  2. 构造 MimeMessage 对象。
  3. 设置发件人、收件人、主题和内容。
  4. 通过 Transport 发送邮件。
  • 示例
  <%@ page import="javax.mail.*, javax.mail.internet.*, java.util.Properties" %>
  <%
      String to = "recipient@example.com";
      String from = "your-email@gmail.com";
      String password = "your-app-password"; // Gmail 需使用应用专用密码

      Properties props = new Properties();
      props.put("mail.smtp.host", "smtp.gmail.com");
      props.put("mail.smtp.port", "587");
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");

      Session mailSession = Session.getInstance(props, new Authenticator() {
          protected PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication(from, password);
          }
      });

      try {
          MimeMessage message = new MimeMessage(mailSession);
          message.setFrom(new InternetAddress(from));
          message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
          message.setSubject("JSP 邮件测试");
          message.setText("这是一封来自 JSP 的测试邮件!");
          Transport.send(message);
          out.println("邮件发送成功!");
      } catch (MessagingException e) {
          out.println("邮件发送失败: " + e.getMessage());
      }
  %>

示例代码

  • sendMail.jsp(完整示例):
  <%@ page contentType="text/html;charset=UTF-8" %>
  <%@ page import="javax.mail.*, javax.mail.internet.*, java.util.Properties" %>
  <%
      String to = request.getParameter("to");
      String from = "your-email@gmail.com"; // 替换为你的邮箱
      String password = "your-app-password"; // 替换为你的应用密码

      Properties props = new Properties();
      props.put("mail.smtp.host", "smtp.gmail.com");
      props.put("mail.smtp.port", "587");
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");

      Session mailSession = Session.getInstance(props, new Authenticator() {
          protected PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication(from, password);
          }
      });

      try {
          MimeMessage message = new MimeMessage(mailSession);
          message.setFrom(new InternetAddress(from));
          message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
          message.setSubject("来自 JSP 的邮件");
          message.setText("这是一封测试邮件,发送时间: " + new java.util.Date());
          Transport.send(message);
          out.println("邮件已成功发送至 " + to);
      } catch (MessagingException e) {
          out.println("邮件发送失败: " + e.getMessage());
      }
  %>
  • 调用方式:访问 sendMail.jsp?to=recipient@example.com

参考资料

  1. Oracle 官方文档
  1. JavaTpoint JSP 教程
  1. Google 应用密码

注意事项

  • Gmail 用户:需启用“两步验证”并生成“应用专用密码”。
  • 安全性:避免在 JSP 中硬编码邮箱密码,建议使用配置文件或环境变量。

如果需要更复杂的邮件功能(如附件或 HTML 内容),请告诉我!