JavaMail的一个初级问题

今天刚开始学JAVAMAIL遇到个问题,刚开始报javax.mail.AuthenticationFailedException这个错,但是现在什么错也不报了,后台能打出东西,邮件也发不出去.这是为什么呢?请各位帮帮忙.

web.xml



  <servlet>

    <description>This is the description of my J2EE component</description>

    <display-name>This is the display name of my J2EE component</display-name>

    <servlet-name>send</servlet-name>

    <servlet-class>com.servlet.send</servlet-class>

  </servlet>



  <servlet-mapping>

    <servlet-name>send</servlet-name>

    <url-pattern>/send</url-pattern>

  </servlet-mapping>



index.html





<html>

  <head>

    <title>index.html</title>



  </head>

 

  <body>

    <form method="post" action="send">

FROM:<input name="from">


TO  :<input name="to">


Subject:<input name="subject">


Context:<input name="context">

<input type="submit">

    </form>

  </body>

</html>



send.java



public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {



response.setContentType("text/html");

PrintWriter out = response.getWriter();

request.setCharacterEncoding("gb2312");

String from = request.getParameter("from");

String to = request.getParameter("to");

String subject = request.getParameter("subject");

String context = request.getParameter("context");

System.out.println(context + "sddddddddddddddddddddddddddddddd");

// 确定要发送的邮件服务器的地址

String mailserver = "711abc1fd2994d8";

// 设置邮件的传输协议

try {

Properties prop = System.getProperties();

prop.put("mail.smtp.host", mailserver);




// 建立邮件发送的连接

Session session = Session.getDefaultInstance(prop, null);

// 创建发送的信息的载体

Message msg = new MimeMessage(session);

// 设置相关的邮件属性

msg.setFrom(new InternetAddress(from));

// 点到点的发送

msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));

msg.setSubject(subject);

msg.setSentDate(new Date());

msg.setText(context);

// 发送

Transport.send(msg);

} catch (Exception e) {



}



out.print("send ok");

out.flush();

out.close();

}

2个回答

你需要发送smtp认证

[code="java"]
prop.put("mail.smtp.auth", "true");
[/code]

完整代码请参考:[url]http://www.rgagnon.com/javadetails/java-0538.html[/url]

你把异常catch了,又什么都没做,当然看不到错误消息了。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问