本帖最后由 cthief 于 2013-03-13 20:41:43 编辑

解决方案 »

  1.   

    发送界面
    <%@ 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="[email protected]"/><br>
         主&nbsp;&nbsp;题<input type="text" size="40" name="subject"/><br>
         内&nbsp;&nbsp;容<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 = "[email protected]";
    //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);
    }
    }
      

  2.   

    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: 25EHLO 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:<[email protected]>
    250 Ok
    RCPT TO:<[email protected]>
    250 Ok
    DEBUG SMTP: Verified Addresses
    DEBUG SMTP:   [email protected]
    DATA
    354 End data with <CR><LF>.<CR><LF>
    From: [email protected]
    To: [email protected]
    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: base645Y+q5piv5LiA5Liq5rWL6K+V
    .
    250 Ok: queued as 
    QUIT
    221 Bye
    邮件发送成功
    用你的代码没有有测出问题,发送成功了。只测试业务逻辑,没有使用servlet方式发送
      

  3.   

    看你的qq邮箱是否支持smtp协议?
    登录qq邮箱---设置--账户---POP3/IMAP/SMTP/Exchange服务---勾选POP3/SMTP服务。再试
      

  4.   

    我之前也做了一个javamail,上传都我的资源里,自己看吧,包含数据库的;
      

  5.   

    你的Debug第4行显示的是DEBUG SMTP: useEhlo true, useAuth true但是我测试显示的是DEBUG SMTP: useEhlo true, useAuth false
      

  6.   

    邮箱的smtp是开启的,换了sina、qq的邮箱服务器都不行啊——怎么看javamail的版本,我在网上下的就光是个mail.jar 和activiation.jar的名字,没有版本号
      

  7.   

    好,我试试数字邮箱
    4l的朋友测试的邮件还在我邮箱里可以看见,就是说是可以发送成功的。可我自己就是发布成功
    javamail是1.4ea版本的
      

  8.   

    数字邮箱也不行啊。
    测试还是显示
    DEBUG SMTP: useEhlo true, useAuth false
      

  9.   

    props.put("mail.smtp.auth", true);改为
    props.put("mail.smtp.auth", "true");看下
      

  10.   

    你先不要用servlet,直接用main方法测一下你的后台代码。如果能通,那就是你配置的问题了。先试试