无法在javamail中发送附件

时间:2017-10-23 06:34:08

标签: android email javamail email-attachments

我尝试在android中使用java mail发送带附件的电子邮件,选择要从图库发送的文件的过程成功,收件人已成功收到电子邮件,但附件未发送,

示例代码

private void sendEmail() {
    //Getting content for email
    String email = editTextEmail.getText().toString().trim();
    String subject = editTextSubject.getText().toString().trim();

    String message1 = editTextMessage.getText().toString();

    Symmetric sym = new Symmetric();
    String key = sym.GenerateKeyString();
    //byte[] nonce = sym.GenerateNonce();

    String mes = sym.Encrypt(key, message1);
    String message = mes;
    Log.d("key", key);
    Log.d("Pesan", mes);


    String attachment = String.valueOf(URI);

    //Creating SendMail object

        SendMail sm = new SendMail(this, email, subject, message, attachment);

    //Executing sendmail to send email
    sm.execute();
}

public void openGallery() {
    Intent intent = new Intent();
    intent.setType("*/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.putExtra("return-data", true);
    startActivityForResult(
            Intent.createChooser(intent, "Complete action using"),
            PICK_FROM_GALLERY);
}

mime消息中的这部分附件

messageBodyPart = new MimeBodyPart();
            String filename = attachment;
            Log.d("PATH FIle:", filename);
            DataSource source = new FileDataSource(filename);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(filename);
            multipart.addBodyPart(messageBodyPart);
            MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
            mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
            mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
            mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
            mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
            mc.addMailcap("message/rfc822;; x-java-content- handler=com.sun.mail.handlers.message_rfc822");

            // Send the complete message parts
            mm.setContent(multipart);
            Transport.send(mm);

附件

try {

        //Creating MimeMessage object
        if (attachment!="null") {
            MimeMessage mm = new MimeMessage(session);

            //Setting sender address
            mm.setFrom(new InternetAddress(MainActivity.email));
            //Adding receiver
            mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
            //Adding subject
            mm.setSubject(subject);
            //Adding message

            BodyPart messageBodyPart = new MimeBodyPart();

            // Now set the actual message
            messageBodyPart.setText(message);
            Log.d("PATH FIle:", attachment);
            // Create a multipar message

            Multipart multipart = new MimeMultipart();

            // Set text message part
            multipart.addBodyPart(messageBodyPart);

            // Part two is attachment
            messageBodyPart = new MimeBodyPart();
            String filename = attachment;
            Log.d("PATH FIle:", filename);
            DataSource source = new FileDataSource(filename);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(filename);
            multipart.addBodyPart(messageBodyPart);
            MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
            mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
            mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
            mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
            mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
            mc.addMailcap("message/rfc822;; x-java-content- handler=com.sun.mail.handlers.message_rfc822");

            // Send the complete message parts
            mm.setContent(multipart);
            Transport.send(mm);
        }

        Log.d("Tanpa Attachment", attachment);
        MimeMessage mm = new MimeMessage(session);
        //Log.d("celeng",enkrip);
        //Setting sender address
        mm.setFrom(new InternetAddress(MainActivity.email));
        //Adding receiver
        mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
        //Adding subject
        mm.setSubject(subject);
        //Adding message
        //Asymmetric asym = new Asymmetric();
        //String enkripmessage = asym.Encrypts(message);
        mm.setText(message);
        Transport.send(mm);

        //Sending email

        //Sending email


    } catch (MessagingException e) {
        e.printStackTrace();
    }

0 个答案:

没有答案