扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
用Servlet实现邮件发送非常简单,我们只是需要构建一个jsp文件,提供给用户提交邮件信息的表单。
·用VB编写收发电子邮件程序
·办公技巧 巧用Excel群发电子邮件
·电子邮件使用技巧集锦
·使用Outlook 2003为电子邮件加锁
·PowerBuilder 7.0中实现电子邮件的收
·Java设计防病毒电子邮件程序之代码
·在PPt 2002中创建用电子邮件发送的多
·IMail设置教程:第三节 建立可用的电
·多人共用Outlook进行电子邮件管理
·用C++ Builder编写发送电子邮件软件
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<center>
<p>Welcome</p>
<form name="form1" method="post" action="maildo">
<p>收件人:
<input type="text" name="send">
</p>
<p>主 题:
<input type="text" name="subject">
</p>
<p>内 容:
<textarea name="content"></textarea>
</p>
<p>
<input type="submit" name="Submit" value="send">
</p>
</form>
<p> </p>
</center>
</body>
</html>
浏览器收集到用户输入的信息后传递给Servlet来处理,Servlet通过request.getParameter()方法得到相关的数据后,通过JavaMail提供的API发送电子邮件。
package com.j2medev.servletmail;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.*;
import java.net.*;
public class MailServlet extends HttpServlet
{
private static String host;
private static String from;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
host = config.getInitParameter("host");
from = config.getInitParameter("from");
System.out.println(host + from);
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
String send = request.getParameter("send");
String subject = request.getParameter("subject");
String content = request.getParameter("content");
try
{
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
// Get session
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from));
// Set the to address
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
send));
// Set the subject
message.setSubject(subject);
// Set the content
message.setText(content);
// Send message
Transport.send(message);
} catch (Exception e)
{
e.printStackTrace();
}
}
}
接下来我们编辑web.xml文件,注意在init-param中写上host和from的参数,以便Servlet初始化的时候可以读取。
<?xml version="1.0" ?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>ServletMail</servlet-name>
<servlet-class>com.j2medev.servletmail.MailServlet</servlet-class>
<init-param>
<param-name>host</param-name>
<param-value>smtp.263.net</param-value>
</init-param>
<init-param>
<param-name>from</param-name>
<param-value>eric.zhan@263.net</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ServletMail</servlet-name>
<url-pattern>/maildo</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
</web-app>
由于我们需要用到JavaMail和JAF的支持,因此我们需要把相关的包放到YOURAPP/WEB_INF/lib下。在浏览器输入http://localhost:8080/yourapp/index.jsp 发送电子邮件。
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。