How to Send SMTP Email Using Xamarin.Forms without user interaction?

ZeyadAtefZeyadAtef EGMember ✭✭
edited March 2016 in Xamarin.Forms

Hello,

Can I send an email using Xamarin.Forms without user interaction? I tried using System.Net.Mail and MailKit and both are not working in xamarin.forms. MailKit cannot be added as a nuget package in the main project but can be added in the IOS and Android projects separately. Can I use an API for sending emails with the indie license? Any other alternatives?

Thank you.

Best Answers

  • SturgmeisterSturgmeister US ✭✭
    Accepted Answer

    I'm not sure if you'll find anything that "just works" in Xamarin.Forms. But if you found a library like MailKit that works on Android and iOS, then you can use an interface + dependency injection or a service locator to make it work in a Xamarin.Forms PCL. The built-in DependencyService has a good guide here: https://developer.xamarin.com/guides/xamarin-forms/dependency-service/introduction/, in case you decide to go that route.

  • devnldevnl NL ✭✭
    Accepted Answer

    Another option would be to just implement the mail stuff in a web service that sends the actual mails and call that. That way you're independent of the user and their devices.

Answers

  • SturgmeisterSturgmeister USUniversity ✭✭
    Accepted Answer

    I'm not sure if you'll find anything that "just works" in Xamarin.Forms. But if you found a library like MailKit that works on Android and iOS, then you can use an interface + dependency injection or a service locator to make it work in a Xamarin.Forms PCL. The built-in DependencyService has a good guide here: https://developer.xamarin.com/guides/xamarin-forms/dependency-service/introduction/, in case you decide to go that route.

  • ZeyadAtefZeyadAtef EGMember ✭✭
    edited March 2016

    @AlessandroCaliaro,

    I've already checked it before, did not find it helpful as I don't want users to send emails I want emails to be sent automatically. Also the last 3 or 4 replies in that forum concerning sending emails automatically weren't very helpful.

    Thank you anyway.

  • ZeyadAtefZeyadAtef EGMember ✭✭

    @Sturgmeister

    Thank you for the answer. I was thinking of going to that route but didn't know how. Seems like the only option available. I will check the the guide now, thank you.

  • devnldevnl NLUniversity ✭✭
    Accepted Answer

    Another option would be to just implement the mail stuff in a web service that sends the actual mails and call that. That way you're independent of the user and their devices.

  • RHudsonRHudson CAMember ✭✭✭

    Are you absolutely sure you want SMTP in your application? You should carefully rethink this nightmare.

    How will you prevent abuse?
    How will you handle bounces?
    What about compliance with the new DMARC rules?
    What SMTP settings will the app use, and are you willing to support your users so they can configure this?
    What if they're connected via WIFI to a provider that blocks port 25 (as most do now)?

    I got around these headaches by using a web service and put my email functionality server-side

  • ZeyadAtefZeyadAtef EGMember ✭✭

    @devnl

    I would go with the other solution only because I'm not familiar with how to create a web service and call it. If you could provide me with an example or a link I would be grateful. Thanks

  • ZeyadAtefZeyadAtef EGMember ✭✭

    @RobertHudson.9525, I was not really aware of all these problems, thank you for pointing them out. Like I just asked @devnl, would you please provide me with an example or a link explaining how to create a web service and call it using xamarin.forms? Thanks in advance.

  • AneeAnee USMember
    edited March 2016

    @ZeyadAtef ,

    Can check with SMTP. I already integrated in one of my application.
    MailMessage message = new System.Net.Mail.MailMessage();
    string toEmail = ToAddress;
    message.From = new MailAddress(fromEmail);
    message.To.Add(toEmail);
    message.Subject = Subject;
    message.Body = MsgBody;
    message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

                    foreach (var files in FileNameLst)
                    {
    
                        // Create  the file attachment for this e-mail message.
                        Attachment data = new Attachment(files, MediaTypeNames.Application.Octet);
                        // Add time stamp information for the file.
                        ContentDisposition disposition = data.ContentDisposition;
                        disposition.CreationDate = System.IO.File.GetCreationTime(files);
                        disposition.ModificationDate = System.IO.File.GetLastWriteTime(files);
                        disposition.ReadDate = System.IO.File.GetLastAccessTime(files);
                        // Add the file attachment to this e-mail message.
                        message.Attachments.Add(data);
                    }
    
                SmtpClient smtpClient = new SmtpClient("smtp.sendgrid.net", 25);
                smtpClient.EnableSsl = true;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = new NetworkCredential(fromEmail, fromPW);
                smtpClient.Send(message);
    
  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    SmtpClient smtpClient = new SmtpClient("smtp.sendgrid.net", 25);

    Keep in mind that sendgrid.net is not free.

  • chetanpskumarchetanpskumar USMember
    edited April 2017

    Hi , i was trying send email with xam.plugins.messaging . but emailMessenger.CanSendEmail is always showing false
    var emailMessenger = MessagingPlugin.EmailMessenger;
    try {
    if (emailMessenger.CanSendEmail)
    {
    // Send simple e-mail to single receiver without attachments, bcc, cc etc.
    emailMessenger.SendEmail("[email protected]", "Xamarin Messaging Plugin", "Well hello there from Xam.Messaging.Plugin");

                    // Alternatively use EmailBuilder fluent interface to construct more complex e-mail with multiple recipients, bcc, attachments etc. 
                    var email = new EmailMessageBuilder()
                      .To("[email protected]")
                      .Cc("[email protected]")
                      .Bcc(new[] { "[email protected]", "[email protected]" })
                      .Subject("Xamarin Messaging Plugin")
                      .Body("Well hello there from Xam.Messaging.Plugin")
                      .Build();
    
                    emailMessenger.SendEmail(email);
    
                }
            }catch(Exception Ex)
            {
                DisplayAlert("Alert", Ex.ToString(), "ok");
            }
    

    Hope i can get solved on this .. tnx in advance .....

  • RafeeqRafeeq USMember ✭✭

    Can I know How can we use SMTP for automatically sending Email? Is it better way to use SMTP to send Email Automatically.Thank You

  • CaioshinnCaioshinn USMember ✭✭
    edited January 2018

    Ok guys, I just checked some differents solutions because I have the same need to send email from my app.
    Now, after some times to evaluate the situation, trying some components available in nuget or github, I can tell that there is almost 1 solution that works for sure.

    Briefly:
    1. Mailkit doesn't work for me, it immediately crashes when creating an object of MimeMessage type on Android.
    2. Seems that Messaging Plugin for Xamarin and Windows is only able to send email using the default client of OS, and this is not my requirement
    3. Using the standard System.Net.Mail namespace works good for me: I tested it successfully both from Android and iOS, it doesn't require any specific platform implementation, and it works good using standard authentication (on port 25) and also with SSL (only if the server support implicit SSL, tipically on port 587 like gmail)

  • MD1MD1 USMember ✭✭

    @Caioshinn - Could you please share the code which you coded using System.Net.Mail namespace. Appreciate your quick response in this regard.

Sign In or Register to comment.