Java Mail 使用 CSS 格式化在电子邮件中发送的表格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26705602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:07:00  来源:igfitidea点击:

Java Mail Formatting a table sent in an e-mail using CSS?

javajavamail

提问by mg3np1

I can success you the Java Mail API to send e-mails but now I am trying to sending the contents of a ResultSet populated from a MySQL query in table formatted with borders etc, can I use CSS tags to do this? if so how?

我可以成功使用 Java Mail API 来发送电子邮件,但现在我正在尝试发送从 MySQL 查询填充的 ResultSet 的内容,该结果集的内容以带边框等格式设置,我可以使用 CSS 标签来执行此操作吗?如果是这样怎么办?

My code is as follows:

我的代码如下:

public void getOutstanding() throws MessagingException {
    try {
        String outS = "SELECT period_to, type, amt, status FROM tblinstall "
                        + "WHERE status like ?";

        PreparedStatement update = toDB.prepareStatement(outS);

        email = new StringBuilder();

        email.append("<html><head><style type='text/css'>table .out {border-width:1px, "
                    + "border-color: black}</style></head>"
                    + "<body>"
                    + "<table class'out'><span style=border-color: black, border-width: 1px>");

        update.setString(1, "Outstanding");
        ResultSet results = update.executeQuery();

        while (results.next()) {
            System.out.println("in results...");
            email.append("<tr>");
            email.append("<td>");
            long period = results.getLong("period_to");
            email.append(DateConvert.fromEpoch(period));
            email.append("</td>");

            email.append("<td>");
            email.append(results.getString("type"));
            email.append("</td>");

            email.append("<td>");
            email.append(results.getString("amt"));
            email.append("</td>");

            email.append("<td>");
            email.append(results.getString("status"));
            email.append("</td>");

            email.append("<tr>");
        }

        email.append("</table></body></html>");
        sm.populateMailMessage(email.toString());
        sm.sendMail();
    } catch (SQLException sql) {
        sql.printStackTrace();
    }                 
}

I am able to send the email but only plain text, 'sm' is an instances of SendMail as below:

我可以发送电子邮件,但只能发送纯文本,“sm”是 SendMail 的一个实例,如下所示:

public class SendMail {
    private Properties mailAccessCredentials;
    private Session mailSession;
    private MimeMessage mailMessage;        

    public SendMail() throws MessagingException{
        populateProperties();
    }    

    private void populateProperties() throws AddressException, MessagingException {     
        //Step1    
        System.out.println("\n 1st ===> setup Mail Server Properties..");
        mailAccessCredentials = System.getProperties();
        mailAccessCredentials.put("mail.smtp.port", "587"); // TLS Port
        mailAccessCredentials.put("mail.smtp.auth", "true"); // Enable Authentication
        mailAccessCredentials.put("mail.smtp.starttls.enable", "true"); // Enable StartTLS
        System.out.println("Mail Server Properties have been setup successfully..");     
    }    

    public void populateMailMessage(String msg) throws MessagingException{               
        //Step2    
        System.out.println("\n\n 2nd ===> get Mail Session..");
        mailSession = Session.getDefaultInstance(mailAccessCredentials, null);
        mailMessage = new MimeMessage(mailSession);
        mailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("n****@gmail.com"));

        mailMessage.setSubject("Report from Database Payments now Due");

        mailMessage.setContent(msg, "text/html");
        System.out.println("Mail Session has been created successfully..");
    }

    public void sendMail() throws MessagingException{        
        //Step3    
        System.out.println("\n\n 3rd ===> Get Session and Send mail");
        Transport transport = mailSession.getTransport("smtp");

        // Enter your correct gmail UserID and Password
        transport.connect("smtp.gmail.com", "lettingssmart@******", "****");
        transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
        transport.close();
    }

    public static void main(String[] args) throws MessagingException {
        SendMail sm = new SendMail();
        //sm.populateMailMessage();
        sm.sendMail();
    }
}

采纳答案by Madhawa Priyashantha

you can use inline cssfor style your table .you can't use css separatly since most email services don't support it ..try to change your email String building code to following code.

您可以使用内联 css为您的表格设置样式。您不能单独使用 css,因为大多数电子邮件服务不支持它。尝试将您的电子邮件字符串构建代码更改为以下代码。

 email.append("<html><body>"
                    + "<table style='border:2px solid black'>");

        update.setString(1, "Outstanding");
        ResultSet results = update.executeQuery();

        while (results.next()) {
            System.out.println("in results...");
            email.append("<tr bgcolor=\"#33CC99\">");
            email.append("<td>");
            long period = results.getLong("period_to");
            email.append(DateConvert.fromEpoch(period));
            email.append("</td>");

            email.append("<td>");
            email.append(results.getString("type"));
            email.append("</td>");

            email.append("<td>");
            email.append(results.getString("amt"));
            email.append("</td>");

            email.append("<td>");
            email.append(results.getString("status"));
            email.append("</td>");

            email.append("<tr>");
        }

        email.append("</table></body></html>");
        sm.populateMailMessage(email.toString());
        sm.sendMail();