DEBUG: setDebug: JavaMail version 1.4ea EHLO ODR88FQJUVOOV0B at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388) |
|
发送界面
<%@ page language="java" import="java.util.*" contentType="text/html; charset=gb2312"%> <html> <head> <title>JavaMail发送文本文件</title> </head> <body> <div align="center"> <font size="2">使用JavaMail发送文本邮件示例<br/> <form method="post" action="SendTextMail"> 收信人:<input type="text" size="40" name="to"/><br> 发信人:<input type="text" size="40" name="from" value="yajuan.sun@qq.com"/><br> 主 题<input type="text" size="40" name="subject"/><br> 内 容<textarea rows="6" cols="38" name="content"></textarea><br> <input type="submit" value="发送"/> <input type="reset" value="取消"/> </form> </font> </div> </body> </html> 调用的servlet package servlets; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import beans.TextMail; public class SendTextMail extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置发送方的SMTP地址 String host = "smtp.qq.com"; //user String name = "yajuan.sun@qq.com"; //password String password = "qqyouxiang"; TextMail mail = new TextMail(host,name,password); mail.setFrom(request.getParameter("from")); mail.setTo(request.getParameter("to")); mail.setSubject(request.getParameter("subject")); mail.setText(request.getParameter("content")); response.setContentType("text/html"); response.setCharacterEncoding("gb2312"); PrintWriter out = response.getWriter(); if(mail.send()) out.print("<font size=""2"">邮件发送成功</font>"); else out.print("<font size=""2"">邮件发送失败</font>"); } } 邮件发送 package beans; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class TextMail { private MimeMessage message; private Properties props; private Session session; private String name=""; private String password=""; public TextMail(String host,String name,String password){ //发送邮件初始化 this.name = name; this.password = password; props = new Properties(); //设置SMTP主机 props.put("mail.smtp.host", host); //设置SMTP属性验证 props.put("mail.smtp.auth", true); //获得邮件会话对象 MyAuthenticator auth = new MyAuthenticator(name,password); session = Session.getDefaultInstance(props, auth); session.setDebug(true); //创建MIME邮件对象 message = new MimeMessage(session); } public void setFrom(String from){ try{ message.setFrom(new InternetAddress(from)); }catch(AddressException e){ System.out.println("setFrom地址错误");} catch(MessagingException e){System.out.println("setFrom设置message错误");} } public void setTo(String to){ try{ message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); }catch(AddressException e){e.printStackTrace();} catch(MessagingException e){e.printStackTrace();} } public void setSubject(String subject){ try{ message.setSubject(subject); }catch(Exception e){ e.printStackTrace(); } } public void setText(String text){ try{ message.setText(text); }catch(Exception e){ e.printStackTrace(); } } //send public boolean send(){ // try { // System.out.println("写出message"); //// message.writeTo(System.out); // } catch (FileNotFoundException e1) { // // TODO Auto-generated catch block // System.out.println("写message出错"); // } catch (IOException e1) { // // TODO Auto-generated catch block // e1.printStackTrace(); // } catch (MessagingException e1) { // // TODO Auto-generated catch block // e1.printStackTrace(); // } Transport transport = null; try{ //创建SMTP邮件协议发送对象 transport = session.getTransport("smtp"); System.out.println("SMTP邮件协议对象创建成功"); }catch(Exception e){ System.out.println("创建smtp邮件协议发送对象失败");return false;} try{//取得与邮件服务器的连接 transport.connect((String)props.get("mail.smtp.host"),name, password); System.out.println("连接服务器成功"); }catch(Exception e2){System.out.println("与邮件服务器连接失败");return false;} try{//通过邮件服务器发送邮件 transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); }catch(Exception e3){System.out.println("发送失败"); return false;} try{transport.close();}catch(Exception e){System.out.println("关闭连接失败");return false;} return true; // }catch(NoSuchProviderException e){ // e.printStackTrace(); // return false; // }catch(MessagingException e){ // e.printStackTrace(); // return false; // } } } 认证 package beans; import javax.mail.PasswordAuthentication; import javax.mail.Authenticator; public class MyAuthenticator extends Authenticator { String name; String password; public MyAuthenticator(String name,String password){ this.name = name; this.password = password; } protected PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(name,password); } } |
|
高手请帮忙看看啊,万分感谢!
已经在网上找了好多办法,但是都不行啊!请高手醍醐灌顶啊! |
|
不能沉啊 请高手帮忙看看问题出在哪儿了?
|
|
20分 |
DEBUG: setDebug: JavaMail version 1.4.7 DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] SMTP邮件协议对象创建成功 DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "smtp.qq.com", port 25, isSSL false 220 smtp.qq.com Esmtp QQ Mail Server DEBUG SMTP: connected to host "smtp.qq.com", port: 25 EHLO moby-PC 250-smtp.qq.com 250-PIPELINING 250-SIZE 52428800 250-AUTH LOGIN PLAIN 250-AUTH=LOGIN 250-MAILCOMPRESS 250 8BITMIME DEBUG SMTP: Found extension "PIPELINING", arg "" DEBUG SMTP: Found extension "SIZE", arg "52428800" DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN" DEBUG SMTP: Found extension "AUTH=LOGIN", arg "" DEBUG SMTP: Found extension "MAILCOMPRESS", arg "" DEBUG SMTP: Found extension "8BITMIME", arg "" DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM DEBUG SMTP: AUTH LOGIN command trace suppressed DEBUG SMTP: AUTH LOGIN succeeded 连接服务器成功 DEBUG SMTP: use8bit false MAIL FROM:<yajuan.sun@qq.com> 250 Ok RCPT TO:<test@qq.com> 250 Ok DEBUG SMTP: Verified Addresses DEBUG SMTP: chiqingye@qq.com DATA 354 End data with <CR><LF>.<CR><LF> From: yajuan.sun@qq.com To: test@qq.com Message-ID: <6037166.0.1363187885900.JavaMail.moby@moby-PC> Subject: =?UTF-8?B?5rWL6K+V?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: base64 5Y+q5piv5LiA5Liq5rWL6K+V . 250 Ok: queued as QUIT 221 Bye 邮件发送成功 用你的代码没有有测出问题,发送成功了。只测试业务逻辑,没有使用servlet方式发送 |
20分 |
我也测试了是可以的,是不是你的配置出了问题 |
看你的qq邮箱是否支持smtp协议?
登录qq邮箱—设置–账户—POP3/IMAP/SMTP/Exchange服务—勾选POP3/SMTP服务。再试 |
|
5分 |
你javamail版本多少?
|
5分 |
我之前也做了一个javamail,上传都我的资源里,自己看吧,包含数据库的;
|
你的Debug第4行显示的是 DEBUG SMTP: useEhlo true, useAuth true 但是我测试显示的是DEBUG SMTP: useEhlo true, useAuth false |
|
先谢谢,如果是配置错,配置会是错在哪儿? |
|
邮箱的smtp是开启的,换了sina、qq的邮箱服务器都不行啊——
怎么看javamail的版本,我在网上下的就光是个mail.jar 和activiation.jar的名字,没有版本号 |
|
建议用数字的qq邮箱试下
|
|
数字邮箱也不行啊。
测试还是显示 DEBUG SMTP: useEhlo true, useAuth false |
|
50分 |
props.put(“mail.smtp.auth”, true);改为
props.put(“mail.smtp.auth”, “true”);看下 |
成功了,问题竟然出在这儿了,天哪!双引号引发的绝望啊!
谢谢你这么耐心帮我看程序!太谢谢了!谢谢楼上各位! |
|
你先不要用servlet,直接用main方法测一下你的后台代码。如果能通,那就是你配置的问题了。先试试
|
|
请问,大侠用的什么调试工具? |
|
楼主,你怎么打印出这些发送状态的日志的呢,跪求:
|
|
session.setDebug(true); |
|
16楼的是正解
|