在xamarin.form中,如何使用xamarin.form发送短信?

时间:2019-11-19 09:31:16

标签: c# xamarin xamarin.forms xamarin.android xamarin.ios

我是xamarin.form的新手,我想自动将短信发送到电话号码。有免费的解决方案吗?我正在做一个简单的项目。

2 个答案:

答案 0 :(得分:3)

要使用Xamarin.Forms发送消息,您需要使用nuget软件包管理器中的 Xamarin.Essentials 插件

发送消息的代码

public async Task SendSms(string messageText, string recipient)
{
    var message = new SmsMessage(messageText, new []{ recipient });
    await Sms.ComposeAsync(message);
}

有关更多信息,请访问 https://docs.microsoft.com/en-us/xamarin/essentials/sms

答案 1 :(得分:0)

在nuget包中添加Xamarin.Essentials,然后尝试以下代码:

public async Task SendSms(string messageText, string recipient) 
{
    try
    {
        var message = new SmsMessage(messageText, new []{ recipient });
        await Sms.ComposeAsync(message);
    }
    catch (FeatureNotSupportedException ex)
    {
        // Sms is not supported on this device.
    }
    catch (Exception ex)
    {
        // Other error has occurred.
    }
}

如果必须发送给多个收件人,请将参数“字符串收件人”更改为一组收件人“字符串[]收件人”。 让我知道您是否需要进一步的帮助。