写一段发送邮件的jsp代码,tomcat版本为18.0
时间: 2024-01-21 11:15:55 浏览: 30
以下是一个简单的发送邮件的JSP代码示例:
```
<%@ page import="javax.mail.*, javax.mail.internet.*, java.util.*" %>
<%
// 设置邮件服务器信息
String host = "smtp.gmail.com";
String username = "your_email@gmail.com";
String password = "your_email_password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
// 创建会话对象
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
// 创建邮件对象
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient_email@gmail.com"));
message.setSubject("邮件主题");
message.setText("邮件内容");
// 发送邮件
Transport.send(message);
out.println("邮件发送成功");
%>
```
注意:需要在JSP页面中导入Java Mail API和Java Activation Framework库,否则会出现编译错误。此外,还需要替换示例代码中的邮件服务器信息、发件人和收件人邮箱地址以及邮件主题和内容。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)