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

When sending e-mail on iOS device using Xamarin Essentials, it fails to send the attachment #1920

Open vsfeedback opened 3 years ago

vsfeedback commented 3 years ago

This issue has been moved from a ticket on Developer Community.


I try to use Email.ComposeAsync from Xamarin Essentials to send an e-mail on a iOS phone. When I send an e-mail, although I attach an file using Atachment.Add() method, it is sent without the attachment.

Steps to reproduce:

What is expected to happen: e-mail to be sent with attachment

What happens: e-mail is sent without attachment

I have tried with both Outlook app and Gmail app on the iOS device with same result. Phone details: iPhone XS, OS version 14.6

================ NuGet packages used (all latest) ================ NETStandard.Library 2.0.3 Xamarin.Essentials 1.7.0 Xamarin.Forms 5.0.0.2083

================ contents of MainPage.xaml.cs ================ using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Essentials; using Xamarin.Forms;

namespace emailTest { public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); }

private void sendEmail_Clicked(object sender, EventArgs e) { EmailTest ET = new EmailTest(); ET. SendEmail("subject", "hello world", new List {"myemail.com"}); //I changed my e-mail so is not public }

public class EmailTest { public async Task SendEmail(string subject, string body, List recipients) { try { var message = new EmailMessage { Subject = subject, Body = body, To = recipients, //Cc = ccRecipients, //Bcc = bccRecipients };

//add attachment var fn = "Attachment.txt"; var file = Path.Combine(FileSystem.CacheDirectory, fn); File.WriteAllText(file, "Hello World"); message. Attachments.Add(new EmailAttachment(file));

//send email await Email.ComposeAsync(message); } catch (FeatureNotSupportedException fbsEx) { // Email is not supported on this device } catch (Exception ex) { // Some other exception occurred } } } } }

================ contents of MainPage.xaml ================ <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="emailTest.MainPage">

retrohead commented 3 years ago

I am having this same issue where attachments are not added but only when using outlook. Using the default email application works but this is not suitable in all situations as some companies restrict access to the default email application.

MSicc commented 2 years ago

Same here... it gets added to Outlook if I am using the Share menu and select Outlook manually, however.

Magic73 commented 2 years ago

Same here, for UWP, Android and iOS.

Edit: on UWP, this could be the issue: https://stackoverflow.com/questions/33053358/sending-email-attachments-via-uwp-emailmanager-not-working and inside there's a workaround (building a .msg file and open it with the launcher)

marwalsch commented 2 years ago

Can confirm this for iPhone 6s/iOS 14 and iPhone 12 pro/iOS 15.

It fails exclusively when Outlook (and probably other third party clients as well) is set as the default Mail-program and fallen back to. I actually had to uninstall Mail for this to happen as it seems the API prefers the Mail app anyway.

As @MSicc observed using Outlook through the share API is never an issue.

Crem78 commented 1 year ago

I too am having problems testing the app with iOS 16.2

From my app I launch the mail with an attachment: ok with "Mail" but there is no attachment with "Gmail" or "Outlook"

marwalsch commented 1 year ago

I understand this by design(?).

If MFMailComposeViewController is unable to be opened, it falls back to URL compose. I suppose a workaround might be to remove this fallback and force the user to use the regular Mail app. Haven't tested this though.

Not sure if this is on Apple's side for allowing attachments through the Share API, but no third party Mail clients through other means but URL compose.

mbae23 commented 1 year ago

Can somebody from the project please process this ticket? So we can know whether there is a chance that something is done about this issue. But I don't think there is a chance, because I agree with @marwalsch after analyzing bug reports from our customers and looking at the code linked by him/her. When this problem occurs, MFMailComposeViewController.canSendMail() must have returned false because only a third party mail app is in use. Xamarin.Essentials tries a mailto URL which does not allow attachments. It also doesn't allow a mail body in HTML, trying to pass one throws a FeatureNotSupportedException. I think it's a bug that the attachment is silently omitted instead of also throwing this exception.