无法在jsseprovider,gmailsender中附加文件

时间:2019-04-20 10:26:29

标签: java android

我正在尝试此代码。我可以使用以下代码发送邮件,但是当我插入附件代码(您可以在// CODE FROM HERE到// CODE TILL HERE之间看到)时显示错误

  

正在发送:javax.mail.SendFailedException:没有收件人地址

MimeMessage message = new MimeMessage(session);
try {

    DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
    message.setSender(new InternetAddress(sender));
    message.setSubject(subject);
    message.setDataHandler(handler);
//CODE FROM HERE 
    Multipart emailContent = new MimeMultipart();

    //Text body part
    MimeBodyPart textBodyPart = new MimeBodyPart();
    textBodyPart.setText("My multipart text");

    //Attachment body part.
    MimeBodyPart pdfAttachment = new MimeBodyPart();
    clienttargetPdf = Environment.getExternalStorageDirectory().getAbsolutePath() + "/mm.pdf";
    pdfAttachment.attachFile(clienttargetPdf);

    //Attach body parts
    emailContent.addBodyPart(textBodyPart);
    emailContent.addBodyPart(pdfAttachment);

    //Attach multipart to message
    message.setContent(emailContent);

    Transport.send(message);
//CODE TILL HERE.  

    if (recipients.indexOf(',') > 0)
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
    else
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
    Transport.send(message);
} catch (Exception e) {
    Log.d("mylog", "Error in sending: " + e.toString());
}

1 个答案:

答案 0 :(得分:0)

当您已经尝试发送消息时,我想说您的问题就在这里:

int b, sign = ((b = byteBuffer.ReadByte()) >> 6) & 1, i = b & 0x3F, offset = 6;

但实际上应该在设置收件人之后几行发送

    Transport.send(message);
//CODE TILL HERE.