当前位置: 移动技术网 > IT编程>开发语言>Java > 邮件发送简单例子-jsp文件

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

2017年12月12日  | 移动技术网IT编程  | 我要评论
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>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网