<%@ page import= " javax.mail.*, javax.mail.internet.*, javax.activation.*, java.util.*"%><html>
<head>
<title> JavaMail 电子邮件发送系统 </title></head>
<body>
<%
try{
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
sendMailSession = Session.getInstance(props, null);
props.put("mail.smtp.host", "smtp.sohu.com");
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(request.getParameter("from")));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("to")));
newMessage.setSubject(request.getParameter("subject"));
newMessage.setSentDate(new Date());
newMessage.setText(request.getParameter("text"));
transport = sendMailSession.getTransport("smtp");
transport.connect("smtp.sohu.com","wang76_cn","764688");
transport.send(newMessage);
%>
<p> 你的邮件已发送,请返回。 <p>
<%
}
catch(MessagingException m)
{
out.println(m.toString());
}
%>
</body>
</html>我从表单中提交form:   [email protected]
               to:   [email protected]
怎么出错呀: javax.mail.SendFailedException: Sending failed; nested exception is: javax.mail.MessagingException: 505 Client was not authenticated   

解决方案 »

  1.   

    Unfortunately, this doesn't use the Authenticator class in javax.mail, so you would have to build in your own mechanism to prompt the user for this information.You may also need to set the mail.smtp.auth property to true: props.put("mail.smtp.auth", "true");
      

  2.   

    <%@ page contentType="text/html;charset=gb2312" %>
    <%@ page import="sun.net.smtp.SmtpClient, java.io.*" %> 
    <% 
     String e_smtp=request.getParameter("smtp");
     String e_from=request.getParameter("from");
     String e_to=request.getParameter("to");
     String e_subject=request.getParameter("subject");
     String e_body=request.getParameter("body"); try{ 
      SmtpClient client = new SmtpClient(e_smtp); 
      client.from(e_from); 
      client.to(e_to); 
      PrintStream message = client.startMessage(); 
      message.println("To:" + e_to); 
      message.println("Subject:" + e_subject); 
      message.println(e_body); 
      message.println(); 
      client.closeServer(); 
     } 
    catch (IOException e){ 
      System.out.println("EMAIL 发送出错:"+e); 
    }
    out.println("--------------------------------发送成功--------------------------------<br>");
    out.println("主题:" + e_subject);
    out.println("<br>");
    out.println("BODY:" + e_body);
    %> 
      

  3.   

    稍微做下修改,程序如下(调试通过)
    <%@ page language="java" import= " javax.mail.*, javax.mail.internet.*, javax.activation.*, java.util.*"%><html>
    <head>
    <title> JavaMail 电子邮件发送系统 </title></head>
    <body>
    <%
    try{
    Properties props = new Properties();
    Session sendMailSession;
    Store store;
    Transport transport;
    sendMailSession = Session.getInstance(props, null);
    props.put("mail.smtp.host", "202.96.44.20");  //"202.96.44.20" 是“smtp.263.net”的IP!
    props.put("mail.smtp.auth", "true");  //add
    Message newMessage = new MimeMessage(sendMailSession);
    newMessage.setFrom(new InternetAddress("[email protected]"));
    newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
    newMessage.setSubject("subject1");
    newMessage.setSentDate(new Date());
    newMessage.setText("text");
    transport = sendMailSession.getTransport("smtp");
    newMessage.saveChanges();
    transport.connect("202.96.44.20","xz_zeng","password");
    transport.sendMessage(newMessage,newMessage.getRecipients(Message.RecipientType.TO));  //modify
    //transport.send(newMessage);
    transport.close();
    %>
    <p> 你的邮件已发送,请返回。 <p>
    <%
    }
    catch(SendFailedException e)
    {
      out.println(e.toString());
    }
    catch(MessagingException m)
    {
    out.println(m.toString());
    }%>
    </body>
    </html> 还有啊,你把你263的密码改一下吧,哈哈。。!