使用jsp发送邮件和附件文件 [英] send mail and attachment file using jsp
本文介绍了使用jsp发送邮件和附件文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用jsp发送电子邮件。
我已经开发了这个小代码来发送电子邮件。
我已经添加了所有需要的库,如
java邮件API和JAF。但仍然不行。
我不知道这里出了什么问题。
它显示错误:无法发送消息。
I want to send email using jsp. I have develop this small code to send email. I have added all require library like java mail API and JAF. But still it is not working. I don't know whats going wrong in this. it shows me Error: unable to send message.
<%@ 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>
推荐答案
我在机器上尝试过两个错误
I have tried this on my machine two errors I got.
首先,您拼错了属性
这里
roperties.setProperty("mail.password", "Paasword");
其次,访问被拒绝为 file.txt
。
Second, Access is denied error for file.txt
. I managed to give a path to it and it worked.
结果:显示我的邮件已成功发送。
<%@ 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发送邮件和附件文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文