首页 > 解决方案 > Javamail 正文与附件一起时不出现

问题描述

所以我有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();
            }
        });

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

提前致谢!

标签: javaandroid-studiojakarta-mail

解决方案


看起来您从其他人那里复制了 GMailSender。您应该从修复所有这些常见错误开始。

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

您缺少的关键见解是,对于多部分消息,主体部分必须是多部分中的第一个正文部分。您的呼叫Message.setContent(_multipart);覆盖了您呼叫设置的消息内容message.setDataHandler(handler);,这也覆盖了您呼叫设置的内容message.setText(body);

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


推荐阅读