我发送一封电子邮件与html从我的方法与java邮件,只有在gmail它到达破碎,没有一些风格,这是什么?

6qftjkof  于 7个月前  发布在  Java
关注(0)|答案(1)|浏览(57)

这是我的主要方法,第二个是我调用它的地方。我的html格式似乎是正确的方式,样式是内联的,当我将其发送给outlook用户或当我手动将html放在gmail中的以下div中时,它可以工作:

<div id=":vn" class="Am Al editable LW-avf tS-tW" hidefocus="true" aria-label="Message Body" g_editable="true" role="textbox" aria-multiline="true" contenteditable="true" tabindex="1" style="direction: ltr; min-height: 266px;" spellcheck="false" aria-owns=":y1" aria-controls=":y1" aria-expanded="false"></div>

字符串
主要方法:

public void enviarEmail(String destinatario, String asunto, String contenidoHtml, String contentType) throws MessagingException {
    // Configuración de las propiedades
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.hostinger.com");
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.starttls.required", "true");

    // Crear una nueva sesión con autenticación
    Session session = Session.getInstance(props, new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });

    // Crear el mensaje
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress(username));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destinatario));
    message.setSubject(asunto);
    // Especificar UTF-8 en el contenido
    message.setContent(contenidoHtml, contentType);

    // Enviar el mensaje
    Transport.send(message);
}


然后我在这个方法中调用它:

public void enviarTicketsPorEmail(String correoDestinatario, Carrito carrito) throws MessagingException {
    StringBuilder cuerpoEmail = new StringBuilder();

    // .newsletter-recibo-de-tickets
    cuerpoEmail.append("<!DOCTYPE html>")
            .append("<html>")
            .append("<head>")
            .append("<meta charset=\"UTF-8\">")
            .append("<meta http-equiv=\"X-UA-Compatible\" content=\"text/html; IE=edge\">")
            .append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">")
            .append("<link rel=\"stylesheet\" href=\"https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700;900&display=swap\" />")
            .append("</head>")

            //body
            .append("<div style=\"margin:0;line-height:normal; display:flex; flex-direction: column; width:100%;justify-content: center;align-items: center;align-content: center;flex-wrap: nowrap;\">")

            .append("<div style=\"width: 100%;\">")
            .append("<img src=\"https://simplepassbucket.s3.sa-east-1.amazonaws.com/img/newsletters/parteInicialCompraExitosa.png\" style=\"width: 100%;\">")
            .append("</div>")
            .append("<div style=\"line-height:130%;display:inline-block;font-size:14px;margin:10px 0\">")
                    .append("<span style:'font-size: 14px'>Código de compra: <span style=\"font-weight:600; font-size: 14px\" id=\"m_-4881203300558445123codigoCompra\">")
            .append(carrito.getPago().getPagoId())
            .append("</span>")
            .append("</span>")
            .append("</div>")

       
//THE REST OF THE CODE WAS REMOVED SO IT IS NOT SO LONG

            .append("</div>")
            .append("</html>");
    // Usar la función de envío de correo para enviar el correo con el cuerpo construido y el tipo de contenido correcto
    mailSender.enviarEmail(correoDestinatario, "Tus tickets :)", cuerpoEmail.toString(), "text/html; charset=UTF-8");
}


我已经解释过了,但是基本上我需要一个java邮件中的方法的例子,它能够自动发送html给gmail用户,而不会破坏或变形。

zhte4eai

zhte4eai1#

我使用的是display:flex; gmail不支持,我通过实现表格结构解决了这个问题。

相关问题