如何使用java从另一个类调用文件对象并通过电子邮件将其作为附件发送?

如何使用java从另一个类调用文件对象并通过电子邮件将其作为附件发送?,java,jakarta-mail,Java,Jakarta Mail,我有一个classA,其中我有文件对象和另一个classB来发送带有附件的电子邮件。 我尝试做的是,从classB中的classA获取文件对象,并通过电子邮件将其作为附件发送 当我执行代码时,它将文件作为附件发送,但在电子邮件中,文件没有名称 A类: import java.io.File; import javax.mail.MessagingException; public class A{ public void A(){ System.out.println("

我有一个classA,其中我有文件对象和另一个classB来发送带有附件的电子邮件。 我尝试做的是,从classB中的classA获取文件对象,并通过电子邮件将其作为附件发送

当我执行代码时,它将文件作为附件发送,但在电子邮件中,文件没有名称

A类:

import java.io.File;
import javax.mail.MessagingException;

public class A{
    public void A(){
       System.out.println("Sending the file...");
       File file = new File("c:\\temp\\FileA.txt");
    }   
}
B类:

public class B {
    public static void B(File file) throws MessagingException {
        String host = "smtp.gmail.com";
        String Password = "***";
        String from = "***@gmail.com";
        String toAddress = "***@gmail.com";

        //Here i don t want to use this file
        //String filename = "C:/file.txt";

        // Get system properties
        Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtps.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getInstance(props, null);

        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, toAddress);
        message.setSubject("Attachment TEST ");

        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText("coucou the file is here");

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);

        messageBodyPart = new MimeBodyPart();

        DataSource source = new FileDataSource(file);

        messageBodyPart.setDataHandler(new DataHandler(source));
        //messageBodyPart.setFileName(file);

        multipart.addBodyPart(messageBodyPart);

        message.setContent(multipart);

        try {
            Transport tr = session.getTransport("smtps");
            tr.connect(host, from, Password);
            tr.sendMessage(message, message.getAllRecipients());
            System.out.println("Mail Sent Successfully");
            tr.close();
        } catch (SendFailedException sfe) {
            System.out.println(sfe);
        }
    }
    public static void main(String args[]) throws MessagingException{
        B file = new B();
    }
}

我应该怎么做才能在电子邮件中获得确切的文件名?谢谢你

当你定义了一个在B类中有类名的方法时,我不理解你的代码是如何运行的。构造函数没有返回类型

代码的问题:-

1文件变量是a类构造函数中的局部变量。它应声明为实例字段

import java.io.File;
import javax.mail.MessagingException;

public class A{

    File file;
    public void A() throws IOException{

     System.out.println("Sending the file...");
     file = new File("c:\\temp\\FileA.txt");
    }   
}
2您定义了一个方法,该方法的名称为类B中的类。构造函数没有返回类型

3设置附件的名称已被注释掉。请取消对其注释,并传递字符串而不是文件对象

messageBodyPart.setFileName(file.getName());
BI类的最终代码尚未在IDE中编译:

public class B {

    public B(A aa) throws MessagingException {

        File file = aa.file;

        String host = "smtp.gmail.com";
        String Password = "***";
        String from = "***@gmail.com";
        String toAddress = "***@gmail.com";

        //Here i don t want to use this file
        //String filename = "C:/file.txt";

        // Get system properties
        Properties props = System.getProperties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtps.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getInstance(props, null);

        MimeMessage message = new MimeMessage(session);

        message.setFrom(new InternetAddress(from));

        message.setRecipients(Message.RecipientType.TO, toAddress);

        message.setSubject("Attachment TEST ");

        BodyPart messageBodyPart = new MimeBodyPart();

        messageBodyPart.setText("coucou the file is here");

        Multipart multipart = new MimeMultipart();

        multipart.addBodyPart(messageBodyPart);

        messageBodyPart = new MimeBodyPart();

        DataSource source = new FileDataSource(file);

        messageBodyPart.setDataHandler(new DataHandler(source));

        messageBodyPart.setFileName(file.getName());

        multipart.addBodyPart(messageBodyPart);

        message.setContent(multipart);

        try {
            Transport tr = session.getTransport("smtps");
            tr.connect(host, from, Password);
            tr.sendMessage(message, message.getAllRecipients());
            System.out.println("Mail Sent Successfully");
            tr.close();

        } catch (SendFailedException sfe) {

            System.out.println(sfe);
        }
    }
    public static void main(String args[]) throws MessagingException{

        A aaa = new A();
        B file = new B(aaa);
    }
}
更改此行:

//messageBodyPart.setFileNamefile

为此:

messageBodyPart.setFileName(file.getName());
我也不完全理解你的代码结构


希望它能帮助您

首先,使用MimeBodyPart.attachFile方法,它更简单:

MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("coucou the file is here");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
messageBodyPart.attachFile(file);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
第二,将您的主要方法放在:

public class A {
    public static void main(String[] args) throws Exception {
         // whatever
    }
    // other methods, if needed...
}

file.getName有什么问题?顺便说一句,另一个问题的选择答案是更好的方法,谢谢你,库马尔。我在A班,我不知道主要方法放在哪里。谢谢你,库马尔。我在班上不知道把主要方法放在哪里