当前位置:首页 > JSP教程 > JSP常见问题

邮件发送简单例子-jsp文件

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>


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐