Javamail正文与附件一起显示时不显示

Javamail正文与附件一起显示时不显示,java,android-studio,jakarta-mail,Java,Android Studio,Jakarta Mail,因此,我对Javamail有一个问题,如果我在邮件中发送附件,主体就会消失。当我不随邮件发送附件时,我只能看到邮件的正文 My Gmail Sender.java: public GMailSender(String user, String password) { this.user = user; this.password = password; Properties props = new Properties(); pr

因此,我对Javamail有一个问题,如果我在邮件中发送附件,主体就会消失。当我不随邮件发送附件时,我只能看到邮件的正文

My Gmail Sender.java:

public GMailSender(String user, String password) {
        this.user = user;
        this.password = password;

        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", mailhost);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.quitwait", "false");

        session = Session.getDefaultInstance(props, this);
        _multipart = new MimeMultipart();
    }


    protected PasswordAuthentication getPasswordAuthentication()
    {
        return new PasswordAuthentication(user, password);
    }

    public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception
    {
        MimeMessage message = new MimeMessage(session);
        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
        message.setSender(new InternetAddress(sender));
        message.setSubject(subject);

        message.setText(body);
        message.setDataHandler(handler);
        if(_multipart.getCount() > 0)
            message.setContent(_multipart);
        if (recipients.indexOf(',') > 0)
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
        else
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
        Transport.send(message);

    }

    public void addAttachment(String filename) throws Exception
    {
        BodyPart messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(filename);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(filename);

        _multipart.addBodyPart(messageBodyPart);
    }
My MainActivity.java

        Button bt_send = findViewById(R.id.Alert);
        bt_send.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                BackgroundMail bm = new BackgroundMail(context);
                bm.setGmailUserName("***@gmail.com");
                bm.setGmailPassword("******");
                bm.setMailTo(to);
                bm.setFormSubject(value + " DOC/" + mTvResult.getText().toString());
                bm.setFormBody("Document Nummer:\n" + mTvResult.getText().toString() + "\n \nDocument Type:\n" + value);
                if (images.size() > 0) {
                    for (Object file : images) {
                        bm.setAttachment(file.toString());
                    }
                }
                bm.setSendingMessage("Loading...");
                bm.setSendingMessageSuccess("The mail has been sent successfully.");
                bm.send();
            }
        });
那么,我如何在仍然能够看到身体本身的情况下添加附件呢


提前谢谢

看起来你从其他人那里复制了Gmail发件人。你应该从解决这些问题开始

您从未调用addAttachment方法。(请注意,您可以用方法替换该方法)。请记住张贴您实际使用的代码

您缺少的关键细节是,对于多部分消息,主体部分必须是多部分中的第一个主体部分。调用
Message.setContent(_multipart)
覆盖调用
message.setDataHandler(handler)设置的消息内容
,它还重写了通过调用
message.setText(body)设置的内容

您需要创建另一个MimeBodyPart,在该MimeBodyPart上设置内容,然后将该MimeBodyPart添加为多部分的第一部分,而不是在消息上设置该内容