在配置过程中有打
telnet localhost 4555
没有反应的需要在控制面板-->程序与功能-->打开或关闭windows功能中 打开Telnet服务即可
JavaMail是Java中专门用来处理电子邮件处理的一套规范,使用它可以方便的进行mail的发送、也可以使用JavaMail开发出类似于Microsoft Outlook样的应用程序。
JavaMail核心类:
发送邮件依靠之前配置过的James服务器和JavaMail(JAF)类完成,但是在进行邮件发送时,一定要注意的是,由于现在所有的邮箱用户都保存在了James服务器上,所以一定要首先编写一个可以用于服务器验证用户名和密码的操作类,此类必须继承javax.mail.Authenticator类,而且必须覆写getPasswordAuthentication()方法。
<%@ 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" ></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>
mydeal.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.*" %> <%@ page import="org.apache.commons.mail.*" %> <% 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"); String mailserver="localhost"; //局域网发送邮件时的SMTP服务器 SimpleEmail email = new SimpleEmail(); email.setHostName(mailserver);//设置邮件服务器 email.setAuthentication(from,password);//设置邮箱用户名和密码 email.setFrom(from); //设置发件人地址 email.addTo(to); //设置收件人地址 email.setSubject(subject); //设置主题 email.setSentDate(new Date());//设置发送时间 email.setMsg(messageText); //设置发送的消息 email.send(); //发送邮件 out.println("<script language='javascript'>alert('邮件已发送!');window.location.href='index.jsp';</script>"); }catch(Exception e){ e.printStackTrace(); System.out.println("发送邮件产生的错误:"+e.getMessage()); out.println("<script language='javascript'>alert('邮件发送失败!');window.location.href='index.jsp';</script>"); } %>
需要导的包:mail.jar