已损坏的图片代码

时间:2016-05-30 13:46:34

标签: html image gmail

我必须使用javax发送电子邮件。消息正文是html。它有一个图像标记,我的问题是应该是src属性中的值,以便在另一个网络中打开邮件时图像可用。图像放在服务器内的一个名为images的文件夹中。应用程序托管使用apache tomcat 7。

这是充当邮件正文的html字符串。

    String emailBody = "<table width=\"99%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"+
            "<tr>"+
            "<td align=\"center\" valign=\"top\" style = \"background-color:#ED1B24;\">"+
            "<img src =\""+
            "http://myip:8080/Survey_services" + "/images/mrflogo.jpg" + "\">" + 
            "</td>" +
            "</tr>"+
            "<tr>"+
            "<td align=\"left\">"+
            "<p>Hi "+firstname+"," + "</p></b>"+
            "<p>A user account has been created for you, the user name will be your employee id</p></b>"+
            "<p>Your Password: "+password+"</p></b>"+
            "<p>Kindly change password at your convenience</p></b></b>"+
            "<p>Regards,</p></b>"+
            "<p>Admin Team</p>"+
            "</td>"+
            "</tr>"+
            "</table>"

我已经尝试了http://myip:8080/Survey_serviceshttp://localhost:8080/Survey_services两种情况,无论是哪种情况,在其他计算机上打开图像时都没有加载邮件,也无法从Gmail中加载.Html的恢复正常。

FYI myip是托管项目的机器的ipV4地址。

修改

根据@Bill Shannon建议我尝试了以下内容。

String emailBody = "<table width=\"99%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"+
            "<tr>"+
            "<td align=\"center\" valign=\"top\" style = \"background-color:#ED1B24;\">"+
            "<img src =\""+
            "cid:image" + "\">" + 
            "</td>" +
            "</tr>"+
            "<tr>"+
            "<td align=\"left\">"+
            "<p>Hi "+firstname+"," + "</p></b>"+
            "<p>A user account has been created for you, the user name will be your employee id</p></b>"+
            "<p>Your Password: "+password+"</p></b>"+
            "<p>Kindly change password at your convenience</p></b></b>"+
            "<p>Regards,</p></b>"+
            "<p>Admin Team</p>"+
            "</td>"+
            "</tr>"+
            "</table>";

 mailSession = Session.getDefaultInstance(emailProperties, null);
        emailMessage = new MimeMessage(mailSession);
        emailMessage.setFrom(new InternetAddress(data[0]));     
        emailMessage.addRecipient(Message.RecipientType.TO,
                new InternetAddress(mail));             
        emailMessage.setSubject("Account created");                    
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(emailBody, "text/html; charset=utf-8");
        Multipart multipart = new MimeMultipart();      
        multipart.addBodyPart(messageBodyPart);     
        messageBodyPart = new MimeBodyPart();

        DataSource fds = new FileDataSource(
             "path");

    messageBodyPart.setDataHandler(new DataHandler(fds));
    messageBodyPart.setHeader("Content-ID", "<image>");
    multipart.addBodyPart(messageBodyPart);
    emailMessage.setContent(multipart);

我无法弄清楚我应该提供什么价值,我尝试使用request.getServletContext()。getRealPath(&#34; /&#34;)来提供服务器中文件的路径,其中request是请求servlet的对象,并在结尾添加文件夹和文件名。当我这样做时,会抛出异常。

这是堆栈跟踪。

  

javax.mail.MessagingException:发送消息时发生IOException;     嵌套异常是:       java.io.FileNotFoundException:D:\ workspace.metadata.plugins \ org.eclipse.wst.server.core \ tmp0 \ wtpwebapps \ Survey_services(访问被拒绝)       at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1290)       在email.GmailClass.send(GmailClass.java:227)       在servlets.CreateUser.doPost(CreateUser.java:168)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:650)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:731)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)       在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)       在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436)       在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)       at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:625)       在org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.doRun(AprEndpoint.java:2517)       在org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.run(AprEndpoint.java:2506)       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)       at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)       在java.lang.Thread.run(Thread.java:745)   引起:java.io.FileNotFoundException:D:\ workspace.metadata.plugins \ org.eclipse.wst.server.core \ tmp0 \ wtpwebapps \ Survey_services(访问被拒绝)       at java.io.FileInputStream.open0(Native Method)       在java.io.FileInputStream.open(FileInputStream.java:195)       在java.io.FileInputStream。(FileInputStream.java:138)       在javax.activation.FileDataSource.getInputStream(FileDataSource.java:97)       在javax.activation.DataHandler.writeTo(DataHandler.java:305)       在javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1645)       在javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:961)       在javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:553)       在com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:81)       在javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:889)       在javax.activation.DataHandler.writeTo(DataHandler.java:317)       在javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1645)       在javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1850)       at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1241)       ......还有25个

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

显然,URL必须正确指向服务器上的图像,您可以通过在另一个网络上的浏览器中打开URL来测试该图像。即使这样,许多邮件阅读器默认也不会在远程服务器上打开图像。唯一的解决方法是include the image in the message itself by creating a multipart/related message