gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:36:17 25 4
gpt4 key购买 nike

我有一个 A 类,其中有文件对象和另一个 B 类,用于发送带有附件的电子邮件。我尝试做的是,从 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)file变量是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)设置附件名称已被注释掉。取消注释并传递一个 String 而不是 File Object 。

messageBodyPart.setFileName(file.getName());

B 类的最终代码(不过我还没有在 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);
}
}

关于java - 如何使用java从另一个类调用文件对象并通过电子邮件将其作为附件发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25530029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com