我在用javamail发送html的时候,发现对于img格式的图片没办法正常发送,可能是路径问题
如果我的img用的是http://xxx.com/img/XXX这样查找就可以正常显示,但是用相对路径就出错。
有没有什么办法解决?
还有,是不是对于css javamail是没办法正常发送的?还有,我想发送jsp转化成的html,也就是说,我的html是动态生成的,里面有些数据是要链接数据库的,如果直接把这个文件发送出去的话没办法编译,请问大家是怎么做的?

解决方案 »

  1.   

    参考下面的例子:import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;import java.util.Properties;class SimpleMail2 {
        public static void main(String[] args) throws Exception{
            System.out.println("Sending mail...");
            Properties props = new Properties();
            props.setProperty("mail.transport.protocol", "smtp");
            props.setProperty("mail.host", "smtp.mymailserver.com");
            props.setProperty("mail.user", "myuser");
            props.setProperty("mail.password", "mypwd");        Session mailSession = Session.getDefaultInstance(props, null);
            mailSession.setDebug(true);
            Transport transport = mailSession.getTransport();        MimeMessage message = new MimeMessage(mailSession);
            message.setSubject("HTML  mail with images");
            message.setFrom(new InternetAddress("[email protected]"));
            message.addRecipient(Message.RecipientType.TO,
                 new InternetAddress("[email protected]"));        //
            // This HTML mail have to 2 part, the BODY and the embedded image
            //
            MimeMultipart multipart = new MimeMultipart("related");        // 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);
            
            // second part (the image)
            messageBodyPart = new MimeBodyPart();
            DataSource fds = new FileDataSource
              ("C:\\images\\jht.gif");
            messageBodyPart.setDataHandler(new DataHandler(fds));
            messageBodyPart.setHeader("Content-ID","<image>");        // add it
            multipart.addBodyPart(messageBodyPart);        // put everything together
            message.setContent(multipart);        transport.connect();
            transport.sendMessage(message,
                message.getRecipients(Message.RecipientType.TO));
            transport.close();
            }
    }
      

  2.   

    发送Email时候必须私用绝对的url,如果使用相对路径,到别人邮箱后就会以别人邮箱的URL为基础地址,再加上对应的相对路径,这样图片不存在当然就显示不了了!
    jsp生成静态页面,所有的内容都变成绝对URL,就可以发送了,页面中不能使用连接数据库这些代码,那样是肯定打不开的,只有生成了静态页面才可以显示全部数据!
    关于生成静态页面,有很多种办法,最常用的是使用模板引擎,也可以使用JSP来生成!给一个参考地址
    http://dxwang.javaeye.com/blog/114937