不使用java mail,写一段处理发送邮件的servlet
时间: 2024-01-21 07:15:59 浏览: 82
以下是一段简单的处理发送邮件的 Servlet 代码,使用了 JavaMail API 和 Servlet API:
```java
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SendMailServlet extends HttpServlet {
private String host;
private String port;
private String username;
private String password;
@Override
public void init() throws ServletException {
// 初始化 SMTP 服务器和登录用户名密码
host = getServletContext().getInitParameter("mail.smtp.host");
port = getServletContext().getInitParameter("mail.smtp.port");
username = getServletContext().getInitParameter("mail.username");
password = getServletContext().getInitParameter("mail.password");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取表单数据
String recipient = request.getParameter("recipient");
String subject = request.getParameter("subject");
String content = request.getParameter("content");
// 设置邮件属性
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
// 创建会话
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(username, password);
}
});
try {
// 创建邮件
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
message.setSubject(subject);
message.setText(content);
// 发送邮件
Transport.send(message);
// 返回成功信息
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<h3>邮件发送成功</h3>");
out.println("</body></html>");
} catch (AddressException e) {
// 处理地址异常
e.printStackTrace();
} catch (MessagingException e) {
// 处理邮件发送异常
e.printStackTrace();
}
}
}
```
在 web.xml 中配置 Servlet:
```xml
<servlet>
<servlet-name>SendMailServlet</servlet-name>
<servlet-class>SendMailServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SendMailServlet</servlet-name>
<url-pattern>/sendmail</url-pattern>
</servlet-mapping>
```
然后在 HTML 表单中调用 Servlet:
```html
<form method="post" action="sendmail">
<label>收件人:</label><input type="text" name="recipient"><br>
<label>主题:</label><input type="text" name="subject"><br>
<label>内容:</label><textarea name="content"></textarea><br>
<input type="submit" value="发送">
</form>
```
请注意,此代码仅为演示目的。在实际生产环境中,您需要考虑更多的安全问题,并使用适当的验证和错误处理技术。
阅读全文