您的位置:首页 > 编程语言 > Java开发

javax.mail发送邮件jsp页面

2016-05-01 00:56 357 查看
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
<%@page import="javax.mail.*"%>
<%@page import="javax.mail.internet.*"%>
<%@page import="javax.activation.*"%>

<%

/*
sina.com:
POP3服务器地址:pop3.sina.com.cn(端口:110)
SMTP服务器地址:smtp.sina.com.cn(端口:25)

163.com:
POP3服务器地址:pop.163.com(端口:110)
SMTP服务器地址:smtp.163.com(端口:25)
*/

String recipient="000@qq.com";
String sender="aaa@sina.com";
String subject="hello";
String text="hello, this is my first jsp mail!";

String host="smtp.sina.com.cn";
String username="aaa@sina.com";
String password="000";

Properties props=new Properties();

props.put("mail.smtp.host",host);
props.put("mail.smtp.port","25");
props.put("mail.smtp.auth","true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

Session mailsession=Session.getDefaultInstance(props);
boolean sessionDebug=false;
mailsession.setDebug(sessionDebug);

MimeMessage message=new MimeMessage(mailsession);

message.setFrom(new InternetAddress(sender));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
message.setSubject(subject, "UTF-8");
message.setText(text);
message.saveChanges();

Transport transport=mailsession.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();

%>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>sendmail</title>
</head>
<body>
<h1>邮件已经发送!</h1>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: