Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试使用JavaMail通过Outlook在HTML电子邮件中发送CSS_Java_Html_Css_Email_Jakarta Mail - Fatal编程技术网

尝试使用JavaMail通过Outlook在HTML电子邮件中发送CSS

尝试使用JavaMail通过Outlook在HTML电子邮件中发送CSS,java,html,css,email,jakarta-mail,Java,Html,Css,Email,Jakarta Mail,我试图通过构建字符串并将其作为参数传递给我的邮件方法来发送HTML电子邮件: // Build string to send as email message StringBuilder buf = new StringBuilder(); buf.append("<html>" + "<body>" + "<table>" + "&

我试图通过构建字符串并将其作为参数传递给我的邮件方法来发送HTML电子邮件:

// Build string to send as email message
        StringBuilder buf = new StringBuilder();
        buf.append("<html>" +
                "<body>" +
                "<table>" +
                "<tr style=\"background-color: #8C0000; color: #fff; font-family: Arial; font-size: 14px; font-weight: bold;\">" +
                "<th style=\"padding: 10px;\">Assigned Group</th>" +
                "<th style=\"padding: 10px;\">Incident Type</th>" +
                "<th style=\"padding: 10px;\">Status</th>" +
                "<th style=\"padding: 10px;\">Assignee</th>" +
                "<th style=\"padding: 10px;\">Incident Number</th>" +
                "<th style=\"padding: 10px;\">Priority</th>" +
                "<th style=\"padding: 10px;\">Summary</th>" +
                "</tr>");
        for (int i = 0; i < incidentNumberList.size(); i++) {
            buf.append("<tr style=\"font-family: Arial; font-size: 12px;\"><td style=\"padding: 10px;\">")
                    .append(assignedGroup)
                    .append("</td><td style=\"padding: 10px;\">")
                    .append(incidentTypeList.get(i))
                    .append("</td><td style=\"padding: 10px;\">")
                    .append(statusList.get(i))
                    .append("</td><td style=\"padding: 10px;\">")
                    .append(assigneeNameList.get(i))
                    .append("</td><td style=\"padding: 10px;\">")
                    .append(incidentNumberList.get(i))
                    .append("</td><td style=\"padding: 10px;\">")
                    .append(priorityList.get(i))
                    .append("</td><td style=\"padding: 10px;\">")
                    .append(summaryList.get(i))
                    .append("</td></tr>");
        }
        buf.append("</table>" +
                "</body>" +
                "</html>");
        String emailMessage = buf.toString();

        // Send an email to the manager of the Assigned Group
        sendManagerEmail(emailMessage);
private static void sendManagerEmail(String emailMessage) {
        final String username = "user";
        final String password = "pass123";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        if (ENCRYPT) {
            props.put("mail.smtp.starttls.enable", "true");
        } else {
            props.put("mail.smtp.starttls.enable", "false");
        }
        props.put("mail.smtp.ssl.trust", "internetsmtp.example.com");
        props.put("mail.smtp.host", "internetsmtp.example.com");
        props.put("mail.smtp.port", "25");
        // SSL
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("martin@example.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("martin@example.com"));
            message.setSubject("Tickets — Attention Needed");
            message.setContent(emailMessage
                    , "text/html; charset=utf-8");

            Transport.send(message);

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
这里重要的一行是
message.setContent(emailMessage
,“text/html;charset=utf-8”)
我发送到此方法的HTML代码字符串包含消息内容的主体


电子邮件的格式是表格,而不是CSS,这使得它相当难看。我可以在这里做些什么来显示CSS吗?我认为将样式直接构建到html标记中就足够了。

上述代码是正确的,但请确保使用TLS()对电子邮件进行加密,以免它们最终成为垃圾邮件。垃圾邮件通常不会呈现HTML。如果设置TLS时遇到问题,请通过将邮件从垃圾邮件移动到收件箱来进行调试,以查看呈现的HTML。

上述代码是正确的,但请确保使用TLS()对电子邮件进行加密,以便它们不会最终成为垃圾邮件。垃圾邮件通常不会呈现HTML。如果设置TLS时遇到问题,请通过将邮件从垃圾邮件移动到收件箱来进行调试,以查看呈现的HTML。

如果尝试将CSS嵌入到实际的HTML中如何?
css-code

如果您尝试将css嵌入到实际的html中,会怎么样?
css code

您能发布电子邮件的html吗?电子邮件的html在上面可见。它被构建为一个字符串,然后被传递到mail方法中@AdrienQUINTYep,但我想看看最终的渲染(电子邮件)如何。我明白了。由于内容的原因,我不能确切地向您展示,但基本上没有应用CSS。表格已应用,但没有样式。我相信你可以根据上面的CSS进行想象。:P至少其他人会知道不要调试垃圾邮件。你能发布电子邮件的html吗?电子邮件的html在上面可见。它被构建为一个字符串,然后被传递到mail方法中@AdrienQUINTYep,但我想看看最终的渲染(电子邮件)如何。我明白了。由于内容的原因,我不能确切地向您展示,但基本上没有应用CSS。表格已应用,但没有样式。我相信你可以根据上面的CSS想象。:P至少其他人会知道不要调试垃圾邮件。