在我的java Web应用程序中,我需要将图像添加到正文邮件内容,我将文本正文传递给一个类邮件到另一个类但我需要动态发送图像如何发送图像我的图像文件夹是{{1但我如何附加邮件正文
我的类我在这里生成消息数据表单数据库发送到另一个类
WishesDAO.java:这个类有3种基于这种方法的方法我需要将单个图像发送到邮件类
WebContent/Images/ABC.JPG
SendEmail.java:
public String method()
{
rs = stmnt.executeQuery(DOBQuery);
while(rs.next())//DOB
{
String From="ABC@hai.com";
String To=rs.getString("Emailid");
String Name=rs.getString("Ename");
String htmlText = "<img src=Images/ABC.JPG/>";
String text="Hi "+Name.toString()+","+"\n\n"+"Happy BirthDay....!"+"\n\n"+htmlText+"\n\nFrom,\nTeam";
SendEmail stp=new SendEmail();
stp.mail(From, To,Name,text);
}
}
如何发送任何人请发送图片
例外1:
javax.mail.MessagingException:发送消息时发生IOException;
嵌套异常是:java.io.FileNotFoundException: \ WebContent \ Images \ HappyBirthday.JPG(系统找不到路径 指定)javax.mail.MessagingException:发送时发生IOException 信息;嵌套异常是:java.io.FileNotFoundException: \ WebContent \ Images \ HappyBirthday.JPG(系统找不到路径 指定的) com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1167) 在javax.mail.Transport.send0(Transport.java:254)at javax.mail.Transport.send(Transport.java:124)at Model.SendEmail.mail(SendEmail.java:58)at Model.WishesDAO.method(WishesDAO.java:73)at Model.Testing.run(Testing.java:24)at java.util.TimerThread.mainLoop(未知来源)at java.util.TimerThread.run(Unknown Source)引起: java.io.FileNotFoundException:\ WebContent \ Images \ HappyBirthday.JPG (系统找不到指定的路径) java.io.FileInputStream.open(Native Method)at java.io.FileInputStream。(未知来源)at javax.activation.FileDataSource.getInputStream(未知来源)at javax.activation.DataHandler.writeTo(未知来源)at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1574)at at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:948)at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:538)at at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:103) 在javax.activation.ObjectDataContentHandler.writeTo(未知来源) 在javax.activation.DataHandler.writeTo(未知来源)at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1574)at at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1840)at at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1119) ......还有7个
我的形象:
image path 谢谢
答案 0 :(得分:0)
欢迎来到SO。
要在电子邮件中使用图片,您需要使用BodyPart
和DataSource
这样的内容,
首先添加HTML
// first part (the html)
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<H1>Hello</H1><img src=\"cid:image\">";
messageBodyPart.setContent(htmlText, "text/html");
// add it
multipart.addBodyPart(messageBodyPart);
然后通过创建Datahandler来添加图像,如下所示:
// second part (the image)
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource(
"/home/manisha/javamail-mini-logo.png");
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID", "<image>");
message.setContent(multipart);
你可以参考Tutorials point example或者去推荐任何其他教程,只需谷歌就可以获得更多。
我希望这会有所帮助。