邮件发送简单例子-jsp文件
点击次数:31 次 发布日期:2008-11-26 15:51:52 作者:源代码网
|
源代码网推荐 源代码网推荐 <html> 源代码网推荐 <head> 源代码网推荐 <title>JSP JavaMail Example </title> 源代码网推荐 </head> 源代码网推荐 源代码网推荐 <body> 源代码网推荐 源代码网推荐 <%@ page import="java.util.*" %> 源代码网推荐 <%@ page import="javax.mail.*" %> 源代码网推荐 <%@ page import="javax.mail.internet.*" %> 源代码网推荐 <%@ page import="javax.activation.*" %> 源代码网推荐 源代码网推荐 <% 源代码网推荐 String host = "yourmailhost"; 源代码网推荐 String to = request.getParameter("to"); 源代码网推荐 String from = request.getParameter("from"); 源代码网推荐 String subject = request.getParameter("subject"); 源代码网推荐 String messageText = request.getParameter("body"); 源代码网推荐 boolean sessionDebug = false; 源代码网推荐 源代码网推荐 // Create some properties and get the default Session. 源代码网推荐 Properties props = System.getProperties(); 源代码网推荐 props.put("mail.host", host); 源代码网推荐 props.put("mail.transport.protocol", "smtp"); 源代码网推荐 源代码网推荐 Session mailSession = Session.getDefaultInstance(props, null); 源代码网推荐 源代码网推荐 // Set debug on the Session so we can see what is going on 源代码网推荐 // Passing false will not echo debug info, and passing true 源代码网推荐 // will. 源代码网推荐 mailSession.setDebug(sessionDebug); 源代码网推荐 源代码网推荐 // Instantiate a new MimeMessage and fill it with the 源代码网推荐 // required information. 源代码网推荐 Message msg = new MimeMessage(mailSession); 源代码网推荐 源代码网推荐 msg.setFrom(new InternetAddress(from)); 源代码网推荐 InternetAddress[] address = {new InternetAddress(to)}; 源代码网推荐 msg.setRecipients(Message.RecipientType.TO, address); 源代码网推荐 msg.setSubject(subject); 源代码网推荐 msg.setSentDate(new Date()); 源代码网推荐 msg.setText(messageText); 源代码网推荐 源代码网推荐 // Hand the message to the default transport service 源代码网推荐 // for delivery. 源代码网推荐 Transport.send(msg); 源代码网推荐 源代码网推荐 out.println("Mail was sent to " + to); 源代码网推荐 out.println(" from " + from); 源代码网推荐 out.println(" using host " + host + "."); 源代码网推荐 源代码网推荐 %> 源代码网推荐 </table> 源代码网推荐 </body> 源代码网推荐 </html> 源代码网推荐 源代码网推荐 源代码网供稿. |
