writemail.jsp:
<%@ page contentType="text/html;charset=GB2312" %><html><head><title>写邮件内容</title></head><body><form name="form1" method="post" action="sendmailHTML.jsp"><table width="100" border="1" align="center" cellspacing="1">    <tr >         <td width="34%">收信人地址(邮箱):</td>        <td width="66%"><input name="to" type="text" id="to"></td>    </tr>    <tr >         <td>主题:</td>        <td><input name="title" type="text" id="title"></td>    </tr>    <tr>         <td height="107" colspan="2">         <textarea name="content" cols="50" rows="5" id="content"></textarea></td>    </tr>    <tr align="center">         <td colspan="2">         <input type="submit" name="Submit" value="发送">        <input type="reset" name="Submit2" value="重输">        </td>    </tr></table></form></body></html>sendmailHTML.jsp:<%@ page contentType="text/html;charset=gb2312" %><%@ page import="java.util.*,javax.mail.*"%><%@ page import="javax.mail.internet.*"%><%!public String codeToString(String str){//处理中文字符串的函数  String s=str;  try    {    byte tempB[]=s.getBytes("ISO-8859-1");    s=new String(tempB);    return s;   }  catch(Exception e)   {    return s;   }  }%><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>发送邮件处理结果</title></head><body><%try{//----接收邮件相关内容----String to_mail=codeToString(request.getParameter("to"));String to_title=codeToString(request.getParameter("title"));String to_content=codeToString(request.getParameter("content"));Properties props=new Properties();//------建立邮件会话------props.put("mail.smtp.host","smtp.126.com");props.put("mail.smtp.auth","true");Session s=Session.getInstance(props);s.setDebug(true);MimeMessage message=new MimeMessage(s);//--给消息对象设置发件人、收件人、主题、发信时间--InternetAddress from=new InternetAddress("[email protected]");message.setFrom(from);//发件人InternetAddress to=new InternetAddress(to_mail);message.setRecipient(Message.RecipientType.TO,to);message.setSubject(to_title);message.setSentDate(new Date());//------给消息对象设置内容------//新建一个存放信件内容的BodyPart对象BodyPart mdp=new MimeBodyPart();//给BodyPart对象设置内容和格式的编码方式mdp.setContent(to_content,"text/html;charset=gb2312");//新建一个MimeMultipart对象用来存放BodyPart对象(事实上可以存放多个)Multipart mm=new MimeMultipart();//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)mm.addBodyPart(mdp);//把mm作为消息对象的内容message.setContent(mm);message.saveChanges();Transport transport=s.getTransport("smtp");//以smtp方式登录邮箱,第一个参数是发送邮件用的邮件服务器SMTP地址;第二个参数为用户名;第三个参数为密码transport.connect("smtp.126.com","tougao-email","8807698");transport.sendMessage(message,message.getAllRecipients());transport.close();%><div align="center"><p>发送HTML邮件到<%=to_mail%>成功!</p></div><%}catch(MessagingException e){out.println("发送邮件失败!");}%></body></html>调试结果:
DEBUG: setDebug: JavaMail version 1.3.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.126.com", port 25
邮件发送失败程序应该没问题,运行时已经把25端口的占用程序关闭了,但就是发不出去,为什么啊?