【问题标题】:System.MissingMethodException When Sending Email using MailKit In Xamarin.Forms在 Xamarin.Forms 中使用 MailKit 发送电子邮件时出现 System.MissingMethodException
【发布时间】:2020-04-22 17:24:55
【问题描述】:

我正在尝试使用 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", "xamapp@icloud.com"));
        message.From.Add(new MailboxAddress("Sender", "xamapp.no.reply@gmail.com"));

        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】:

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

    检查以查看您的构建从 nuget 包中提取了哪些确切的 MimeKit 和 MailKit 程序集。如果您不熟悉 nuget 的工作原理,每个 nuget 都可以包含为不同框架构建的库的多个副本。

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2019-10-14
      • 2023-03-18
      • 2017-02-17
      • 1970-01-01
      • 2015-09-21
      • 1970-01-01
      • 2017-01-30
      • 2020-04-08
      • 1970-01-01
      相关资源
      最近更新 更多