0

工作环境:windows 7、fedora 20、Java 7_79、maven、eclipse

编辑

我有一个带有 maven 的 web java 项目,我正在尝试获取我在 src/main/resouces 包中的图像以将图像放在电子邮件中,此图像稍后被设置到文件夹 WEB-INF/classes 的战争中/html。在 Windows 7 上运行项目时,一切正常,但问题是当我在 Fedora 20(Linux)上运行我的 Web 项目时,出现下一个错误:

> 21:26:09,662 ERROR [stderr] (Thread-92) java.lang.NullPointerException
> 21:26:09,662 ERROR [stderr] (Thread-92)   at
> javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:226)
> 21:26:09,663 ERROR [stderr] (Thread-92)   at
> javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:299)
> 21:26:09,663 ERROR [stderr] (Thread-92)   at
> javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1375)
> 21:26:09,663 ERROR [stderr] (Thread-92)   at
> javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1021)
> 21:26:09,664 ERROR [stderr] (Thread-92)   at
> javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:419)
> 21:26:09,664 ERROR [stderr] (Thread-92)   at
> javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1354)
> 21:26:09,664 ERROR [stderr] (Thread-92)   at
> javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2107)
> 21:26:09,665 ERROR [stderr] (Thread-92)   at
> javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2075)
> 21:26:09,665 ERROR [stderr] (Thread-92)   at
> javax.mail.Transport.send(Transport.java:123) 21:26:09,666 ERROR
> [stderr] (Thread-92)  at
> com.acertum.serc.util.SolicitudEmail.enviarCorreo(SolicitudEmail.java:148)
> 21:26:09,666 ERROR [stderr] (Thread-92)   at
> com.acertum.serc.controller.AgregarExcepcionView$1.run(AgregarExcepcionView.java:383)

这是获取图像路径的方法

public MimeBodyPart obtenerImagen(String urlImagen) throws IOException, MessagingException{
    logger.info("entro");
    URL url = getClass().getClassLoader().getResource(urlImagen);
    logger.info("url" + url);
    MimeBodyPart attachmentPart = new MimeBodyPart();
    logger.info("url.getPath()" + url.getPath().toString());
    attachmentPart.attachFile(url.getPath());
    attachmentPart.setDisposition(MimeBodyPart.INLINE);
    return attachmentPart;
}

我发送电子邮件的主要方法是下一个:

try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(ScoreConstants.getFromSolicitudEmail()));
            message.setRecipients(Message.RecipientType.TO, recipientes);
            message.setSubject(ScoreConstants.getSubjectSolicitudEmail());
            String fechaSistema="<html><body><br/><br/><br/><div align='right'><h3>M&eacute;xico,D.F a "+date+"</h3></div><br/>";
            String  nombreAcreditado="<br/><br/><div style='font-family: Calibri;'><strong>Acreditado: </strong> "+ acreditado +"</div><br/>";
            String  tipoproducto="<div style='font-family: Calibri;'><strong>Tipo de Producto: </strong>"+ subproducto +"</div><br/>";
            String  numeroMonto="<div style='font-family: Calibri;'><strong>Monto: </strong>"+ monto +"</div><br/>";
            String  descripcionExcepcion="<div style='font-family: Calibri;'><strong>Descripcion de la excepci&oacute;n: </strong>"+ descripcion +"</div><br/>";



            String linkAceptada = "<br/><br/><br/><br/><br/><br/><br/><br/><div style='margin-left: 350px;font-family: Calibri;'><a href='http://"+ ScoreConstants.getIpServidorWeb()+":"+ScoreConstants.getPuertoServidorWeb()+"/ScoreExcepciones/AceptadoRechazado.xhtml?nombre="
                    + acreditado
                    + "&celular="
                    + celular
                    + "&fase="
                    + fase
                    + "&excepcionId="
                    + excepcionId
                    + "&subproductoId="
                    + subproductoId
                    + "&version="
                    + version
                    + "&empleadoId="
                    + empleadoId
                    + "&ipAddres="
                    + ipAddress
                    + "&accion="+autorizar+"'>Autorizar</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                    + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='http://"+ScoreConstants.getIpServidorWeb()+":"+ScoreConstants.getPuertoServidorWeb()+"/ScoreExcepciones/AceptadoRechazado.xhtml?nombre="
                    + acreditado
                    + "&celular="
                    + celular
                    + "&fase="
                    + fase
                    + "&excepcionId="
                    + excepcionId
                    + "&subproductoId="
                    + subproductoId
                    + "&version="
                    + version
                    + "&empleadoId="
                    + empleadoId
                    + "&ipAddres="
                    + ipAddress
                    + "&accion="
                    + rechazar+ "'>Rechazar</a></div>";
            String cadena = String.format("%s%s%s%s%s%s%s", fechaSistema,
                    obtenerRutaContenido(File.separator + "html" + File.separator + "Correo.html"),
                    nombreAcreditado, tipoproducto, numeroMonto,
                    descripcionExcepcion, linkAceptada);






            MimeBodyPart cadenaContenido = new MimeBodyPart();
            cadenaContenido.setContent(cadena, "text/html");
            String rutaPiePagina=obtenerRutaContenido((File.separator  +"html" + File.separator + "PiePagina.html"));


            MimeBodyPart piePagina = new MimeBodyPart();
            piePagina.setContent(rutaPiePagina, "text/html");


            MimeBodyPart imagenCreada=obtenerImagen(("html" + File.separator + "Logo-AcertumBank-S.jpg"));

            message.setContent(ordenContenidos(imagenCreada,cadenaContenido,piePagina));

            Transport.send(message);
        } catch (AddressException e) {
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);
        } catch (MessagingException e) {
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);
        }
4

2 回答 2

0
obtenerRutaContenido(File.separator + "html" + File.separator + "Correo.html"),
...
obtenerRutaContenido((File.separator  +"html" + File.separator + "PiePagina.html"));
...
obtenerImagen(("html" + File.separator + "Logo-AcertumBank-S.jpg"));

如果obtenerRutaContenido()obtenerImagen()相似,您也需要从路径开始File.separator

于 2015-08-14T16:32:31.123 回答
0

主要你没有一个文件(在文件系统上),而是一个资源(在一个 jar/war/class 路径/...中)。

public MimeBodyPart obtenerImagen(String urlImagen) throws IOException, MessagingException {
    logger.info("entro");
    URL url = getClass().getClassLoader().getResource(urlImagen);
    MimeBodyPart attachmentPart = new MimeBodyPart(url.openStream());
    attachmentPart.setFileName(url.getFile()); // Just "xyz.jpg" maybe
    attachmentPart.setDisposition(MimeBodyPart.INLINE);
    //attachmentPart.setContentType("image/jpeg");
    return attachmentPart;
}

这会将资源的 InputStream 带到 MimeBodyPart 构造函数以填充它的内容。

于 2015-08-14T16:41:08.637 回答