我正在尝试使用JavaMail发送邮件,但出现IO异常,提示访问被拒绝
请让我知道我在哪里出错。我已经使用Java邮件和Internet API以及datasource和datahandler API。另外,我尝试作为附件发送的html文件位于适当的路径中,并且我对该文件具有访问权限。下面是我的代码
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendMail
{
public static void execute(String reportFileName) throws Exception
{
String path="C:/reports";
String[] to={"[email protected]"};
String[] cc={"[email protected]"};
String[] bcc={"[email protected]"};
SendMail.sendMail("[email protected]",
"******",
"smtp.gmail.com",
"465",
"true",
"true",
true,
"javax.net.ssl.SSLSocketFactory",
"false",
to,
cc,
bcc,
"Test Mail",
"Test Message",
path,
reportFileName);
}
public static boolean sendMail(String userName,
String passWord,
String host,
String port,
String starttls,
String auth,
boolean debug,
String socketFactoryClass,
String fallback,
String[] to,
String[] cc,
String[] bcc,
String subject,
String text,
String attachmentPath,
String attachmentName){
//Object Instantiation of a properties file.
Properties props = new Properties();
props.put("mail.smtp.user", userName);
props.put("mail.smtp.host", host);
props.put("mail.smtp.password", passWord);
props.put("mail.smtps.auth", "true");
if(!"".equals(port)){
props.put("mail.smtp.port", port);
}
if(!"".equals(starttls)){
props.put("mail.smtp.starttls.enable",starttls);
props.put("mail.smtp.auth", auth);
}
if(debug){
props.put("mail.smtp.debug", "true");
}else{
props.put("mail.smtp.debug", "false");
}
if(!"".equals(port)){
props.put("mail.smtp.socketFactory.port", port);
}
if(!"".equals(socketFactoryClass)){
props.put("mail.smtp.socketFactory.class",socketFactoryClass);
}
if(!"".equals(fallback)){
props.put("mail.smtp.socketFactory.fallback", fallback);
}
try{
props.put("mail.smtp.isSSL", "true");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
msg.setText(text);
msg.setSubject(subject);
Multipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachmentPath);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachmentName);
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
msg.setFrom(new InternetAddress(userName));
for(int i=0;i<to.length;i++){
msg.addRecipient(Message.RecipientType.TO, new
InternetAddress(to[i]));
}
for(int i=0;i<cc.length;i++){
msg.addRecipient(Message.RecipientType.CC, new
InternetAddress(cc[i]));
}
for(int i=0;i<bcc.length;i++){
msg.addRecipient(Message.RecipientType.BCC, new
InternetAddress(bcc[i]));
}
msg.saveChanges();
Transport transport = session.getTransport("smtps");
transport.connect("smtp.gmail.com","[email protected]", "******");
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
return true;
} catch (Exception mex){
mex.printStackTrace();
return false;
}
}
public static void main (String []args) throws Exception
{
SendMail.execute("dummy_report.html");
}
}
以下是StackTrace
java.io.FileNotFoundException: C:\reports (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at javax.activation.FileDataSource.getInputStream(Unknown Source)
at javax.activation.DataHandler.writeTo(Unknown Source)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:845)
at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:361)
at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:85)
at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source)
at javax.activation.DataHandler.writeTo(Unknown Source)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1683)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:585)
at sel.SendMail.sendMail(SendMail.java:143)
at sel.SendMail.execute(SendMail.java:22)
at sel.SendMail.main(SendMail.java:157)
javax.mail.MessagingException: IOException while sending message;
nested exception is:
java.io.FileNotFoundException: C:\reports (Access is denied)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:625)
at sel.SendMail.sendMail(SendMail.java:143)
at sel.SendMail.execute(SendMail.java:22)
at sel.SendMail.main(SendMail.java:157)
Caused by: java.io.FileNotFoundException: C:\reports (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at javax.activation.FileDataSource.getInputStream(Unknown Source)
at javax.activation.DataHandler.writeTo(Unknown Source)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:845)
at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:361)
at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:85)
at javax.activation.ObjectDataContentHandler.writeTo(Unknown Source)
at javax.activation.DataHandler.writeTo(Unknown Source)
at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1350)
at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1683)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:585)
... 3 more
最佳答案
FileDataSource仅指向目录,而不指向文件。看起来attachmentName应该在由attachmentPath命名的目录中命名文件。尝试以下方法:
File att = new File(new File(attachmentPath), attachmentName);
messageBodyPart.attachFile(att);
关于java - 使用JavaMail发送邮件时出现IO异常(访问被拒绝),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21889718/