如何在android中将图像作为附件发送邮件?

时间:2013-03-22 05:48:49

标签: android image email gmail email-attachments

在我的Android应用程序中,我需要发送带有图像作为附件的邮件。我完成了发送邮件。但是如何发送带有图像作为邮件附件的邮件。我发布了发送邮件的代码。请帮我将图像作为附件发送到以下代码中。

这是代码 -

public class MailImageFile extends javax.mail.Authenticator {

        public MailImageFile(){}

 public void Mail(String user, String pass) {
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()           {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("abc@gmail.com", "pqr123%");
        }
        });
    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("abc@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,     InternetAddress.parse("xyz@gmail.com"));
        message.setSubject("Testing Subject");
        message.setContent("Hi...", "text/html; charset=utf-8");

        Transport.send(message);

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

}

}

4 个答案:

答案 0 :(得分:1)

 public class MailImageFile extends javax.mail.Authenticator {

            public MailImageFile(){}

     public void Mail(String user, String pass) {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator()           {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("abc@gmail.com", "pqr123%");
            }
            });
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("abc@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,     InternetAddress.parse("xyz@gmail.com"));
    message.setContent(_multipart);
            message.setSubject("Testing Subject");
            message.setContent("Hi...", "text/html; charset=utf-8");

            Transport.send(message);

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

// Got this solution form here

    private Multipart _multipart; 
_multipart = new MimeMultipart(); 

public void addAttachment(String filename,String subject) throws Exception { 
    BodyPart messageBodyPart = new MimeBodyPart(); 
    DataSource source = new FileDataSource(filename); 
    messageBodyPart.setDataHandler(new DataHandler(source)); 
    messageBodyPart.setFileName(filename); 
    _multipart.addBodyPart(messageBodyPart);

    BodyPart messageBodyPart2 = new MimeBodyPart(); 
    messageBodyPart2.setText(subject); 

    _multipart.addBodyPart(messageBodyPart2); 
} 


    }

答案 1 :(得分:0)

试试这段代码:

Intent myEmailIntent = new Intent(android.content.Intent.ACTION_SEND);
        myEmailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Email Subject");
        myEmailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{"sendersemailaddreass@gmail.com"});

        myEmailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"your email message");
        //message type
        myEmailIntent.setType("text/plain");
        File myAttachmentfile = getFileStreamPath("path of the file that you want to attach Eg: image");
        myEmailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        //attachment type
        myEmailIntent.setType("image/jpeg");
        myEmailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+myAttachmentfile.getAbsolutePath()));
        startActivityForResult(Intent.createChooser(myEmailIntent, "SENDER NAME"),101);

//这是您将图片或任何文件附加到电子邮件的方式

emailIntent.putExtra(Intent.EXTRA_STREAM, "your file path");

让我知道它是否适合你。

答案 2 :(得分:0)

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emailaddress);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
emailIntent.setType("image/png");

ArrayList<Uri> uris = new ArrayList<Uri>();

uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.file1));
uris.add(Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.file2));

emailIntent.putExtra(Intent.EXTRA_STREAM, uris));

startActivity(emailIntent);

答案 3 :(得分:0)

这肯定会帮助您在不使用意图的情况下附加图像。

{
    "sales": {
        "saledate": "17Mar2008",
        "sale": [{
            "productid": 1,
            "quantity": 10
        }, {
            "productid": 2,
            "quantity": 1
        }, {
            "productid": 3,
            "quantity": 3
        }, {
            "productid": 4,
            "quantity": 5
        }]
    }
}