在Xamarin.Forms应用中从Gmail发送电子邮件 [英] Sending e-mail from Gmail in Xamarin.Forms app
问题描述
我正在尝试使用Gmail从Xamarin Forms应用程序中发送电子邮件.
I am trying to send e-mail from within a Xamarin Forms app, using Gmail.
我只用一种方法创建了一个接口:SendEmail();
I have created an Interface with only 1 method: SendEmail();
然后,在Droid项目中,我添加了一个实现上述接口的类.使用Dependency属性并在主项目中获得方法的实现,一切都很好,除了以下错误:
Then, in the Droid project, I added a class which implements said interface. Using the Dependency attribute and getting the implementation of the method in the main project, all is fine, except the following error:
Could not resolve host 'smtp.gmail.com'
这是该方法的实际实现:
This is the actual implementation of the method:
string subject = "subject here ";
string body= "body here ";
try
{
var mail = new MailMessage();
var smtpServer = new SmtpClient("smtp.gmail.com", 587);
mail.From = new MailAddress("myEmailAddress@gmail.com");
mail.To.Add("anotherAddress@yahoo.com");
mail.Subject = subject;
mail.Body = body;
smtpServer.Credentials = new NetworkCredential("username", "pass");
smtpServer.UseDefaultCredentials = false;
smtpServer.EnableSsl = true;
smtpServer.Send(mail);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
四处搜寻,除了实际的smtp地址外,我找不到任何有关它的详细信息.
Searching around I could not find any details regarding it other that the actual smtp address.
此外,我使用了Google的不太安全的应用程序程序,未收到凭据错误,我认为它可以很好地连接到该帐户.
Also, I have used the Less Secure apps procedure from Google, not receiving a credentials error I assume that it can connect to the account just fine.
推荐答案
终于想通了!
首先,我使用的是Xamarin的Android Player,它显然不支持网络连接.
First of all, I was using Android Player by Xamarin, which apparently does not support network connectivity.
所以我的修复很容易:使用了Visual Studio Community 2015中内置的Android模拟器(针对该问题的任何版本),并使用James Montemagno(NuGet上的Xam.Plugin.Connectivity)插件测试了网络连接.
So my fix was easy: used an Android Emulator ( any version of it for that matter ) built in Visual Studio Community 2015, and tested network connectivity using the plugin by James Montemagno ( Xam.Plugin.Connectivity on NuGet ).
这篇关于在Xamarin.Forms应用中从Gmail发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!