以前在自己电脑上装了一个邮件服务器,用以前的代码都可以收发。
今天准备把它和126,163,什么的连上,问题出现了,一运行就报错,晕死我了。
弄了半天原来126已经不支持SMTP/POP3了,后来弄了个QQ邮箱,并且开启了SMTP/POP3,总算发上去了。
把发送的代码发下
form:
<form action="Send" method="post" name="mail"> <table border="1" align="left"> <tr> <td> From: </td> <td> <input name="from">secret:<input name="secret"> </td> </tr> <tr> <td> TO: </td> <td> <input name="to"> </td> </tr> <tr> <td> Subject: </td> <td> <input name="subject"> </td> </tr> <tr> <td> Context: </td> <td> <textarea rows="10" cols="60" name="context"></textarea> </td> </tr> <tr> <td colspan="2"> <input type="submit"> </td> <td> </tr> </table> </form>
servlet
package org.zzx.send; import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @SuppressWarnings("serial") public class Send extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); request.setCharacterEncoding("gbk"); // 接收邮件类容 String from = request.getParameter("from"); String to = request.getParameter("to"); String subject = request.getParameter("subject"); String context = request.getParameter("context"); String secret = request.getParameter("secret"); // 邮件的服务器地址,邮件服务器的服务器名 String mailServerName = "smtp.qq.com"; try { // 获得系统属性 Properties prop = System.getProperties(); // 设置属性 prop.setProperty("mail.smtp.host", mailServerName); prop.setProperty("mail.smtp.port", "25"); prop.setProperty("mail.smtp.auth", "true"); String name=from.substring(0,from.indexOf("@")); MyAuthenticator au = new MyAuthenticator(name,secret); // 建立邮件发送的连接 Session session = Session.getDefaultInstance(prop, au); System.out.println("ddddddddddddddd"); // 创建邮件信息的载体 Message message = new MimeMessage(session); // 设置邮件相关属性 // 设置邮件发送地址 message.setFrom(new InternetAddress(from)); // 点到点的发送 message.setRecipient(Message.RecipientType.TO, new InternetAddress( to)); // 设置邮件主题 message.setSubject(subject); // 邮件发送时间 message.setSentDate(new Date()); // 设置邮件类容 message.setText(context); // 关键。。。。发送 Transport.send(message); } catch (Exception e) { e.printStackTrace(); } out.write("ok"); out.flush(); out.close(); } }
Authenticator
package org.zzx.send; import javax.mail.PasswordAuthentication; public class MyAuthenticator extends javax.mail.Authenticator { private String strUser; private String strPwd; public String getStrUser() { return strUser; } public void setStrUser(String strUser) { this.strUser = strUser; } public String getStrPwd() { return strPwd; } public void setStrPwd(String strPwd) { this.strPwd = strPwd; } public MyAuthenticator(String user, String password) { this.strUser = user; this.strPwd = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(strUser, strPwd); } }
一定要开启了SMTP/POP3,不然你再牛也发布上去