[英]Specifying the filename while creating a File Object it from byte array (without creating a physical file)
I was stuck to a problem exactly same as this and with the solution posted i was able to solve my issue. 我陷入了一个与此完全相同的问题,并且在发布解决方案后,我能够解决我的问题。 But now the problem is, when the attachment is received, there is no name for it. 但是现在的问题是,当收到附件时,没有名称。 In my method, i have asked for receiver's email id, subject , content, filename and byte[] for the File. 在我的方法中,我要求提供文件的收件人的电子邮件ID,主题,内容,文件名和字节[]。 There is no problem in format of the file i am passing but problem is with the name. 我传递的文件格式没有问题,但名称有问题。 The recipient gets "noname" as file name. 收件人获取“ noname”作为文件名。 How do we specify the filename of our choice. 我们如何指定所选文件名。 The filename which i am passing as parameter doesnt gets reflected. 我作为参数传递的文件名没有得到反映。 Please suggest. 请提出建议。
The code which i am using is 我正在使用的代码是
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();
}
My email service Implementation for this goes here 我的电子邮件服务的实现在这里
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);
}}
Please someone help in figuring this out 请有人帮忙解决这个问题
From the Spring Documentation I can say that the inlined elements don't have special names besides the contentId. 从Spring文档中,我可以说内联元素除了contentId之外没有其他特殊名称。 Maybe you want to add an attachment instead using the addAttachment method? 也许您想使用addAttachment方法添加附件? Then you can have a name for your file. 然后,您可以为文件命名。
http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/mail/javamail/MimeMessageHelper.html http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/mail/javamail/MimeMessageHelper.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.