不使用Java Mail API接收邮件正文/内容 [英] Not receiving message body/content using Java Mail API

查看:128
本文介绍了不使用Java Mail API接收邮件正文/内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码发送邮件使用Java Mail API附件和正文部分(消息部分),但我只得到附件,而不是正文消息。



<任何帮助或意见将不胜感激。

  public static void sendmail(String to,String from,String url,
String port,final String username,final String password,
String filename){

属性props = new Properties();
props.put(mail.smtp.auth,true);
props.put(mail.smtp.starttls.enable,false);
props.put(mail.smtp.host,url);
props.put(mail.smtp.port,port);

会话session = Session.getInstance(props,
new javax.mail.Authenticator(){

protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(用户名,密码);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject(HTML邮件与图像);
message.setContent(< h1> Hello world< / h1>,text / html);
message.setHeader(Content-ID,&memememe>);
Multipart multipart = new MimeMultipart(related);
BodyPart messageBodyPart = new MimeBodyPart();
System.out.println(file attached is+ filename);
DataSource source = new FileDataSource(filename);
// messageBodyPart.setHeader(Content-ID,Part.ATTACHMENT);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(source.getName());
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart,Part.INLINE);
Transport.send(message);
System.out.println(发送邮件成功....);
} catch(MessagingException e){
System.out.println(::::::发送邮件时出错....
+ e.getMessage());
抛出新的RuntimeException(e);
}
}


解决方案

你正在调用 setContent()两次。

为了在您需要将内容设置为 MimeMultipart 的消息。嗯,你这样做 - 但是multipart对象不包括应该是邮件正文的部分。



如果你想要一个包含文本和附件,您创建一个 MimeMultipart multipart / mixed ,而不是 multipart / related c $ c>



以下是一些如何构建可能的消息的示例:






最简单的是带有几个附件的文本。例如,你想把你的简历发给某人,所以你写几个简介,并附上一个或多个文件(求职信,简历):

 
mime───multipart/mixed-┬-text
├─attachment1
└─attachment2

Multipart / mixed( MimeMultipart 的默认值)意味着电子邮件代理将逐个显示零件。




  • 如果附件的内容处置为 inline ,并且电子邮件代理能够显示给定的

  • 如果附件的内容处理是附件他们通常会被显示为某种图标或链接以保存附件。






如果要发送漂亮的HTML消息,通常要包含明文版本和HTML版本。这样即使在不支持HTML的电子邮件阅读器中,收件人也可以读取它。如果收件人可能会视力受损,这是非常有帮助的。所以,你需要使用multipart / alternative:

 
mime-──multipart/mixed-┬-multipart/alternative-┬-text/ plain
│└─text/ html
├─attachment1
└─attachment2

所以,再次消息内容包括身体和两个附件三部分。但是身体本身是一个 MimeMultipart(替代),它包含明文版本和HTML版本。记住要把明文放在第一位,而第二个HTML则是邮件代理选择最后一个它知道如何显示的选项。



附件是要像身体一样显示,就像以前一样。






现在我们来看看一个没有附件,但它确实有应该嵌入在HTML内的图像。在这种情况下,邮件代理需要知道附件不仅仅是将文件发送给读者进行下载,而且应该与HTML相关联地显示它们。所以正确的mime类型是 multipart / related ,以显示部件是相关的。在这种情况下,您还需要给予正确的内容ID,并在HTML中使用这些内容ID。这不是MIME标准的一部分,但是HTML邮件通常是这样做的。



就MIME而言,这样的消息将如下所示: / p>

 
mime-──multipart/alternative-┬-text/ plain
└─multipart/related-┬-text/ html
├─embeddedimage 1
└─embeddedimage 2

这一次我们没有附件,所以我们可以把multipart / alternative作为我们的顶级内容。它具有明文替代方法,如前所述,但第二种替代方案本身就是一个 MimeMultipart(related)



在里面,你有HTML部分和两个图像。 HTML及其图像必须始终是相同多部分/相关对象的一部分。






现在,如果你想将您的文档附加到这样的消息中,其中包含HTML 图像?那么你会使用这样的东西:

 
mime-──multipart/mixed-┬-multipart/alternative-┬-text/ plain
│└─multipart/related-┬-text/ html
│├─embeddedimage 1
│└─embeddedimage 2
├─attachment1
└─attachment2

所以你的顶级对象是multipart / mixed,允许你连续地添加附件到你的消息。消息body(multipart / mixed的第一部分)是具有嵌入式multipart / related的multipart / alternative的复杂结构。






总结:




  • 如果消息的结构不仅仅是一个纯文本体,那么它的内容必须是某种类型的 MimeMultipart 对象

  • 一个 multipart / mixed 消息有一个第一部分,应该是您可读的消息,其余的是附件。 / li>
  • 一个 multipart / alternative 消息给出了相同内容的不同显示方式,从最常见的分母排序到最罕见的演示类型(例如plain文本→HTML→富文本→专有格式),收件人的邮件程序选择最后一个它知道如何显示。

  • A multipart / related 消息通常用于将HTML主体与其内联消息相结合。第一部分是HTML,其他部分具有HTML用于其< img src =.../> 标签的Content-ID。 li>

I have below code for sending mail using Java Mail API with attachment and body part (message part), but I am getting only the attachment, not the body message.

Any help or comments will be appreciated.

public static void sendmail(String to, String from, String url,
        String port, final String username, final String password,
        String filename) {

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "false");
    props.put("mail.smtp.host", url);
    props.put("mail.smtp.port", port);

    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(from));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(to));
        message.setSubject("HTML        mail    with    images");
        message.setContent("<h1>Hello    world</h1>", "text/html");
        message.setHeader("Content-ID", "<memememe>");
        Multipart multipart = new MimeMultipart("related");
        BodyPart messageBodyPart = new MimeBodyPart();
        System.out.println("file    attached    is    " + filename);
        DataSource source = new FileDataSource(filename);
        // messageBodyPart.setHeader("Content-ID",Part.ATTACHMENT);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(source.getName());
        multipart.addBodyPart(messageBodyPart);
        message.setContent(multipart, Part.INLINE);
        Transport.send(message);
        System.out.println("Sent    message    successfully....");
    } catch (MessagingException e) {
        System.out.println("::::::Error    while    sending    mail...."
                + e.getMessage());
        throw new RuntimeException(e);
    }
}

解决方案

You are calling setContent() twice. Only one setContent() will work - the last one.

In order to have more than one part in your message you need to set the content to a MimeMultipart. Well, you are doing that - but the multipart object does not include the part that's supposed to be the message body.

If you want a message that has a text and an attachment, you create a MimeMultipart (multipart/mixed, not multipart/related as you did).

Here are some examples of how to structure possible messages:


The simplest of all is text with a few attachments. For example, you want to send your CV to someone, So you write a few words of introduction and attach one or more document (cover letter, CV):

mime───multipart/mixed─┬─text
                       ├─attachment1
                       └─attachment2

Multipart/mixed (the default for a MimeMultipart) means that the e-mail agent will show the parts serially - one after the other.

  • If the attachments' content-disposition is inline, and the e-mail agent is capable to show the given attachment type, then it will be shown inside the message itself - at its end.
  • If the attachments' content-disposition is attachment, they will usually be shown as some sort of icons or links for saving the attachments.

If you want to send a pretty HTML message, it is customary to include both a plaintext version and an HTML version. This is so that the recipient may be able to read it even in an e-mail reader that doesn't support HTML. It's very helpful if the recipient might be visually-impaired. So, you need to use multipart/alternative:

mime───multipart/mixed─┬─multipart/alternative─┬─text/plain
                       │                       └─text/html
                       ├─attachment1
                       └─attachment2

So, again, the message content includes three parts, the body and the two attachments. But the body itself is a MimeMultipart(alternative), and it contains the plaintext version and the HTML version. Remember to put the plaintext first, and the HTML second, as the convention is for the mail agent to pick the last alternative that it knows how to display.

The attachments are going to be displayed serially after the body, just like before.


Now let's look at a mail that doesn't have "attachments", but it does have images that are supposed to be embedded inside the HTML. In this case, the mail agent needs to know that the attachments are not just files sent over to the reader to be downloaded, but that it should display them in association with the HTML. So the correct mime type for that is multipart/related, to show that the parts are related. In this case, you also need to give them proper content IDs, and use those content IDs in the HTML. This is not part of the MIME standard, but it's how HTML mail is usually done these days.

As far as MIME is concerned, such a message will look like:

mime───multipart/alternative─┬─text/plain
                             └─multipart/related─┬─text/html
                                                 ├─embedded image 1
                                                 └─embedded image 2

This time we don't have attachments, so we can put the multipart/alternative as our top level content. It has the plaintext alternative first, like before, but the second alternative is itself a MimeMultipart("related").

Inside it, you have the HTML part, and the two images. The HTML and its images must always be parts of the same multipart/related object.


Now, what if you wanted to attach your document to such a message, one that has HTML and images inside it? Then you would be using something like this:

mime───multipart/mixed─┬─multipart/alternative─┬─text/plain
                       │                       └─multipart/related─┬─text/html
                       │                                           ├─embedded image 1
                       │                                           └─embedded image 2
                       ├─attachment1
                       └─attachment2

So your top level object is multipart/mixed, allowing you to add attachments serially to your message. The message "body" (the first part of the multipart/mixed) is the complex structure of multipart/alternative with an embedded multipart/related. And then the other attachments follow that.


In summary:

  • If the message has any structure that is more than just a plain text body, then its content has to be a MimeMultipart object of some kind.
  • A multipart/mixed message has a first part which is supposed to be your readable message, and the rest are attachments.
  • A multipart/alternative message gives different display alternatives of the same content, ordered from most common denominator to most rare presentation type (e.g. plain text→HTML→rich text→proprietary formatting) and the recipient's mail program picks the last one that it knows how to display.
  • A multipart/related message is usually used to combine an HTML body with its inline messages. The first part is the HTML, the other parts have Content-IDs that the HTML uses for its <img src="..." /> tags.

这篇关于不使用Java Mail API接收邮件正文/内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆