郵件發(fā)送簡單例子-jsp文件
更新時間:2006年10月13日 00:00:00 作者:
MailExample.jsp
<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>
<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>
相關(guān)文章
jsp導(dǎo)出身份證到excel時候格式不對但以X結(jié)尾的卻可以
excel導(dǎo)出身份證的時候顯示有的對有的不對,身份證以X結(jié)尾的可以,其它都顯示不正確,關(guān)于這個問題的解決方法如下,需要的朋友可以參考下2014-10-10
JSP實用教程之簡易文件上傳組件的實現(xiàn)方法(附源碼)
文件上傳是我們在日常開發(fā)中經(jīng)常遇到的一個功能,下面這篇文章主要給大家介紹了關(guān)于JSP實用教程之簡易文件上傳組件的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,需要的朋友們下面來一起看看吧。2017-07-07
JSP中的字符替換函數(shù) str_replace() 實現(xiàn)!
JSP中的字符替換函數(shù) str_replace() 實現(xiàn)!...2006-10-10
JSP教程之使用JavaBean完成業(yè)務(wù)邏輯的方法
這篇文章主要介紹了JSP教程之使用JavaBean完成業(yè)務(wù)邏輯的方法,較為詳細的分析了JavaBean完成業(yè)務(wù)邏輯所涉及的相關(guān)概念及使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09
JBuilder2005單元測試之創(chuàng)建測試固件
這篇文章主要介紹了JBuilder2005單元測試之創(chuàng)建測試固件2006-10-10
深入淺析Jsp中 out.print 和 out.write 的區(qū)別
本文簡明扼要的給大家介紹了jsp中 out.print 和 out.write 的區(qū)別,雖然本文簡短但是主要內(nèi)容給大家介紹清楚了,需要的朋友參考下吧2017-02-02

