前几天帮一位老师做一个邮件群发,但是要做到单独显示的邮件发送程序,之前没有碰过JavaMail,在图书馆找了一本JavaWeb宝典,看了看里面的关于JavaMail的实例,很容易上手也很容易理解,很快就做完了。在这里和大家分享一下。
凡是从简入繁。首先来看看如何发送一个普通的Email
首先准备环境:
Java发送邮件有用到JavaMail的包
mail.jar 和ac.jar(当然命名可能不同action,这个无所谓,到官网或者其他地方都可以下到相应的jar包)
整个程序用Jsp来简单表述一下。
发送页面就不必解释了
<%@ page contentType="text/html; charset=GBK" language="java" %>
<html>
<head>
<title>发送普通文本格式的E-mail</title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<link href="css/style.css" rel="stylesheet">
<script language="javascript">
function checkform(myform){
for(i=0;i<myform.length;i++){
if(myform.elements[i].value==""){
alert(myform.elements[i].title+"不能为空!");
myform.elements[i].focus();
return false;
}
}
}
</script>
</head>
<body>
<form name="form1" method="post" action="mydeal.jsp" onSubmit="return checkform(form1)">
<table width="588" height="360" border="0" align="center" cellpadding="0" cellspacing="0" background="images/bg.jpg">
<tr>
<td width="60" height="73" background="Images/board_left.gif"> </td>
<td width="486" background="Images/board_left.gif"> </td>
<td width="46" background="Images/board_left.gif"> </td>
</tr>
<tr valign="top">
<td height="247"> </td>
<td valign="top"><table width="96%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="16%" height="27" align="center">收件人:</td>
<td width="84%" colspan="2" align="left"><input name="to" type="text" id="to" title="收件人" size="60" value="[email protected]"></td>
</tr>
<tr>
<td height="27" align="center">发件人:</td>
<td colspan="2" align="left"><input name="from" type="text" id="from" title="发件人" size="60"></td>
</tr>
<tr>
<td height="27" align="center">密 码:</td>
<td colspan="2" align="left"><input name="password" type="password" id="password" title="发件人信箱密码" size="60"></td>
</tr>
<tr>
<td height="27" align="center">主 题:</td>
<td colspan="2" align="left"><input name="subject" type="text" id="subject" title="邮件主题" size="60"></td>
</tr>
<tr>
<td height="93" align="center">内 容:</td>
<td colspan="2" align="left"><textarea name="content" cols="59" rows="7" class="wenbenkuang" id="content" title="邮件内容"></textarea></td>
</tr>
<tr>
<td height="30" align="center"> </td>
<td height="40" align="right"><input name="Submit" type="submit" class="btn_bg" value="发送">
<input name="Submit2" type="reset" class="btn_bg" value="重置">
<input name="Submit3" type="button" class="btn_bg" onClick="window.close();" value="关闭">
</td>
<td align="left"> </td>
</tr>
</table></td>
<td> </td>
</tr>
<tr valign="top">
<td height="40"> </td>
<td> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
关键的是处理的页面,简单起见,还用一个Jsp来实现
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ page import="java.util.*" %>
<%@ page import ="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%
try{
request.setCharacterEncoding("GBK");
String from=request.getParameter("from");
String to=request.getParameter("to");
String subject=request.getParameter("subject");
String messageText=request.getParameter("content");
String password=request.getParameter("password");
//生成SMTP的主机名称
/*因为我们发送邮件用的是SMTP协议,所以要有一些设置,如果你的发送人是一个你申请的邮箱,那么就用你的邮箱服务器,也就是用下面的注释代码,否则用注释后的代码*/
//int n =from.indexOf('@');
//int m=from.length() ;
//String mailserver ="smtp."+from.substring(n+1,m);
String mailserver="mrlx";
//建立邮件会话
Properties pro=new Properties();
pro.put("mail.smtp.host",mailserver);
pro.put("mail.smtp.auth","true");
Session sess=Session.getInstance(pro);
sess.setDebug(true);
//新建一个消息对象
MimeMessage message=new MimeMessage(sess);
//设置发件人
InternetAddress from_mail=new InternetAddress(from);
message.setFrom(from_mail);
//设置收件人
InternetAddress to_mail=new InternetAddress(to);
message.setRecipient(Message.RecipientType.TO ,to_mail);
//设置主题
message.setSubject(subject);
//设置内容
message.setText(messageText);
//设置发送时间
message.setSentDate(new Date());
//发送邮件
message.saveChanges(); //保证报头域同会话内容保持一致
Transport transport =sess.getTransport("smtp");
transport.connect(mailserver,from,password);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
out.println("<script language='javascript'>alert('邮件已发送!');window.location.href='index.jsp';</script>");
}catch(Exception e){
System.out.println("发送邮件产生的错误:"+e.getMessage());
out.println("<script language='javascript'>alert('邮件发送失败!');window.location.href='index.jsp';</script>");
}
%>