<jsp:directive.page contentType="text/html;charset=GBK" pageEncoding="UTF-8"/>
<%@ page import="java.util.*"%>
<%@ page import ="javax.mail.*"%>
<%@ page import ="javax.mail.internet.*"%>
<%@ page import ="javax.activation.*"%>
<%@ page import ="cn.com.gei.bjbp08.work.CheckSendEmail"%>
<%
 boolean result=false;
 String toemails="[email protected]";
 String content="你好";
 String subject="你好";
      try {
        //创建属性对象
        Properties props = new Properties();
        //设置邮件传输协议为:smtp
        props.put("mail.transpost.protocol","smtp");
        //设置邮件服务器地址
        props.put("mail.smtp.host", "smtp.163.com");
        //设置邮件验证为真
        props.put("mail.smtp.auth", "true");
        //设置邮件服务器端口
        props.put("mail.smtp.port","25");        //调用验证类进行验证
        CheckSendEmail auth=new CheckSendEmail("[email protected]","******");        //创建session对象
        Session sendMailSession;
        sendMailSession = Session.getInstance(props, auth);
        //设置输出调试信息
        sendMailSession.setDebug(true);        //创建信息对象
        Message newMessage = new MimeMessage(sendMailSession);        //输入发送信息
        //设置发信人地址
        newMessage.setFrom(new InternetAddress("[email protected]"));        //设置收信人地址,可以支持多用户发送
        newMessage.setRecipients(Message.RecipientType.TO,
                                InternetAddress.parse(toemails));
        //设置信件文本格式(当设置了附件,这里就不能有)
        newMessage.setContent("SendMail", "text/html; charset=gb2312");        //设置信件主题
        newMessage.setSubject(subject);        //设置信件发送日期
        newMessage.setSentDate(new Date());        //设置信件正文(当设置了附件,这里就不能有)
        newMessage.setText(content);
        //创建对象
        Transport transport;
        transport = sendMailSession.getTransport("smtp");        //发送
        result=true;
        transport.send(newMessage);//此处总是抛出异常,让人很是郁闷,但是邮件却发送成功!
out.println("成功");
      }
      catch (MessagingException ex) {
      out.println("失败");
          ex.printStackTrace();
      }%>

解决方案 »

  1.   

    import javax.mail.Authenticator;
    import javax.mail.PasswordAuthentication;public class CheckSendEmail extends Authenticator {
    private String m_username = null; private String m_userpass = null; public void setUsername(String username) {
    m_username = username;
    } public void setUserpass(String userpass) {
    m_userpass = userpass;
    } public CheckSendEmail(String username, String userpass) {
    super();
    setUsername(username);
    setUserpass(userpass);
    } public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(m_username, m_userpass);
    }
    }
      

  2.   

    你换邮箱了?
    很多免费邮箱新注册的用户不支持pop3协议,所以很多发送失败
      

  3.   

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>发送成功</title>
    </head>
    <body bgcolor="#CFF1E1"> <%
    String host = (String) session.getAttribute("host");
    String user = (String) session.getAttribute("user");
    String password = (String) session.getAttribute("password");%>

    <% try {
    String tto = request.getParameter("to");
    String ttitle = request.getParameter("title");
    String emailtype = request.getParameter("emailtype");//获取email类型
    String tcontent = request.getParameter("content");
    String tfj1 = request.getParameter("fj1");
    String tfj2 = request.getParameter("fj2"); Properties props = new Properties();
    props.put("mail.smtp.host", "smtp."+host);
    props.put("mail.smtp.auth", "true");
    Session s = Session.getInstance(props);
    s.setDebug(true); MimeMessage message = new MimeMessage(s); //给消息对象设置发件人/收件人/主题/发信时间
    InternetAddress from = new InternetAddress(
    user+"@"+host);
    message.setFrom(from);
    InternetAddress to = new InternetAddress(tto);
    message.setRecipient(Message.RecipientType.TO, to);
    message.setSubject(ttitle);
    message.setSentDate(new Date()); Multipart mm = new MimeMultipart();//新建一个MimeMultipart对象用来存放多个BodyPart对象 //设置信件文本内容
    BodyPart mdp = new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
    mdp.setContent(tcontent, emailtype + ";charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
    mm.addBodyPart(mdp);//将含有信件内容的BodyPart加入到MimeMultipart对象中 //设置信件的附件1(自定义附件:直接将所设文本内容加到自定义文件中作为附件发送)
    DataHandler dh = new DataHandler(tfj1,
    "text/plain;charset=gb2312");
    //新建一个DataHandler对象,并设置其内容和格式/编码方式
    if (tfj1.length() > 0) {
    mdp = new MimeBodyPart();//新建一个存放附件的BodyPart mdp.setFileName("text.txt");//加上这句将作为附件发送,否则将作为信件的文本内容
    mdp.setDataHandler(dh);//给BodyPart对象设置内容为dh
    mm.addBodyPart(mdp);//将含有附件的BodyPart加入到MimeMultipart对象中
    }
    //设置信件的附件2(用本地上的文件作为附件)
    if (tfj2.length() > 0) {
    mdp = new MimeBodyPart();
    FileDataSource fds = new FileDataSource(tfj2);
    dh = new DataHandler(fds);
    int ddd = tfj2.lastIndexOf("\\");
    String fname = tfj2.substring(ddd + 1);//提取文件名
    //String ffname = new String(fname.getBytes("gb2312"),
    //"ISO8859-1");//处理文件名是中文的情况]
    String ffname = javax.mail.internet.MimeUtility.encodeText(fname);
    mdp.setFileName(ffname);//可以和原文件名不一致,但最好一样
    mdp.setDataHandler(dh);
    mm.addBodyPart(mdp);
    }
    message.setContent(mm);//把mm作为消息对象的内容
    message.saveChanges();
    Transport transport = s.getTransport("smtp");
    transport.connect("smtp."+host, user, password);
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
    %>

    <div align="center">
    <p>
    <font color="#FF6600">发送成功!</font>
    </p>
    <p>
    <a href="receivemail.jsp">去看看我的信箱</a>
    <br>
    <br>
    <a href="sendmail.html">再发一封</a>
    </p>
    </div>
    <%
    } catch (MessagingException e) {
    out.println(e.toString());
    }
    %>
    </body>
    </html>
      

  4.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>撰写邮件</title>
    </head>
    <body bgcolor="#CFF1E1">
    <form action="sendmail.jsp" method="post" name="form1">
    <table width="80" border="0" align="center" cellspacing="1"
    bgcolor="#006600" class="black">
    <tr bgcolor="#CFF1E1">
    <td width="25%">
     收信人地址:
    </td>
    <td width="76%">
    <input name="to" type="text" id="to" size=50>
    </td>
    </tr>
    <tr bgcolor="#CFF1E1">
    <td>
     主题:
    </td>
    <td>
    <input name="title" type="text" id="title" size=50>
    </td>
    </tr>
    <tr>
    <td height="18" colspan="2" bgcolor="#CFF1E1">
     信件类型&nbsp;
    <select name="emailtype" id="emailtype">
    <option value="text/plain" selected>
     Text
    </option>
    <option value="text/html">
     Html
    </option>
    </select>
    </td>
    </tr>
    <tr>
    <td height="53" colspan="2" bgcolor="#CFF1E1">
    <textarea name="content" cols="70" rows="20" id="content"></textarea>
    </td>
    </tr>
    <tr align="left">
    <td colspan="2" bgcolor="#CFF1E1">
     附件1(自定义):
    <input name="fj1" type="text" id="fj1" size="30">
     (输入文本信息)
    </td>
    </tr>
    <tr align="left" valign="bottom">
    <td colspan="2" bgcolor="#CFF1E1">
    附件2(本 地):&nbsp;
    <input name="fj2" type="file" id="fj2" size="30">
    </td>
    </tr>

    <tr align="center">
    <td colspan="2" bgcolor="#CFF1E1">
    <input type="submit" name="Submit" value="发送">
    <input type="reset" name="Submit2" value="重置">      
    </td>
    </tr>
    </table>
    </form>
    <center>
    <a href="receivemail.jsp"><font color="ffac00">我的收件箱</font></a>
    </center>
    </body>
    </html>
      

  5.   

    我用  胡须棉花糖   给的代码试了一下,总是报 java.lang.IllegalStateException: Not connected,这是为什么呀
      

  6.   

    看看是不是这个原因http://blog.csdn.net/flysky_lfx/archive/2007/06/29/1671997.aspx
      

  7.   

    notingDEBUG: 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.sina.com", port 25220 mail5-201.sinamail.sina.com.cn ESMTP
    DEBUG SMTP: connected to host "smtp.sina.com", port: 25EHLO HAOBO_TOA
    250-mail5-201.sinamail.sina.com.cn
    250-8BITMIME
    250-SIZE 52428800
    250-AUTH PLAIN LOGIN
    250 AUTH=PLAIN LOGIN
    DEBUG SMTP: Found extension "8BITMIME", arg ""
    DEBUG SMTP: Found extension "SIZE", arg "52428800"
    DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
    DEBUG SMTP: Found extension "AUTH=PLAIN", arg "LOGIN"
    DEBUG SMTP: Attempt to authenticate
    AUTH LOGIN
    334 VXNlcm5hbWU6
    TEFOU0VZT1VZVVRBT0AxNjMuY29t
    334 UGFzc3dvcmQ6
    MTk4NjIxMA==
    535 #5.7.0 Authentication failed
    javax.mail.AuthenticationFailedException
    老是这个异常,请大侠们帮个忙!
      

  8.   

    String toemails="[email protected]"; 
            //设置邮件服务器地址 
     props.put("mail.smtp.host", "smtp.163.com"); 你把邮件服务器 设置成sina的 或者你发给
    163的邮箱看看行不