1
我被卡住的问题与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("[email protected]", "[email protected]", "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);
}}
请搞清楚了这一点
感谢您的回复@toomasr但我以前的问题是我刚才提到的链接。我的问题是在不创建物理文件的情况下在电子邮件中添加附件,而解决方案的确有窍门。但现在收到的电子邮件附件没有任何名称,我不知道如何解决这个问题。 – 2013-02-25 05:11:40
谢谢@toomasr,事情奏效! :) – 2013-02-25 05:32:49