xamarin / Essentials

Xamarin.Essentials is no longer supported. Migrate your apps to .NET MAUI, which includes Maui.Essentials.
https://aka.ms/xamarin-upgrade
Other
1.52k stars 505 forks source link

Email Html body missing in Android Platform #620

Closed minaairsupport closed 5 years ago

minaairsupport commented 5 years ago

Bug report best practices: Submitting Issues

Description

I tried to send Email with your library , it was nice in iOS but in android I have missing body that happen incase body html

 public async Task SendEmail(string subject, string body, List<string> recipients)
        {
            try
            {
                var message = new EmailMessage
                {
                    Subject = subject,
                    Body = body,
                    To = recipients,
                    BodyFormat = EmailBodyFormat.Html,
                    //Cc = ccRecipients,
                    //Bcc = bccRecipients
                };
                await Email.ComposeAsync(message);
            }
            catch (FeatureNotSupportedException fbsEx)
            {
                Crashes.TrackError(fbsEx);
                // Email is not supported on this device
            }
            catch (Exception ex)
            {
                Crashes.TrackError(ex);
                // Some other exception occurred
            }
        }

Steps to Reproduce

  1. just call method above and you will get empty body with these params
    SendEmail("subject", "<p> Test body</p>", new List<string>(){jeg@fdngj.com});

in iOS you will find Email with body , Android Empty Body

Expected Behavior

Html body

Actual Behavior

Empty body

Basic Information

Screenshots

Reproduction Link

VS bug #735687

jamesmontemagno commented 5 years ago

I was not able to reproduce this with GMail on my Pixel XL 2.

Showed up just fine. Need more information such as device and email client.

minaairsupport commented 5 years ago

James, I update the issue , the Email client is Outlook

mattleibow commented 5 years ago

Wow... don't tell me outlook requires a special item in the intent. This is really going to be annoying. We probably need to try a few clients.

minaairsupport commented 5 years ago

yea I notice this now , when I change the email client to gmail body appear , when I back to outlook the body disappear again . when I change BodyFormat to PlainText its working in outlook so the issue appear only when you send html body but let's think what special about outlook , may be it block html body ?

jamesmontemagno commented 5 years ago

It could simply be that they don't support it. PlainText is the default and recommended route to be honest. We can report it to the outlook team. Probably need to document it, but most likely out of our control. Will get in contact with the Outlook team though.

minaairsupport commented 5 years ago

I don't believe either OutLook docent support html body https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem.htmlbody I think we missing something I have reviewed the source code and it seems working nice I make some tries may be we could fix the issue

Mrnikbobjeff commented 5 years ago

@minaairsupport Have you tried not only using a text element tag but to include your and their corresponding end tags? In the outlook documentation they have this in their sample code. When I tried setting outlook up it crashed and launched some settings on Android so I could not reproduce this :(

minaairsupport commented 5 years ago

I don't get this sorry , I think the only place I can send my E-mail body is Body property in EmailMessage did you advise me to do something else ?

minaairsupport commented 5 years ago

Thanks Redth for take care of my issue This may be help I think we need to tell the E-mail client what type of body so it may be good idea to add this Intent.ExtraHtmlText

if (!string.IsNullOrEmpty(message?.Body))
            {
                if (message?.BodyFormat == EmailBodyFormat.Html)
                {
                    ISpanned html;
                    if (Platform.HasApiLevel(BuildVersionCodes.N))
                    {
                        html = Html.FromHtml(message.Body, FromHtmlOptions.ModeLegacy);
                    }
                    else
                    {
#pragma warning disable CS0618 // Type or member is obsolete
                        html = Html.FromHtml(message.Body);
#pragma warning restore CS0618 // Type or member is obsolete
                    }
                    intent.PutExtra(Intent.ExtraText, html);
                    intent.PutExtra(Intent.ExtraHtmlText, html); //   <= Add this Extra html
                }
                else
                {
                    intent.PutExtra(Intent.ExtraText, message.Body);
                }
            }

References https://www.w3.org/Protocols/rfc1341/7_3_Message.html https://stackoverflow.com/questions/15814902/sending-html-mail-if-app-allows https://developer.android.com/reference/android/content/Intent.html#EXTRA_HTML_TEXT https://developer.xamarin.com/api/field/Android.Content.Intent.ExtraHtmlText/

Mrnikbobjeff commented 5 years ago

No I did not advise you to do something else. Just include the proper HTML start and end tags as well as a body tag in your E-Mail body. That is the string you pass as body would be something like "<html><body><p>This is a test</p></body></html>"

minaairsupport commented 5 years ago

I tried this as well it dosent work I will be waiting for you solve this issue , I make all possible tries

Redth commented 5 years ago

@jamesmontemagno did you ever hear from the Outlook team if this is something that they should support or not?

jamesmontemagno commented 5 years ago

So, this is out of our control and not supported by the outlook application. Nothing we can really do about it to be honest. Just need to document this correctly.

minaairsupport commented 5 years ago

Could you at least address outlook team to be notified and I will still looking for solution

jamesmontemagno commented 5 years ago

I have already notified them and they are looking into adding support for it.

minaairsupport commented 5 years ago

walk-around to fix the issue till they support it convert this HTML to plain text

https://gist.github.com/minaairsupport/b14b3aeb59e64ebf41dcf5a0af0fe161

ish-1313 commented 5 years ago

hit the same issue couple of days. Samsung s7 edge with oreo.
if trying to send email with html body outlook - no body at all gmail - stripped some tags . for instance - < table >< t* > Samsung email - no body at all

after googling on that matter found, that this problem is not realted to xamarin essentials but to email clients and even in concrete mail client may vary from version to version. in my opinion it worth to put this in documentation, that html bodies handling by clients may vary from completely ignoring html emails to striping various html tags . at least it will save some time ;)

minaairsupport commented 5 years ago

I agree at least to be documented and my walk around could help as well its the same effect like Gmail

jamesmontemagno commented 5 years ago

Yes, I am adding: Not all email clients for Android support Html, since there is no way to detect this we recommend using PlainText when sending emails.

merlinsmike commented 5 years ago

Late to this party but … I would like to use this package but really need a nice HTML e-mail to result, and on Android even Gmail is not really working with HTML with this package. on IOS it seems good, at least for Gmail.

A trivial sample with a bit of color and bold: mail.Body = "<body

Hi this is Mike! <span style=\"color: #ff0000;\"> In Red! Bold...

";

mail.BodyFormat = EmailBodyFormat.Html;

Sends the e-mail and looks like "Hi this is Mike! In Red! Bold...". No red or bold but close! My rather more complex real HTML comes out as gibberish.

Note that when I use System.Net.Mail.SmtpClient the HTML is always rendered correctly at the destination e-mail client. There I use an alternate view -

// Create HTML view AlternateView HTMLEmail = AlternateView.CreateAlternateViewFromString(body, null, "text/html");

// add image to html version of email HTMLEmail.LinkedResources.Add(logo); mail.AlternateViews.Add(HTMLEmail);

Of course this is quite different approach. Any help will surely be appreciated! And jamesmontemagno, I am using many of your fine packages successfully, and you have been a real asset to the community!

Mike