因此,我遇到了Javamail的问题,如果我在邮件中发送附件,正文就会消失。当我不随邮件发送附件时,我可以看到尸体。

我的GMailSender.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);
    }


我的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();
            }
        });


那么,如何在仍然可以看到身体本身的同时添加附件?

提前致谢!

最佳答案

看来您是从其他人那里复制了GMailSender。您应该首先修复所有这些common mistakes

您永远不会调用addAttachment方法。 (请注意,您可以使用MimeBodyPart.attachFile方法替换该方法)。请记住张贴您实际使用的代码。

您缺少的关键见解是对于多部分消息,主体部分需要成为多部分中的第一部分。您对Message.setContent(_multipart);的呼叫将覆盖您对message.setDataHandler(handler);的呼叫所设置的消息内容,这也将覆盖您对message.setText(body);的呼叫所设置的内容。

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

09-26 13:59