Reputation: 267
I am trying to send a mail using JavaMail but getting IO exception saying Access is denied Please let me know where i am making a mistake. I have used java mail and internet APIs along with the datasource , datahandler APIs. Also the html file i m trying to send as an attachment is in appropriate path and i have the access rights to that file. Below is my code
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");
}
}
Below is the 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
Upvotes: 1
Views: 11859
Reputation: 29971
The FileDataSource only points to the directory, not the file. It looks like attachmentName is supposed to name the file within the directory named by attachmentPath. Try this instead:
File att = new File(new File(attachmentPath), attachmentName);
messageBodyPart.attachFile(att);
Upvotes: 9