简体   繁体   English

如何在Android应用中通过邮件发送附件?

[英]How can i send an attachment through mail in android app?

Hi I am sending a mail through android app. 嗨,我正在通过android应用发送邮件。 I import the mail library and activation library in my app. 我在应用程序中导入了邮件库和激活库。

When i send the mail has been sent successfully but the attachment is didn't. 当我发送邮件时,邮件已成功发送,但附件未发送。 Can any tell me how can i send that also. 任何人都可以告诉我如何发送。

Here is my code: 这是我的代码:

public synchronized void sendMail(String body, String recipients) throws Exception {   
        try{
        MimeMessage message = new MimeMessage(session);   
        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));   
        message.setSender(new InternetAddress("shankar.uclid@gmail.com"));   
        message.setSubject("Request For Claim");


        MimeBodyPart messageBodyPart2=new MimeBodyPart(); // creating new MimeBodyPart object and setting DataHandler to this object
        String filename="file:///android_asset/code.js"; //you can change according to your choice
        DataSource source=new FileDataSource(filename);
        messageBodyPart2.setDataHandler(new DataHandler(source));
        messageBodyPart2.setFileName(filename);

        Multipart multipart=new MimeMultipart(); 
        multipart.addBodyPart(messageBodyPart2);

        message.setContent(multipart);

        message.setDataHandler(handler);  
        showLog("recepetent is "+recipients);
        if (recipients.indexOf(',') < 0)   

            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("shankar.uclid@gmail.com"));   
        else  
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   
        Transport.send(message);   
        }catch(Exception e){

            e.printStackTrace();
        }
    }   

I don't why my attachment not getting. 我不为什么我的依恋没有得到。

Thank You 谢谢

Try this way,hope this will help you to solve your problem. 尝试这种方式,希望这将帮助您解决问题。

    public static void email(Context context, String to, String cc,String subject, String body, List<String> files)
    {
        //need to "send multiple" to get more than one attachment
        final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
        emailIntent.setType("text/plain");
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                new String[]{to});
        emailIntent.putExtra(android.content.Intent.EXTRA_CC,
                new String[]{cc});
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
        emailIntent.putExtra(Intent.EXTRA_TEXT, body);
        //has to be an ArrayList
        ArrayList<Uri> uris = new ArrayList<Uri>();
        //convert from paths to Android friendly Parcelable Uri's
        for (String file : files)
        {
            File fileIn = new File(file);
            Uri u = Uri.fromFile(fileIn);
            uris.add(u);
        }
        emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
        context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM