Web开发常用模块——JavaMail邮件
发送页面<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Write Email</title>
</head>
<body>
<form name="form1" method="post" action="sendMail.jsp">
<table width="550px" border="1" align="center">
<caption>写邮件</caption>
<tr>
<td width="20%">收信人:</td>
<td width="80%"><input type="email" name="to" style='width:400px;'/></td>
</tr>
<tr>
<td>主题:</td>
<td><input type="text" name="title" style='width:400px;'/></td>
</tr>
<tr>
<td>正文:</td>
<td><textarea name="content" cols="50" rows="10"></textarea></td>
</tr>
<tr align="center">
<td colspan=2><input type="submit" value="发送.."/></td>
</tr>
</table>
</form>
</body>
</html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="mail.SendMail" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Send Mail</title>
</head>
<body>
<%
//读取参数
request.setCharacterEncoding("UTF-8");
String smtpHost="smtp.163.com";
String from="hadron_cheng@163.com";
String to=request.getParameter("to");
String title =request.getParameter("title");
String msg=request.getParameter("content");
try{
SendMail.sendMessage(smtpHost, from, to, title,msg);
out.println("<h2>发送成功,请查收。</h2>");
}catch(Exception e){
e.printStackTrace();
}
%>
</body>
</html>
package mail;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendMail {
public static void sendMessage(String smtpHost,
String _from, String _to,
String subject, String content)
throws Exception{
//邮件服务器属性
Properties props = new Properties();
//权限验证
props.setProperty("mail.smtp.auth", "true");
//邮件服务器属性
props.setProperty("mail.smtp.host", smtpHost);
//指定邮件协议
props.put("mail.transport.protocol", "smtp");
//邮件回话
Session session = Session.getInstance(props);
//发件和收件地址
InternetAddress from = new InternetAddress(_from);
InternetAddress to = new InternetAddress(_to);
//定义消息对象
MimeMessage msg = new MimeMessage(session);
//设置发件人
msg.setFrom(from);
//添加收件人地址
msg.addRecipient(javax.mail.Message.RecipientType.TO, to);
//设置日期
msg.setSentDate(new Date());
//设置主题,如果出现乱码,请启用注释掉的语句
//msg.setSubject(MimeUtility.encodeText(subject,"UTF-8","B"));
msg.setSubject(subject);
//设置正文
msg.setContent(content,"text/html;charset=UTF-8");
//发送
Transport transport = session.getTransport("smtp");
transport.connect(smtpHost, "hadron_cheng", "123456!");
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
}
}
接收邮件
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.*" %>
<%@page import="javax.mail.*" %>
<%@page import="javax.mail.internet.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mail List</title>
</head>
<body>
<%
Properties props = new Properties();
Session mSession = Session.getInstance(props);
Store store = mSession.getStore("pop3");
store.connect("pop3.163.com","java_edu", ".123456!");
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message message[] = folder.getMessages();
out.println("<p>以下是java_edu@163.com邮箱中收件箱中的内容,</p>"
+"共有"+message.length+"封邮件:<br>");
String id;//邮件id
%>
<table align="center" border="1">
<tr>
<td>序号</td>
<td>标题</td>
<td>发送者地址</td>
<td>邮件大小</td>
<td>发送时间</td>
</tr>
<%for(int i=0;i<message.length;i++){%>
<tr>
<td><%=i+1 %></td>
<%id=((MimeMessage)(message)).getMessageID(); %>
<td><a href='viewMail.jsp?id=<%=id%>'><%=message.getSubject()%></a></td>
<td><%=(String)((InternetAddress)message.getFrom()).getAddress() %></td>
<td><%=message.getSize()%></td>
<td><%=message.getSentDate()%></td>
</tr>
<%}%>
</table>
<%
folder.close(true);
store.close();
%>
</body>
</html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<html>
<head>
<title>接收邮件</title>
</head>
<body>
<%
String messageId=request.getParameter("id");
Session mSession=Session.getInstance(System.getProperties(),null);
mSession.setDebug(false);
Store store = mSession.getStore("pop3");
store.connect("pop3.163.com","java_edu", ".123456!");
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message message[] = folder.getMessages();
int i=0;
for(;i<message.length;i++)//查找到id
if(((MimeMessage)(message)).getMessageID().equals(messageId))
break;
%>
<table align="center" border="1" width="800">
<tr >
<td width="20%">邮件标题:</td>
<td><%=message.getSubject()%></td>
</tr>
<tr>
<td>发送者邮箱:</td>
<td><%=(String)((InternetAddress)message.getFrom()).getAddress() %></td>
</tr>
<tr>
<td>邮件内容:</td>
<td>
<%
Object content =message.getContent();
out.print(content);
%>
</td>
</tr>
</table>
</body>
</html>
页:
[1]