通过Java邮件将图像添加到电子邮件 [英] Adding image to email via Java mail

查看:237
本文介绍了通过Java邮件将图像添加到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

  MimeMessage mail = new MimeMessage(session); 
mail.setFrom(from);
MimeMultipart multipart = new MimeMultipart(related);
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(bodyText,text / html);

multipart.addBodyPart(htmlPart);

MimeBodyPart imgPart = new MimeBodyPart();
String path =/ivr/imagelogos/accenture.jpg;
DataSource ds = new FileDataSource(path);
imgPart.setDataHandler(new DataHandler(ds));
imgPart.setHeader(Content-ID,-img-1);
multipart.addBodyPart(imgPart);

mail.setContent(multipart);

mail.setSentDate(new Date());
mail.setHeader(X-Mailer,ALS通知程序构建1.0.0.10);

//发送消息
Transport.send(mail);

代码正在一个unix框上运行 - 图像路径基于unix文件路径。 p>

运行代码后,我收到此错误:

 发送消息时出现IOException 
javax.mail.MessagingException:IOException同时发送消息;
嵌套异常是:
java.io.FileNotFoundException:/ivr/imagelogos/accenture.jpg(没有这样的文件或目录)
在com.sun.mail.smtp.SMTPTransport.sendMessage( SMTPTransport.java:676)


解决方案

听起来像/ ivr /imagelogos/accenture.jpg文件不存在。你确定这是正确的道路吗?也许这应该是相对于其他一些路径?如果确实存在,运行Java应用程序的用户是否具有读取权限?


Here is my code:

MimeMessage mail = new MimeMessage(session);
mail.setFrom(from);
MimeMultipart multipart = new MimeMultipart("related");
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(bodyText, "text/html");

multipart.addBodyPart(htmlPart);

MimeBodyPart imgPart=new MimeBodyPart();
String path = "/ivr/imagelogos/accenture.jpg";
DataSource ds=new FileDataSource(path);
imgPart.setDataHandler(new DataHandler(ds));    
imgPart.setHeader("Content-ID","the-img-1");
multipart.addBodyPart(imgPart);

mail.setContent(multipart);

mail.setSentDate(new Date());
mail.setHeader("X-Mailer", "ALS Notifier Build 1.0.0.10");

// send the message
Transport.send(mail);

The code is being ran on a unix box - image path is based on unix file paths.

After running the code I receive this error:

IOException while sending message
javax.mail.MessagingException: IOException while sending message;
  nested exception is:
        java.io.FileNotFoundException: /ivr/imagelogos/accenture.jpg (No such file or directory)
        at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:676)

解决方案

Sounds like the /ivr/imagelogos/accenture.jpg file doesn't exist. Are you sure that's the right path? Maybe it's supposed to be relative to some other path? If it does exist, does the user running the Java app have read permissions on it?

这篇关于通过Java邮件将图像添加到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆