使用MailKit在Xamarin.Forms中发送电子邮件时出现System.MissingMethodException

问题描述 投票:0回答:1

我正在尝试使用MailKit库在Xamarin.Forms中发送电子邮件。但是,当执行命中ConnectAsync()方法时,我收到以下异常:

System.MissingMethodException:发生string [] string.Split(char,System.StringSplitOptions)

有没有人遇到过这个。我不知道任何遗漏的方法。

以下是我在.net标准项目中的代码:


        using MailKit.Net.Smtp;
        using MimeKit;
        using MimeKit.Text;
        using System;
        using System.Collections.Generic;
        using System.Diagnostics;
        using System.Linq;
        using System.Threading.Tasks;

        public async Task SendAsync(EmailMessage emailMessage)
        {                                    
                    try
                    {
                        var message = new MimeMessage();

                        message.To.Add(new MailboxAddress("Recipient", "[email protected]"));
                        message.From.Add(new MailboxAddress("Sender", "[email protected]"));

                        message.Subject = "Test!!";  

                        message.Body = new TextPart(TextFormat.Html)
                        {
                            Text = "Test message"  
                        };  

                        using (var emailClient = new SmtpClient())
                        {
                            await emailClient.ConnectAsync("smtp.gmail.com", 465, true);

                            emailClient.AuthenticationMechanisms.Remove("XOAUTH2");

                            await emailClient.AuthenticateAsync("username", "password");

                            await emailClient.SendAsync(message);

                            emailClient.Disconnect(true);
                        }
                    }
                    catch (SmtpCommandException ex)
                    {
                        Debug.WriteLine($"Error sending email: {ex.Message}");
                        Debug.WriteLine($"StatusCode: {ex.StatusCode}");                
                    }
                    catch (SmtpProtocolException ex)
                    {
                        Debug.WriteLine($"Protocol error while sending email: {ex.Message}");
                    }  
    }

xamarin.forms mailkit
1个回答
0
投票

听起来像你的Xamarin.Forms项目正在拉动不匹配的.NET框架版本的库(可能是错误的MimeKit和/或MailKit框架版本)。

检查您的构建从nuget包中提取的MimeKit和MailKit程序集。如果您不熟悉nugets如何工作,每个nuget可以包含为不同框架构建的库的多个副本。

例如,MimeKit nuget具有为以下框架构建的程序集:

  • netstandard1.3
  • netstandard1.6
  • netstandard2.0
  • XA Marin.Android
  • Xamarin.iOS
  • net45
  • portable-net45 + win8(Profile7)
  • portable-net45 + win8 + wpa81(Profile111)

MailKit nuget包含为以下框架构建的程序集:

  • netstandard1.3
  • netstandard1.6
  • netstandard2.0
  • XA Marin.Android
  • Xamarin.iOS
  • net45
  • portable-net45 + win81 + wpa81(Profile32)

确保您的项目正在为两个nugets提供netstandard2.0程序集,而不是混合和匹配。

© www.soinside.com 2019 - 2024. All rights reserved.