a52348477
a52348477
2008-12-18 08:32

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条回答

为你推荐