Send Email as background Task when called instead of taking you to Email client

Riffy 276 Reputation points
2022-02-06T19:19:37.98+00:00

Hi

I am using the Email function from Xamarins.Essentials, but it is being called interactively as follows:

171665-email.png

What I would like to happen is for the email to run as await background task, and return a code for fail/success:

My code is as per standard xamarin essentials code is as follows:

public async Task SendEmailAsync(string subject, string body, List<string> SendTo)  
        {  
            try  
            {  
                var message = new EmailMessage  
                {  
                    Subject = "Hello",  
                    Body = body,  
                    To = SendTo,  
                    BodyFormat = EmailBodyFormat.PlainText  
                };  
                await Email.ComposeAsync(message);  
            }  
            catch (FeatureNotSupportedException fbsEx)  
            {  
                // Email is not supported on this device  
            }  
            catch (Exception ex)  
            {  
                // Some other exception occurred  
            }  
        }  

I have checked online as well as MS Q&A, and there appears to be no suggestions for this.

Any suggestions would be greatly appreciated.

Thanks

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,315 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 70,966 Reputation points Microsoft Vendor
    2022-02-07T07:22:53.05+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Xamarin.Essentials: Email enables an application to open the default email application, Xamarin.Essentials use Platform.AppContext.StartActivity(intent); to open email application

    Here is Xamarin.Essentials: Email source code. Please check it

    You can try to use System.Net.Mail.MailMessage to send the email by SMTP( SMTP provides a set of protocols that simplifies the communication of email messages between email servers. ) in the background without to open the email application.

    You can try to this Example code of System.Net.Mail.MailMessage in your project.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful