Multipart email message

。_饼干妹妹 提交于 2019-12-19 08:06:28

问题


How to use html tags in a multipart email message. When I use <b> its not recognized as the bold tag.


回答1:


Ah, you're using Java.

Note that in my opinion, you should always set a plain text alternative in a HTML email.

This code also lets you inline images (referenced from the HTML with <img src="cid:foo">, but not all email clients support this.

MimeMessage mm = prepareMessage(from, to, subject, cc, bcc);
MimeMultipart mp = new MimeMultipart("alternative");

// Attach Plain Text
MimeBodyPart plain = new MimeBodyPart();
plain.setText(plainText);
mp.addBodyPart(plain);

/*
 * Any attached images for the HTML portion of the email need to be encapsulated with
 * the HTML portion within a 'related' MimeMultipart. Hence we create one of these and
 * set it as a bodypart for the overall message.
 */
MimeMultipart htmlmp = new MimeMultipart("related");
MimeBodyPart htmlbp = new MimeBodyPart();
htmlbp.setContent(htmlmp);
mp.addBodyPart(htmlbp);

// Attach HTML Text
MimeBodyPart html = new MimeBodyPart();
html.setContent(htmlText, "text/html");
htmlmp.addBodyPart(html);

// Attach template images (EmailImage is a simple class that holds image data)
for (EmailImage ei : template.getImages()) {
    MimeBodyPart img = new MimeBodyPart();
    img.setContentID(ei.getFilename());
    img.setFileName(ei.getFilename());
    ByteArrayDataSource bads = new ByteArrayDataSource(ei.getImageData(), ei.getMimeType());
    img.setDataHandler(new DataHandler(bads));
    htmlmp.addBodyPart(img);
}

mm.setContent(mp);



回答2:


You are setting the Content-type mime to text/html on that part of the email message?

Alternatively are you viewing using Outlook - Outlook's viewer uses Word, of all things, to render HTML, instead of using the IE rendering engine like any sensible design would do. This does mean that significant formatting can be lost.

Alternatively, try a different font. A couple of fonts don't define bold variants. This is a long shot though, most font rendering techniques can auto-bold a non-bold font.




回答3:


which programming language you are using to send email.

In any language a option should be there like "IsBodyHtml". To do the "True" this check. Like code of .NET

System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(); mm.IsBodyHtml = true;

So the mail will go as html text.




回答4:


It is a bit difficult to have a definitive answer to this question. There are many reasons why things may not work. These are some things you could check to try to isolate the problem.

Are other html tags recognised? eg. <p>, <a>? If so, have you tried using a <strong> tag instead of <b>?

Check the message source in the email reader. Maybe the '<' or '>' characters have been escaped as '&lt;' or '&gt;' before it was sent.

Have you tried viewing the email in a different reader, say webmail or desktop based?

Try using CSS to change the font-weight:

.important-text { font-weight: bold; }

<span class=".important-text">Super important text</span>


来源:https://stackoverflow.com/questions/1721220/multipart-email-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!