繁体   English   中英

如何在电子邮件正文中显示图像?

[英]How to show an image in the email body?

注意:我不想将图像附加到电子邮件中

我想在电子邮件正文中显示图片,

我曾尝试过HTML图像标记<img src=\\"http://url/to/the/image.jpg\\">"我得到了输出,你可以在我的问题中看到如何在电子邮件中添加图像身体 ,所以我累了Html.ImageGetter

它对我不起作用,它也给了我相同的输出,所以我怀疑是否可以这样做,

我的代码

Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL,new String[] {"abc@gmail.com"}); 
i.putExtra(Intent.EXTRA_TEXT,
    Html.fromHtml("Hi <img src='http://url/to/the/image.jpg'>",
    imgGetter,
    null));

i.setType("image/png");
startActivity(Intent.createChooser(i,"Email:"));


private ImageGetter imgGetter = new ImageGetter() {

    public Drawable getDrawable(String source) {
        Drawable drawable = null;
            try {
                drawable = getResources().getDrawable(R.drawable.icon);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight());
            } catch (Exception e) {
                e.printStackTrace();
                Log.d("Exception thrown",e.getMessage());
            } 
            return drawable;
    }
};

更新1:如果我使用TextViewImageGetter代码我能够获取文本和图像,但我无法在电子邮件正文中看到图像

这是我的代码:

TextView t = null;
t = (TextView)findViewById(R.id.textviewdemo);
t.setText(Html.fromHtml("Hi <img src='http://url/to/the/image.jpg'>",
    imgGetter,
    null));

更新2:我使用了粗体标签和锚标签,因为我在下面显示这些标签工作正常,但是当我使用img标签时,我能够看到一个方形框,表示为OBJ

 i.putExtra(Intent.EXTRA_TEXT,Html.fromHtml("<b>Hi</b><a href='http://www.google.com/'>Link</a> <img src='http://url/to/the/image.jpg'>",
        imgGetter,
        null));

不幸的是,使用Intents不可能做到这一点。

为什么例如粗体文本显示在EditText上,而不是一个形象的原因是StyleSplan正在实施ParcelableImageSpan没有。 因此,当在新的Activity中检索到Intent.EXTRA_TEXT时,ImageSpan将无法取消选中,因此不会成为附加到EditText的样式的一部分。

遗憾的是,使用其他方法不使用Intent传递数据,因为您无法控制接收活动。

我知道这不能回答原来的问题,但对于某些人来说可能是一个可接受的选择,而是将图像附加到电子邮件中。 我设法通过以下代码实现了这一目标......

String urlOfImageToDownload = "https://ssl.gstatic.com/s2/oz/images/google-logo-plus-0fbe8f0119f4a902429a5991af5db563.png";

// Start to build up the email intent
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[] { "abc@mail.com" });
i.putExtra(Intent.EXTRA_SUBJECT, "Check Out This Image");
i.putExtra(Intent.EXTRA_TEXT, "There should be an image attached");

// Do we need to download and attach an icon and is the SD Card available?
if (urlOfImageToDownload != null && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
    // Download the icon...
    URL iconUrl = new URL(urlOfImageToDownload);
    HttpURLConnection connection = (HttpURLConnection) iconUrl.openConnection();
    connection.setDoInput(true);
    connection.connect();
    InputStream input = connection.getInputStream();
    Bitmap immutableBpm = BitmapFactory.decodeStream(input);

    // Save the downloaded icon to the pictures folder on the SD Card
    File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    directory.mkdirs(); // Make sure the Pictures directory exists.
    File destinationFile = new File(directory, attachmentFileName);
    FileOutputStream out = new FileOutputStream(destinationFile);
    immutableBpm.compress(Bitmap.CompressFormat.PNG, 90, out);
    out.flush();
    out.close();
    Uri mediaStoreImageUri = Uri.fromFile(destinationFile);     

    // Add the attachment to the intent
    i.putExtra(Intent.EXTRA_STREAM, mediaStoreImageUri);
}                       

// Fire the intent
startActivity(i);

http://www.oliverpearmain.com/blog/android-how-to-launch-an-email-intent-attaching-a-resource-via-a-url/

两个简单的建议:

  • 关闭你的img标签( <img src="..." />而不是<img src="...">
  • 使用i.setType("text/html")代替i.setType("image/png")

如果这些都不起作用,也许您尝试将图像附加到电子邮件,然后使用"cid:ATTACHED_IMAGE_CONTENT_ID" "http:URL_TO_IMAGE"而不是"http:URL_TO_IMAGE"来引用它?

Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL,new String[] {"abc@gmail.com"}); 
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("http://url/to/the/image.jpg");
i.putExtra(Intent.EXTRA_TEXT,
        Html.fromHtml("Hi <img src='cid:image.jpg' />", //completely guessing on 'image.jpg' here
        imgGetter,
        null));
i.setType("image/png");

请参阅apache电子邮件用户指南中标题为发送带有嵌入式图像的HTML格式电子邮件的部分

虽然,那么你需要知道附加图像的内容id,我不确定是否通过标准的Intent方法浮出水面。 也许您可以检查原始电子邮件并确定其命名约定?

    i resolve problem send image as a body mail in android

   you have need three lib  of java mail 

    1.activation.jar
    2.additionnal.jar
    3.mail.jar




    public class AutomaticEmailActivity extends Activity {

        @Override 
        public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.main); 
            if (android.os.Build.VERSION.SDK_INT > 9) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
            }



            final String fromEmail = "abc@gmail.com"; //requires valid gmail id
            final String password = "abc"; // correct password for gmail id
            final String toEmail = "pradeep.bishnoi89@gmail.com"; // can be any email id 

            System.out.println("SSLEmail Start");
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host
            props.put("mail.smtp.socketFactory.port", "465"); //SSL Port
            props.put("mail.smtp.socketFactory.class",
                    "javax.net.ssl.SSLSocketFactory"); //SSL Factory Class
            props.put("mail.smtp.auth", "true"); //Enabling SMTP Authentication
            props.put("mail.smtp.port", "465"); //SMTP Port

            Authenticator auth = new Authenticator() {
                //override the getPasswordAuthentication method
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(fromEmail, password);
                }
            };

            final Session session = Session.getDefaultInstance(props, auth);
            Button send_email=(Button) findViewById(R.id.send_email);
            send_email.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    sendImageEmail(session, toEmail,"SSLEmail Testing Subject with Image", "SSLEmail Testing Body with Image");

                }
            });

        }




        public static void sendImageEmail(Session session, String toEmail, String subject, String body){
            try{
                MimeMessage msg = new MimeMessage(session);
                msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
                msg.addHeader("format", "flowed");
                msg.addHeader("Content-Transfer-Encoding", "8bit");

                msg.setFrom(new InternetAddress("no_reply@journaldev.com", "NoReply-JD"));

                msg.setReplyTo(InternetAddress.parse("no_reply@journaldev.com", false));

                msg.setSubject(subject, "UTF-8");

                msg.setSentDate(new Date());

                msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));

                MimeMultipart multipart = new MimeMultipart("related");

                BodyPart messageBodyPart = new MimeBodyPart();
                String htmlText = "<H1>Hello</H1><img src=\"cid:image\">";
                messageBodyPart.setContent(htmlText, "text/html");
                // add it
                multipart.addBodyPart(messageBodyPart);


                String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
                String filename = base + "/photo.jpg";

                messageBodyPart = new MimeBodyPart();
                DataSource fds = new FileDataSource(filename);

                messageBodyPart.setDataHandler(new DataHandler(fds));
                messageBodyPart.setHeader("Content-ID", "<image>");
                multipart.addBodyPart(messageBodyPart);   
                msg.setContent(multipart);


                // Send message
                Transport.send(msg);
                System.out.println("EMail Sent Successfully with image!!");
            }catch (MessagingException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }

暂无
暂无

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

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