邮件发送简单例子-jsp文件
程序员文章站
2024-02-26 15:57:46
mailexample.jspjsp javamail example
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>