javaweb邮件的在线发送
本示例都写在了jsp页面中,读者可以把它转移到.java文件中,
这样可以减轻jsp页面的负担,更易于管理。
在做之前,需要导包
代码示例;
<span style="font-size:14px;"><%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@page import="cn.edu.bzu.dao.TitleDAO"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<style type="text/css">
<!--
body
{
background-color: #D6D6D6;
background-image: url(images/bg1.jpg);
}
.ww1 {
height: 30px;
width: 180px;
}
#apDiv1 {
position: absolute;
left: 657px;
top: 70px;
width: 408px;
height: 247px;
z-index: 1;
}
-->
</style>
<%
request.setCharacterEncoding("gbk");
String name=request.getParameter("id");
String password =request.getParameter("password");
TitleDAO us=new TitleDAO();
if( us.delTitleById(name,password)){
out.print("您已登录成功");
}
else{
out.print("登录失败,您的账号或密码输入错误");
response.sendRedirect("error.jsp");
}
%>
<h4 align=“center”>在线发送邮件</h4>
<form action="main.jsp" method="post" name="form1">
<table border="1">
<tr>
<td width="40%"> 邮件服务器</td>
<td width="60%"><input type="text" name="hostname" value="smtp.163.com"/></td>
</tr>
<tr>
<td width="40%"> 用户名</td>
<td width="60%"><input type="text" name="username" value="zhupeng"/></td>
</tr>
<tr>
<td width="40%">密码</td>
<td width="60%"><input type="password" name="userpass" /></td>
</tr>
<tr>
<td width="40%">收信人</td>
<td width="60%"><input type="text" name="to" /></td>
</tr>
</tr>
<tr>
<td width="40%">主题</td>
<td width="60%"><input type="text" name="subject" /></td>
</tr>
</tr>
<tr>
<td width="40%">内容</td>
<td width="60%"><textarea name="message" cols=40 rows=5></textarea></td>
</tr>
</table>
<input type="submit" value="发送"/>
<input type="submit" value="清除"/>
</form>
</body>
</html></span>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="javax.mail.Session"%>
<%@page import="javax.mail.Message"%>
<%@page import="javax.mail.internet.MimeMessage"%>
<%@page import="javax.mail.internet.InternetAddress"%>
<%@page import="javax.mail.Transport"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'main.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<style type="text/css">
<!--
body {
background-color: #D6D6D6;
background-image: url(images/bg1.jpg);
}
.ww1 {
height: 30px;
width: 180px;
}
#apDiv1 {
position: absolute;
left: 657px;
top: 70px;
width: 408px;
height: 247px;
z-index: 1;
}
-->
</style>
<a href="login.jsp"> 返回</a>
<%
request.setCharacterEncoding("utf-8");
out.print("正在发送邮件....<P>");
try{
String strhost=request.getParameter("hostname");//获取邮件的邮箱服务器地址
int nport=0;
String strusername=request.getParameter("username");//发送人邮箱账号
String userpass=request.getParameter("userpass"); //发送人邮箱密码
String strform=strusername+"@"+strhost.replaceAll("smtp.","");
String strlist=request.getParameter("to");//接收人的邮箱地址
String strcclist=null;
String strbblist=null;
String sttsubject =request.getParameter("subject");//邮件主题
String strmessage =request.getParameter("message");//邮件内容
//创建邮件的 属性
Properties pro=new Properties();
pro.put("mail.smtp.host",strhost);
pro.put("mail.smtp.auth","true");
Session mailsession=Session.getDefaultInstance(pro,null);
mailsession.setDebug(true);
//创建一个消息
Message msg= new MimeMessage(mailsession);
//发件人地址
InternetAddress from=new InternetAddress(strform);
msg.setFrom(from);
//目标地址
if(strlist!=null&&strlist.length()>0){
InternetAddress [] to= { new InternetAddress (strlist)};
msg.setRecipients(Message.RecipientType.TO,to);
}
else
{
throw new Exception("no recipient");
}
//密送
if(strbblist!=null&&strbblist.length()>0){
InternetAddress [] oto= { new InternetAddress (strbblist)};
msg.setRecipients(Message.RecipientType.BCC,oto);
}
//抄送
if(strcclist!=null&&strcclist.length()>0){
InternetAddress [] oo= { new InternetAddress (strcclist)};
msg.setRecipients(Message.RecipientType.CC,oo);
}
//添加邮件主题
if(sttsubject!=null){
msg.setSubject(sttsubject);
}
//邮件日期
msg.setSentDate(new Date());
//添加邮件的内容
if(strmessage!=null&&strmessage.trim().length()>0){
msg.setText(strmessage);
}else
{
msg.setText("没有要发送的信息");
}
msg.saveChanges();
int n=-1;
if(nport>0)
n=nport;
Transport trans=mailsession.getTransport("smtp");
trans.connect(strhost,n,strusername,userpass);
trans.sendMessage(msg,msg.getAllRecipients());
trans.close();
out.print("邮件发送成功");
}
catch(Exception e){
out.print("邮件发送失败");
}
%>
</body>
</html>
然后根据输入的内容,就可以发送到指定的邮件地址