c# - 如何向我的项目添加确认电子邮件和短信?
问题描述
我正在为学校项目开发这个应用程序。我是新手,在 Xamarin 和 C# 方面没有太多经验。在我的应用程序中,我可以在提交表单后作为应用程序所有者收到消息。我还需要添加用户或客户在提交表单后收到确认文本和电子邮件的功能。任何帮助,将不胜感激。
这是 Xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ICC.RequestServicesPage">
<ScrollView>
<StackLayout Padding="50,50,50,50">
<Entry x:Name="EntryFName"
Placeholder="First Name"
Keyboard="Text" />
<Entry x:Name="EntryLName"
Placeholder="Last Name"
Keyboard="Text" />
<Entry x:Name="EntryEmail"
Placeholder="Email address"
Keyboard="Email" />
<Entry x:Name="EntryPhone"
Placeholder="Phone number"
Keyboard="Telephone" />
<Button Text="Submit"
VerticalOptions="CenterAndExpand"
HorizontalOptions="Center"
FontSize="Large"
Clicked="OnClickSubmit" />
</StackLayout>
</ScrollView>
</ContentPage>
这是C#页面
namespace COOP
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class RequestServicesPage : ContentPage
{
public RequestServiceFormPage(string service, DateTime date)
{
InitializeComponent();
}
async void OnClickSubmit(object sender, EventArgs e)
{
//build the message body from the form entries
//Phone and Address2 are optional, so only add if they are not null
bool isValid = true;
string fName = EntryFName.Text;
string lName = EntryLName.Text;
string email = EntryEmail.Text;
string phone = EntryPhone.Text;
if (email == null || email == "")
{
isValid = false;
EntryEmail.Placeholder = "Email address *";
EntryEmail.PlaceholderColor = Red;
}
else if (!IsValidEmail(email))
{
isValid = false;
EntryEmail.Placeholder = "Enter a valid Email";
EntryEmail.PlaceholderColor = Red;
EntryEmail.Text = "";
}
if (phone == null || phone == "")
{
isValid = false;
EntryPhone.Placeholder = EntryPhone.Placeholder + " *";
EntryPhone.PlaceholderColor = Red;
}
else if (!IsValidPhone(phone) && phone != null && phone != "")
{
isValid = false;
EntryPhone.Placeholder = "Enter a Valid Phone Number";
EntryPhone.PlaceholderColor = Red;
EntryPhone.Text = "";
}
//build the message body from the form entries
if (isValid)
{
//add the email address and text address to send the email to
List<string> address = new List<string>();
address.Add("test@gmail.com");
address.Add("9999999999@vtext.com");
//build the message body from the form entries
string message = "";
message += "New " + requestService + " Request from: \n";
message += EntryFName.Text + " " + EntryLName.Text + "\n";
message += EntryEmail.Text + "\n";
message += EntryPhone.Text + "\n";
message += EntryAddress1.Text + "\n";
//call send email function. function body is below.
await SendEmail("Test", message);
}
}
public static bool IsValidPhone(string phoneNumber)
{
string numbersOnly = RemoveNonNumeric(phoneNumber);
if (numbersOnly.Length == 7 || numbersOnly.Length == 10)
{
return true;
}
else
{
return false;
}
}
public static string RemoveNonNumeric(string phone)
{
return Regex.Replace(phone, @"[^0-9]+", "");
}
public static bool IsValidEmail(string email)
{
string pattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|" + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)" + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";
var regex = new Regex(pattern, RegexOptions.IgnoreCase);
return regex.IsMatch(email);
}
public async Task SendEmail(string subject, string body, List<string> recipients)
{
try
{
var message = new EmailMessage
{
Subject = subject,
Body = body,
To = recipients,
};
await Email.ComposeAsync(message);
}
}
}
}
解决方案
推荐阅读
- php - 使用 php/guzzle 从 uri 获取 Json
- powerbi - Power BI API - 如何从 app.powerbi.com 获取报告?
- sql - 查找前 N 个最频繁的类别以及每个类别的前 N 个最频繁的子类别
- c# - Linq 表达式树编译非平凡的对象常量并以某种方式引用它们
- list - OCaml 检查元组右侧是否存在元素
- angular - 如何在 Angular 6 中使用 ngModel 时默认选择标签中的值?
- mysql-workbench - 如何将空间数据导入 mysql 8 (Mysql WorkBench 8.0)
- c++ - EthernetInterface 实例中断程序
- sql-server - 多列重叠日期
- php - Checking an array value that isn't a number