使用jsp发送邮件和附件文件

时间:2014-01-18 06:42:06

标签: java jsp email

我想使用jsp发送电子邮件。 我开发了这个小代码来发送电子邮件。 我添加了所有需要的库 java mail API和JAF。但它仍然无法正常工作。 我不知道这是怎么回事。 它显示错误:无法发送消息。

<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%> 
<%@ page import="javax.servlet.http.*,javax.servlet.*" %> 
<% String result; 
   String to = "kudale.ashish1992@gmail.com"; 
   String from = "kudale.ashish1992@yahoo.com"; 
   String host = "smtp.mail.yahoo.com"; 
   Properties properties = System.getProperties(); 
   properties.setProperty("mail.smtp.host", host); 
   properties.setProperty("mail.user", "Username");
   properties.setProperty("mail.password", "Paasword");
   //String to = request.getParameter("to"); 
   //String from = request.getParameter("from");
   String subject = request.getParameter("subject");
   String messageText = request.getParameter("body");
   Session mailSession = Session.getDefaultInstance(properties); 
   try{ 
       MimeMessage message = new MimeMessage(mailSession);
       message.setFrom(new InternetAddress(from)); 
       message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  
       BodyPart messageBodyPart = new MimeBodyPart(); 
       messageBodyPart.setText("This is message body"); 
       Multipart multipart = new MimeMultipart(); 
       multipart.addBodyPart(messageBodyPart); 
       messageBodyPart = new MimeBodyPart(); 
       String filename = "file.txt"; 
       DataSource source = new FileDataSource(filename); 
       messageBodyPart.setDataHandler(new DataHandler(source)); 
       messageBodyPart.setFileName(filename); 
       multipart.addBodyPart(messageBodyPart);   
       message.setContent(multipart ); 
       Transport.send(message); 
       String title = "Send Email"; 
       result = "Sent message successfully...."; 
    }
    catch (MessagingException mex) { 
       mex.printStackTrace(); 
       result = "Error: unable to send message...."; 
   }
 %> 
<html> 
<head> 
<title>Send email</title> 
</head> 
<body> 
<center> <h1>Send Attachement Email using JSP</h1> </center> 
<p align="center"> 
<% out.println("Result: " + result + "\n"); %> 
</p>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

我在我的机器上尝试了两次我得到的错误。

首先,你拼错了properties

roperties.setProperty("mail.password", "Paasword");

其次,file.txt的访问被拒绝错误。我设法给它一条路径并且有效。

结果:它显示我已成功发送消息。

<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%> 
<%@ page import="javax.servlet.http.*,javax.servlet.*" %> 
<% String result; 
   String to = "kudale.ashish1992@gmail.com"; 
   String from = "kudale.ashish1992@yahoo.com"; 
   String host = "localhost"; 
   Properties properties = System.getProperties(); 
   properties.setProperty("mail.smtp.host", host); 
   properties.setProperty("mail.user", "Username");
   properties.setProperty("mail.password", "Paasword");
   //String to = request.getParameter("to"); 
   //String from = request.getParameter("from");
   String subject = request.getParameter("subject");
   String messageText = request.getParameter("body");
   Session mailSession = Session.getDefaultInstance(properties); 
   try{ 
       MimeMessage message = new MimeMessage(mailSession);
       message.setFrom(new InternetAddress(from)); 
       message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  
       BodyPart messageBodyPart = new MimeBodyPart(); 
       messageBodyPart.setText("This is message body"); 
       Multipart multipart = new MimeMultipart(); 
       multipart.addBodyPart(messageBodyPart); 
       messageBodyPart = new MimeBodyPart(); 
       String filename = "file.txt"; 
       DataSource source = new FileDataSource(filename); 
       messageBodyPart.setDataHandler(new DataHandler(source)); 
       messageBodyPart.setFileName(filename); 
       multipart.addBodyPart(messageBodyPart);   
       message.setContent(multipart ); 
       Transport.send(message); 
       String title = "Send Email"; 
       result = "Sent message successfully...."; 
    }
    catch (MessagingException mex) { 
       mex.printStackTrace(); 
       result = "Error: unable to send message...."; 
   }
 %> 
<html> 
<head> 
<title>Send email</title> 
</head> 
<body> 
<center> <h1>Send Attachement Email using JSP</h1> </center> 
<p align="center"> 
<% out.println("Result: " + result + "\n"); %> 
</p>
</body>
</html>