【发布时间】:2013-02-25 05:03:10
【问题描述】:
我遇到了与this 完全相同的问题 并发布了解决方案,我能够解决我的问题。 但是现在的问题是,当收到附件时,它没有名字。在我的方法中,我要求提供文件的接收者的电子邮件 ID、主题、内容、文件名和字节 []。我传递的文件格式没有问题,但问题在于名称。收件人将“noname”作为文件名。我们如何指定我们选择的文件名。我作为参数传递的文件名没有得到反映。请提出建议。
我使用的代码是
File file = new File("D:/my docs/Jetty.pdf");
int len1 = (int)(file.length());
FileInputStream fis1 = null;
try {
fis1 = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
byte buf1[] = new byte[len1];
try {
fis1.read(buf1);
EmailServiceClient.sendEmailWithAttachment("xyz@gmail.com", "abc@gmail.com", "Hi", "PFA", "Jetty.pdf", buf1);
System.out.println("SENT");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我的电子邮件服务实现在这里
public void sendEmailWithAttachment(String emailIdTo, String emailIdFrom, String subject, String content,
final String fileName, byte[] file) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(emailIdTo);
helper.setFrom(emailIdFrom);
helper.setSubject(subject);
helper.setText(content, true);
helper.addInline("attachment", new ByteArrayResource(file) {
@Override
public String getFilename() {
return fileName;
}
});
mailSender.send(message);
} catch (MessagingException e) {
throw new MailParseException(e);
}}
请有人帮忙解决这个问题
【问题讨论】:
标签: java file-io jakarta-mail