如何在Android中以图片作为附件发送邮件? [英] How to send mail with an image as an attachment in android?

查看:308
本文介绍了如何在Android中以图片作为附件发送邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的android应用程序中,我需要发送带有图像的邮件作为附件。发送邮件已经完成。但是如何发送带有图像的邮件作为邮件的附件。发送邮件的代码。请帮助我以以下代码的附件形式发送图像。

In my android application i need to send the mail with an image as a attachment.I am done with the sending the mail.but how to send the mail with an image as a attachment to the mail.Here i am posting the code for the sending the mail.Please help me to send the image as an attachment in following code.

这是代码-

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);
    }

}

}

推荐答案

 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);
        }

// 在此处获得此解决方案表格

    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); 
} 


    }

这篇关于如何在Android中以图片作为附件发送邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆