使用JavaMail发送邮件时出现IO异常(访问被拒绝)

时间:2014-02-19 18:54:50

标签: java javamail

我正在尝试使用JavaMail发送邮件但是 IO异常表示访问被拒绝    请让我知道我在哪里犯了错误。我使用了java邮件和Internet API以及数据源,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={"xyz@gmail.com"};
    String[] cc={"xyz@gmail.com"};
    String[] bcc={"abc@gmail.com"};

    SendMail.sendMail("xyz@gmail.com",
                        "******",
                        "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","xyz@gmail.com", "******");

        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

1 个答案:

答案 0 :(得分:8)

FileDataSource仅指向目录,而不是文件。看起来attachmentName应该在attachmentPath命名的目录中命名文件。试试这个:

File att = new File(new File(attachmentPath), attachmentName);
messageBodyPart.attachFile(att);