显示从servlet到电子邮件的HTML表格数据,但我在电子邮件

时间:2017-02-25 19:01:50

标签: java html html-email

我正在使用以下代码。但在邮件中,我得到的palin html标签只是不以表格格式输出。请帮我把邮件中的输出作为表格格式

StringBuilder content = new StringBuilder(); 
content.append("<HTML>")
.append("<HEAD>")
.append("<TITLE>Welcome</TITLE>")
.append("</HEAD>")
// Start on the body
.append("<BODY>")
.append("<CENTER>")
.append("<TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0 WIDTH=70%>")
.append("<TR>")
.append("<TH style=\"padding:5px\">Subject</TH>")
.append("<TH style=\"padding:5px\">Grade</TH>")
.append("</TR>")
.append("<TR>")
.append("<TD>Mathmatics</TD>")
.append("<TD>A</TD>")
.append("</TR>")
.append("<TR>")
.append("<TD>SCEINECE</TD>")
.append("<TD>B</TD>")
.append("</TR>")       

.append("</TABLE>")
.append("</CENTER>")
.append("</BODY></HTML>");



String resultMessage = "";

try {
    EmailUtility.sendEmail(host, port, user, pass, recipient, subject, content.toString());
}

3 个答案:

答案 0 :(得分:0)

我使用过javax邮件。这将有效

 if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {
        Uri selectedImageUri = data.getData();

        imageview.setImageURI(selectedImageUri);

        try {
            bitmap1 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "" + e, Toast.LENGTH_SHORT).show();
        }

        bitmap1.compress(Bitmap.CompressFormat.JPEG, 7, bytearrayoutputstream);

        BYTE = bytearrayoutputstream.toByteArray();

        bitmap2 = BitmapFactory.decodeByteArray(BYTE, 0, BYTE.length);

        imagetoo.setImageBitmap(bitmap2);
    }

MailParam类

public class Main {

public static void main(String[] arg) {

    StringBuilder MessageContent = new StringBuilder();
    MessageContent.append("<HTML>")
            .append("<HEAD>")
            .append("<TITLE>Welcome</TITLE>")
            .append("</HEAD>")
            // Start on the body
            .append("<BODY>")
            .append("<CENTER>")
            .append("<TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0 WIDTH=70%>")
            .append("<TR>")
            .append("<TH style=\"padding:5px\">Subject</TH>")
            .append("<TH style=\"padding:5px\">Grade</TH>")
            .append("</TR>")
            .append("<TR>")
            .append("<TD>Mathmatics</TD>")
            .append("<TD>A</TD>")
            .append("</TR>")
            .append("<TR>")
            .append("<TD>SCEINECE</TD>")
            .append("<TD>B</TD>")
            .append("</TR>")

            .append("</TABLE>")
            .append("</CENTER>")
            .append("</BODY></HTML>");

    MailParam mp = new MailParam();
    mp.setUsername("....");
        .....
    Properties props = new Properties();
    props.put("mail.smtp.auth", mailParam.getAuth());
    props.put("mail.smtp.starttls.enable", mailParam.getStartls());
    props.put("mail.smtp.host", mailParam.getHost());
    props.put("mail.smtp.port", mailParam.getPort());


    BodyPart messageBodyPart = new MimeBodyPart();
    Multipart multipart = new MimeMultipart();
    try {
        messageBodyPart.setContent(MessageContent, "text/html; charset=utf-8");

        //messageBodyPart.setText(MessageContent);
        multipart.addBodyPart(messageBodyPart);
    } catch (MessagingException e) {
        e.printStackTrace();
    }


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


    // Sender's email ID needs to be mentioned


    try {
        // Create a default MimeMessage object.
        MimeMessage message = new MimeMessage(session);

        message.addHeader("Content-type", "text/HTML; charset=UTF-8");

        message.addHeader("Content-Transfer-Encoding", "quoted-printable");


        // Set From: header field of the header.
        message.setFrom(new InternetAddress("your_mail@yourdomain");







        //message.setRecipients(Message.RecipientType.TO,recipientAddress);
        message.setRecipients(Message.RecipientType.TO, "npsantost@libero.it");

        // Set Subject: header field
        message.setSubject("my subject");

        // Send the actual HTML message, as big as you like
        //message.setContent("<h1>This is actual message</h1>", "text/html");

        message.setContent(multipart);

        // Send message
        Transport.send(message);
} catch (AddressException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    }


}
}

答案 1 :(得分:0)

这是我的Utility类代码:

public class EmailUtility {
public static void sendEmail(String host, String port,
        final String userName, final String password, String toAddress,
        String subject, String message) throws AddressException,
        MessagingException {

    // sets SMTP server properties
    Properties properties = new Properties();
    properties.put("mail.smtp.host", host);
    properties.put("mail.smtp.port", port);
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");

    // creates a new session with an authenticator
    Authenticator auth = new Authenticator() {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password);
        }
    };

    Session session = Session.getInstance(properties, auth);

    // creates a new e-mail message
    Message msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress(userName));
    InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
    msg.setRecipients(Message.RecipientType.TO, toAddresses);
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    msg.setText(message);

    // sends the e-mail
    Transport.send(msg);

}

}

答案 2 :(得分:0)

在您的Class EmailUtility

中添加此块
   BodyPart messageBodyPart = new MimeBodyPart();
    Multipart multipart = new MimeMultipart();
    try {
        messageBodyPart.setContent(message, "text/html; charset=utf-8");

        //messageBodyPart.setText(MessageContent);
        multipart.addBodyPart(messageBodyPart);
    } catch (MessagingException e) {
        e.printStackTrace();
    }

然后替换

msg.setText(消息);

 // creates a new e-mail message
    Message msg = new MimeMessage(session);
    msg.setContent(multipart);