Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用jsp发送邮件和附件文件_Java_Jsp_Email - Fatal编程技术网

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

Java 使用jsp发送邮件和附件文件,java,jsp,email,Java,Jsp,Email,我想用jsp发送电子邮件。 我已经开发了这个发送电子邮件的小代码。 我添加了所有需要的库,如 java邮件API和JAF。但它仍然不起作用。 我不知道这里面出了什么问题。 它向我显示错误:无法发送消息 <%@ page import="java.io.*,java.util.*,javax.mail.*"%> <%@ page import="javax.mail.internet.*,javax.activation.*"%> <%@ page import="

我想用jsp发送电子邮件。 我已经开发了这个发送电子邮件的小代码。 我添加了所有需要的库,如 java邮件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>

发送电子邮件
使用JSP发送附件电子邮件


我已经在我的机器上尝试过了,但出现了两个错误

首先,您在此处拼写错误了
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>

发送电子邮件
使用JSP发送附件电子邮件


请进行堆栈跟踪。您的smtp端口可能有问题。它可能未打开。仍不工作我打开了smtp端口25。您是否尝试将
smtp.mail.yahoo.com
作为主机?当你从雅虎邮箱发送邮件时,你收到邮件了吗。我刚试过,结果显示消息发送成功。代码没有更改。仍然不工作。我添加了完整的javamail API提取文件夹和JAF。我应该只添加jar文件吗?你有邮件吗?现在是共享堆栈跟踪的时候了。您需要在类路径中添加mail.jar和activation.jar文件。Java电子邮件API包和JAF包ara中的所有jar文件都可以在CLASSPATH.not working添加库属性中找到。启用smtp端口,enble TLS,将我的主机更改为smtp.mail.yahoo.com。仍然没有wokingShare堆栈跟踪。怎么看?您使用的应用程序服务器是什么。Tomcat,jboss。哪一个?