在idea的web项目,建立邮件发送表示页面,包括收件人、主题、具体内容、以及上传的附件的jsp
时间: 2023-07-10 21:11:19 浏览: 199
jsp实现的邮件发送
好的,建立邮件发送页面的步骤如下:
1. 在 IntelliJ IDEA 中新建一个 Web 项目。
2. 在 src 目录下创建一个名为 jsp 的文件夹,用于存放 JSP 页面。
3. 在 jsp 文件夹下创建一个名为 mail.jsp 的文件,用于编写邮件发送页面。
4. 在 mail.jsp 中添加以下代码,用于显示邮件发送表单:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>邮件发送</title>
</head>
<body>
<h1>邮件发送</h1>
<form action="sendMail" method="post" enctype="multipart/form-data">
<div>
<label for="to">收件人:</label>
<input type="email" name="to" id="to" required>
</div>
<div>
<label for="subject">主题:</label>
<input type="text" name="subject" id="subject" required>
</div>
<div>
<label for="content">内容:</label>
<textarea name="content" id="content" rows="10" required></textarea>
</div>
<div>
<label for="attachment">附件:</label>
<input type="file" name="attachment" id="attachment">
</div>
<div>
<input type="submit" value="发送">
</div>
</form>
</body>
</html>
```
5. 在 web.xml 文件中添加以下代码,用于配置邮件发送的 Servlet:
```xml
<servlet>
<servlet-name>SendMailServlet</servlet-name>
<servlet-class>com.example.SendMailServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SendMailServlet</servlet-name>
<url-pattern>/sendMail</url-pattern>
</servlet-mapping>
```
6. 在 src 目录下创建一个名为 SendMailServlet 的 Java 类,用于处理邮件发送请求。
7. 在 SendMailServlet 类中添加以下代码,用于处理邮件发送请求:
```java
import javax.mail.*;
import javax.mail.internet.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.*;
import java.util.Properties;
public class SendMailServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String to = request.getParameter("to");
String subject = request.getParameter("subject");
String content = request.getParameter("content");
Part attachment = request.getPart("attachment");
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("your-email-address@gmail.com", "your-email-password");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("your-email-address@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(subject);
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(content, "text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);
if (attachment != null) {
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
attachmentBodyPart.setFileName(attachment.getSubmittedFileName());
attachmentBodyPart.setContent(attachment.getInputStream(), attachment.getContentType());
multipart.addBodyPart(attachmentBodyPart);
}
message.setContent(multipart);
Transport.send(message);
response.sendRedirect("mail.jsp?success=true");
} catch (MessagingException e) {
response.sendRedirect("mail.jsp?success=false");
throw new RuntimeException(e);
}
}
}
```
以上代码将邮件发送至 Gmail 服务器,如果你使用其他邮件服务器,请修改 properties 对象中的配置信息。
8. 运行项目,访问 mail.jsp 页面即可看到邮件发送表单。
阅读全文