我正在尝试在我的 xamarin 表单项目中发送电子邮件,我已经在 iPhone 模拟器和 iPhone 设备上进行了尝试。当我按下 iPhone 上的发送电子邮件按钮时,没有任何反应,甚至没有出现调试错误。我还确保我在设备上使用我的电子邮件登录。
我使用了 serviceDependency 并按照此链接中的设置进行操作: https://developer.xamarin.com/recipes/ios/shared_resources/email/send_an_email/
我的界面:
public interface InterfaceEmail
{
void sendEmail();
}
iOS 实现:
[assembly: Xamarin.Forms.Dependency(typeof(SendEmail))]
namespace myProject.iOS
{
public partial class SendEmail : InterfaceEmail
{
MFMailComposeViewController mailController;
public SendEmail() {}
public void sendEmail()
{
if (MFMailComposeViewController.CanSendMail)
{
mailController = new MFMailComposeViewController();
mailController.SetToRecipients (new string[] {"my@email.com"});
mailController.SetSubject ("test mail");
mailController.SetMessageBody ("This is a test", false);
mailController.Finished += (object sender, MFComposeResultEventArgs e) =>
{
Console.WriteLine(e.Result.ToString());
e.Controller.DismissViewController(true, null);
};
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(mailController, true, null);
}}}}
在我的共享代码中实现:
async void Handle_ToolbarButton(object sender, System.EventArgs e)
{
var action = await DisplayActionSheet("What do you want to do?", "Abort", null, "Send email");
if(action == "Send email")
{
DependencyService.Get<InterfaceEmail>().sendEmail();
}
}
有没有人知道这里可能出了什么问题?
最佳答案
为了更好地发送电子邮件而无需编写特定于平台的代码,请将此 nuget 安装到您的解决方案中 xam.plugin.Messaging( https://www.nuget.org/packages/Xam.Plugins.Messaging/ )
然后在PCL中编写下面的代码
var email = new EmailMessageBuilder()
.To("to.plugins@xamarin.com")
.Subject("Xamarin Messaging Plugin")
.Body("Well hello there from Xam.Messaging.Plugin")
.Build();
您还可以添加附件。更多详情请浏览https://github.com/cjlotz/Xamarin.Plugins/blob/master/Messaging/Details.md
关于c# - 以 xamarin 形式发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43694998/