【问题标题】:How to send mail with an image as an attachment in android?如何在android中发送带有图像作为附件的邮件?
【发布时间】:2013-03-22 05:48:48
【问题描述】:

在我的 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);
    }

}

}

【问题讨论】:

    标签: android image email gmail email-attachments


    【解决方案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); 
    } 
    
    
        }
    

    【讨论】:

    • 它将图像附加到邮件中..但是当我看到邮件中的邮件时,我无法查看该图像...
    • 你能下载图片吗?
    • 是的...现在它的工作..将此添加到代码 messageBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
    • 有人可以帮助我吗?我在 android 4.2 中关注本教程 jondev.net/articles/… 但未能发送电子邮件我只想发送带附件和不带附件的电子邮件
    【解决方案2】:

    试试这个代码:

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

    让我知道它是否对你有用。

    【讨论】:

      【解决方案3】:
      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);
      

      【讨论】:

        【解决方案4】:

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

        Multipart multipart = new MimeMultipart();
        
        MimeBodyPart attachPart = new MimeBodyPart();
        String attachFile = "/storage/emulated/0/pic.jpg";//picture location
        
        DataSource source = new FileDataSource(attachFile);
        attachPart.setDataHandler(new DataHandler(source));
        attachPart.setFileName(new File(attachFile).getName());
        
        //Trick is to add the content-id header here
        attachPart.setHeader("Content-ID", "image_id");
        multipart.addBodyPart(attachPart);
        
        //third part for displaying image in the email body
        attachPart = new MimeBodyPart();
        attachPart.setContent("<h1>Attached Image</h1>" +
                "<img src='cid:image_id'>", "text/html");
        multipart.addBodyPart(attachPart);
        //Set the multipart message to the email message
        message.setContent(multipart);
        
        Transport.send(message);
        System.out.println("Done");
        

        【讨论】:

          猜你喜欢
          • 2014-03-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-04-16
          • 2015-07-22
          • 1970-01-01
          • 2018-12-23
          • 1970-01-01
          相关资源
          最近更新 更多