Hello

Im trying to send emails thru Java and they have HTML plus embedded images. Everything shows up except the embedded images. What could be wrong?

My email is a entire String with HTML code.

Thank you

Recommended Answers

All 11 Replies

you expect us to debug your code without seeing your code?

you expect us to debug your code without seeing your code?

I had two pages open. One was this and another one was one more elaborated (which I closed by accident). Sorry about that.

Give me a second...

 public void EnviarCorreo(String CorreoOrigen, String CorreoDestino, String usuario, String contrasena, String asunto, String mensaje, String NombreArchivo, String TipoPastel, String Cantidad,String Calle,String Fecha, String link,String pricolor,String seccolor,String textcolor,String nombrebmp,String nombre_comprador,String precio_producto,String cantidad_producto)
    {
        try
        {



            // Propiedades de la conexión
            Properties props = new Properties();
            String servidorsmtp="smtp.server.com";
            props.setProperty("mail.smtp.host", servidorsmtp);
            props.setProperty("mail.smtp.starttls.enable", "true");
            props.setProperty("mail.smtp.port", "25");
            props.setProperty("mail.smtp.user", usuario);
            props.setProperty("mail.smtp.auth", "true");


            // Preparamos la sesion
            Session session = Session.getDefaultInstance(props);

            // Construimos el mensaje
            MimeMessage message = new MimeMessage(session);
            message.setHeader("Content-Type", "multipart/mixed");

            message.setFrom(new InternetAddress(CorreoOrigen));
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(CorreoDestino)); 


            Multipart mp = new MimeMultipart(); 
            BodyPart texto=new MimeBodyPart();
            texto.setHeader("Content-Type", "text/html; charset=utf-8");


            MimeBodyPart ajunto=new MimeBodyPart();
            ajunto.setHeader("Content-Type", "multipart/mixed");

            if (NombreArchivo.endsWith(".pdf")) //Para un cliente
            {
                String imagen=ImageToBase64(nombrebmp+".bmp");
                message.setSubject("Felicidades! Has completado tu compra de un "+TipoPastel+" Tu numero de pedido es: " + asunto);


                String singularopluralcanti="";
                if ((Integer.valueOf(Integer.parseInt(Cantidad))>1))
                {
                    singularopluralcanti = "Has pedido " + Cantidad + " unidades de este producto.";
                }
                if ((Integer.valueOf(Integer.parseInt(Cantidad))==1))
                {
                    singularopluralcanti = "Has pedido " + Cantidad + " unidad de este producto.";
                }
                else
                {
                    singularopluralcanti = "Has pedido " + Cantidad + " unidades de este producto.";
                }


                if (link==null || link.length()<1 || link.isEmpty())
                {
                    link="http://somepage.com/correo/default.png";
                }

                if (pricolor==null || pricolor.length()<1 || pricolor.isEmpty())
                {
                    pricolor="10B0CA";
                }

                if (seccolor==null || seccolor.length()<1 || seccolor.isEmpty())
                {
                    seccolor="35A4B5";
                }


                if (imagen==null || imagen.length()<1 || imagen.isEmpty())
                {
                    imagen="Si";
                }
                else
                {
                    imagen="<img src='data:image/png;base64,"+imagen+"'  />";
                }





                String htmlcontenido="<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>"+
"<html xmlns='http://www.w3.org/1999/xhtml'>"+
"<head>"+
"<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />"+
"<title>Pedido "+asunto+"</title>"+
"</head>"+

"<body>" +
                        "" +
                        "" +
                        "" +
                        "<style type='text/css'>"+
"body,td { color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; }"+
"</style>"+
"<body style='background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;'>"+
"<div style='background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;'>"+
"<table cellspacing='0' cellpadding='0' border='0' width='100%'>"+
"<tr>"+
    "<td align='center' valign='top' style='padding:20px 0 20px 0'>"+
      " <!-- [ header starts here] -->"+
        "<table bgcolor='#FFFFFF' cellspacing='0' cellpadding='10' border='0' width='650' style='border:1px solid #E0E0E0;'>"+
          "<tr>"+
           "<td valign='top'><img src='"+link+"'  style='margin-bottom:10px;' height='160px' border='0'/></td>"+
           "</tr>"+
      "<!-- [ middle starts here] -->"+
            "<tr>"+
                "<td valign='top'>"+
                    "<h1 style='font-size:22px; font-weight:normal; line-height:22px; margin:0 0 11px 0;'>Hola, "+nombre_comprador+"</h1>"+
                    "<p style='font-size:12px; line-height:16px; margin:0 0 10px 0;'>"+
                        "Gracias por tu pedido."+     
                    "</p>"+
                    "<p style='font-size:12px; line-height:16px; margin:0;'></br></br><b><font size='+2' color='#FF0000'>Por favor imprime esta hoja como comprobacion de su compra y presentalo a la pasteleria</font></b></br></br></font></b></br></br> Tu confirmacion de pedido esta a continuacion. Gracias de nuevo por comprar con nosotros.</p>"+
                "</td>"+
            "</tr>"+
            "<tr>"+
                "<td>"+
                    "<h2 style='font-size:18px; font-weight:normal; margin:0;'>Tu pedido es #"+asunto+"</h2>"+
                "</td>"+
            "</tr>"+
            "<tr>"+
                "<td>"+
                    "<table cellspacing='0' cellpadding='0' border='0' width='650'>"+
                        "<thead>"+
                        "<tr>"+
                            "<th align='left' width='325' bgcolor='#EAEAEA' style='font-size:13px; padding:5px 9px 6px 9px; line-height:1em;'>Nombre comprador:</th>"+
                            "<th width='10'></th>"+
                            "<th align='left' width='325' bgcolor='#EAEAEA' style='font-size:13px; padding:5px 9px 6px 9px; line-height:1em;'>Pagado:</th>"+
                        "</tr>"+
                        "</thead>"+
                        "<tbody>"+
                        "<tr>"+
                            "<td valign='top' style='font-size:12px; padding:7px 9px 9px 9px; border-left:1px solid #EAEAEA; border-bottom:1px solid #EAEAEA; border-right:1px solid #EAEAEA;'>"+
                                ""+nombre_comprador+"<br/>"+



                            "</td>"+
                            "<td>&nbsp;</td>"+
                            "<td valign='top' style='font-size:12px; padding:7px 9px 9px 9px; border-left:1px solid #EAEAEA; border-bottom:1px solid #EAEAEA; border-right:1px solid #EAEAEA;'>"+
                                "<p>Si</p>"+


                            "</td>"+
                        "</tr>"+
                        "</tbody>"+
                    "</table>"+
                    "<br/>"+

                    "<table cellspacing='0' cellpadding='0' border='0' width='100%'>"+
                        "<thead>"+
                        "<tr>"+
                            "<th align='left' width='325' bgcolor='#EAEAEA' style='font-size:13px; padding:5px 9px 6px 9px; line-height:1em;'>Punto de recogida:</th>"+
                            "<th width='10'></th>"+
                            "<th align='left' width='325' bgcolor='#EAEAEA' style='font-size:13px; padding:5px 9px 6px 9px; line-height:1em;'>Fecha de recogida:</th>"+
                        "</tr>"+
                        "</thead>"+
                        "<tbody>"+
                        "<tr>"+
                            "<td valign='top' style='font-size:12px; padding:7px 9px 9px 9px; border-left:1px solid #EAEAEA; border-bottom:1px solid #EAEAEA; border-right:1px solid #EAEAEA;'>"+
                            Calle+""+

                                "&nbsp;"+
                            "</td>"+
                            "<td>&nbsp;</td>"+
                            "<td valign='top' style='font-size:12px; padding:7px 9px 9px 9px; border-left:1px solid #EAEAEA; border-bottom:1px solid #EAEAEA; border-right:1px solid #EAEAEA;'>"+
                                Fecha+""+
                                "&nbsp;"+
                            "</td>"+
                        "</tr>"+
                        "</tbody>"+
                    "</table>"+
                    "<br/>"+

                    "<table cellspacing='0' cellpadding='0' border='0' width='650' style='border:1px solid #EAEAEA;'>"+
    "<thead>"+
        "<tr>"+
            "<th align='left' bgcolor='#EAEAEA' style='font-size:13px; padding:3px 9px'>Articulo</th>"+
            "<th align='left' bgcolor='#EAEAEA' style='font-size:13px; padding:3px 9px'></th>"+
            "<th align='center' bgcolor='#EAEAEA' style='font-size:13px; padding:3px 9px'>Cda</th>"+
            "<th align='right' bgcolor='#EAEAEA' style='font-size:13px; padding:3px 9px'>Precio</th>"+
        "</tr>"+
    "</thead>"+

            "<tbody bgcolor='#F6F6F6'>"+
        "<tr>"+
    "<td align='left' valign='top' style='font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;'>"+
        "<strong style='font-size:11px;'>Opera</strong>"+
                "<dl style='margin:0; padding:0;'>"+
                        "<dt><strong><em>Corte</em></strong></dt>"+
            "<dd style='margin:0; padding:0 0 0 9px;'>"+
                                    imagen+"                            </dd>"+
                    "</dl>"+
                                                        "</td>"+
    "<td align='left' valign='top' style='font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;'></td>"+
    "<td align='center' valign='top' style='font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;'>"+cantidad_producto+"</td>"+
    "<td align='right' valign='top' style='font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;'>"+
                                                "<span class='price'>"+precio_producto+"</span>"+            



            "</td>"+
"</tr>"+
    "</tbody>"+

    "<tbody>"+

            "<tr class='grand_total'>"+
        "<td colspan='3' align='right' style='padding:3px 9px'>"+
                        "<strong>Suma Total</strong>"+
                    "</td>"+
        "<td align='right' style='padding:3px 9px'>"+
                        "<strong><span class='price'>"+precio_producto+"</span></strong>"+
                    "</td>"+
    "</tr>"+
        "</tbody>"+
"</table>"+

                    "<p style='font-size:12px; margin:0 10px 10px 0'></p>"+
                "</td>"+
            "</tr>"+
            "<tr>"+
                "<td bgcolor='#EAEAEA' align='center' style='background:#EAEAEA; text-align:center;'><center><p style='font-size:12px; margin:0;'>De nuevo, <strong>muchas gracias</strong></p></center></td>"+
            "</tr>"+
        "</table>"+
    "</td>"+
"</tr>"+
"</table>"+
"</div>"+
"</body>"+
"</html>";




                 texto.setContent(htmlcontenido,"text/html; charset=utf-8");
                mp.addBodyPart(texto);
                ajunto.setContent(NombreArchivo,"application/pdf");
            }    
            ajunto.attachFile(NombreArchivo);
            mp.addBodyPart(ajunto);

            message.setContent(mp);

            // Lo enviamos.
            Transport t = session.getTransport("smtp");
            t.connect(usuario,contrasena);
            t.sendMessage(message, message.getAllRecipients());

            // Cierre.
            t.close();
        }
        catch (Exception e)
        {
            System.out.println("ERROR: No se puede conectar al servidor SMTP. No se ha podido enviar este correo (Informacion adicional: " + e.getLocalizedMessage() + " )");
        }
    }

Again, sorry about that.

and which is the image that shoud be shown? the bmp file? why are you using bmp anyway? it's not really a good choice for over the web, since of the filesizes and the loading times that can go with it.

and which is the image that shoud be shown? the bmp file? why are you using bmp anyway? it's not really a good choice for over the web, since of the filesizes and the loading times that can go with it.

Im trying to show both the BMP and the base64 representation of another image. Both do not show.

if you print the values of your image, what do you get? the path where it is loaded on your local machine? you're not really sending an image, if I'm not mistaken, but that path, and since the server on which you read the mail doesn't have that image on that path ...

if you print the values of your image, what do you get? the path where it is loaded on your local machine? you're not really sending an image, if I'm not mistaken, but that path, and since the server on which you read the mail doesn't have that image on that path ...

I get the URL of the image.

Im using Outlook; In 2013, there is a option that says "View In Web Browser". If I click that, it opens IE and it works perfectly. So, even though I know the links are correct, this shows that it is not a incorrect URL issue.

Anyone have any tips on this?

Any ideas? Maybe wrong headers or something ?

I don't know how to do this but I can help you out with the where to look question. You need to look into the concept of CID's also known as content ids. Basically, you create a multipart message and assign a content ID to the image attachment/data which will be used as the src for the image.

http://stackoverflow.com/questions/4312687/how-to-embed-images-in-email
http://stackoverflow.com/questions/5260654/embedding-images-into-html-email-with-java-mail
http://stackoverflow.com/questions/2996514/inline-images-in-email-using-javamail

I don't know how to do this but I can help you out with the where to look question. You need to look into the concept of CID's also known as content ids. Basically, you create a multipart message and assign a content ID to the image attachment/data which will be used as the src for the image.

http://stackoverflow.com/questions/4312687/how-to-embed-images-in-email
http://stackoverflow.com/questions/5260654/embedding-images-into-html-email-with-java-mail
http://stackoverflow.com/questions/2996514/inline-images-in-email-using-javamail

Ive read these article but I dont understand too well how to implement it.

As you see I only have 2 images: How can I assign this CID to them and use it in the code? Its kinda confusing to me personally.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.