Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Cannot use MailKit in Xamarin: System.NotSupportedException: No data is available for encoding 51936

$
0
0

I am using MailKit in my Xamarin application.
I have a mailsender class in my android project and on the line;

public MimeMessage EmailMessage = new MimeMessage()

I get the error message:

System.NotSupportedException: System.NotSupportedException: No data is available for encoding 51936. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.

I am using Dependency Injection although I am wondering if I need to do this. Will the code work in my .net standard library? If I can getthis code to work it looks like it will be the same in iOS.

My MailSender class;

using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
using Sir.Mobile.Data;
using Sir.Mobile.Data.Interfaces;
using Sir.Mobile.Helpers;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Android.Content;
using Sir.Mobile.Droid.DependencyServices;
using Xamarin.Forms;

[assembly: Dependency(typeof(MailSender))]

namespace Sir.Mobile.Droid.DependencyServices
{
public class MailSender : Java.Lang.Object, IMailSender
{
public MimeMessage EmailMessage = new MimeMessage();
public static string SmtpServerName => Constants.SmtpServerName;
public static string EmailFrom => Constants.EmailFrom;
public static string ErrorEmailTo = Constants.ErrorEmailTo;
private static int PortNo => 587;

    public MailSender()
    {
    }

    public async Task SendMailAsync(string to, string subject, string body)
    {
        EmailMessage.To.Add(to.ToMail(""));
        await SendEmailAsync(subject, body);
    }

    public async Task SendMailAsync(List<string> toList, string subject, string body)
    {
        if (toList == null)
        {
            throw new NullReferenceException("Mail TO list is not set");
        }

        SetToList(toList);
        await SendEmailAsync(subject, body);
    }

    public async Task SendMailAsync(string to, List<string> ccList, string subject, string body)
    {
        SetCcList(ccList);
        await SendMailAsync(to, subject, body);
    }

    public async Task SendMailAsync(List<string> toList, List<string> ccList, string subject, string body)
    {
        if (toList == null)
        {
            throw new NullReferenceException("Mail TO list is not set");
        }

        SetCcList(ccList);
        SetToList(toList);
        await SendEmailAsync(subject, body);
    }

    private async Task SendEmailAsync(string subject, string body)
    {
        EmailMessage.From.Add(string.IsNullOrWhiteSpace(StaticData.SelectedUser.FullName)
            ? EmailFrom.ToMail("")
            : EmailFrom.ToMail(StaticData.SelectedUser.FullName));

        EmailMessage.Subject = subject;
        var builder = new BodyBuilder { TextBody = body };
        EmailMessage.Body = builder.ToMessageBody();

        await SendEmailAsync();
    }

    public async Task SendErrorMailAsync(Exception ex)
    {
        EmailMessage.From.Add(string.IsNullOrWhiteSpace(StaticData.SelectedUser.FullName)
            ? EmailFrom.ToMail("")
            : EmailFrom.ToMail(StaticData.SelectedUser.FullName));

        EmailMessage.To.Add(Constants.ErrorEmailTo.ToMail(""));
        EmailMessage.Subject = $"Exception: {ex.Message}";
        var builder = new BodyBuilder { TextBody = ex.ToString() };
        EmailMessage.Body = builder.ToMessageBody();
        await SendEmailAsync();
    }

    private void SetCcList(IEnumerable<string> ccList)
    {
        foreach (var cc in ccList)
        {
            EmailMessage.Cc.Add(cc.ToMail(""));
        }
    }

    private void SetToList(IEnumerable<string> toList)
    {
        foreach (var to in toList)
        {
            EmailMessage.To.Add(to.ToMail(""));
        }
    }

    private async Task SendEmailAsync()
    {
        using (var client = new SmtpClient())
        {
            await client.ConnectAsync(SmtpServerName, PortNo, SecureSocketOptions.None).ConfigureAwait(false);
            await client.SendAsync(EmailMessage).ConfigureAwait(false);
            await client.DisconnectAsync(true).ConfigureAwait(false);
        }
    }
}

}


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>