C# 从unity3d内部发送电子邮件
我正在尝试使用SmtpClient类在智能手机上从unity内部发送电子邮件。我已经在iOS上完成了这个相当简单的任务,但是完全相同的代码在android上不起作用。代码如下:C# 从unity3d内部发送电子邮件,c#,android,smtp,unity3d,C#,Android,Smtp,Unity3d,我正在尝试使用SmtpClient类在智能手机上从unity内部发送电子邮件。我已经在iOS上完成了这个相当简单的任务,但是完全相同的代码在android上不起作用。代码如下: private IEnumerator sendAutoMail (string textBody,string title) { MailMessage mail = new MailMessage(); mail.From = new MailAddress(sender); mail.To.
private IEnumerator sendAutoMail (string textBody,string title)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress(sender);
mail.To.Add(receiver);
mail.Subject = "R4UL " + title;
mail.Body = textBody;
Debug.Log("Connecting to SMTP server");
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
smtpServer.Port = 587;
smtpServer.Credentials = new System.Net.NetworkCredential(sender, password) as ICredentialsByHost;
smtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
};
Debug.Log("Sending message");
smtpServer.Send(mail);
yield return null;
}
其中发件人、密码和收件人分别是我的Gmail帐户、我的Gmail密码和我希望发送电子邮件的人
因此,这在iOS上非常有效,但在android上会抛出以下错误:
I/Unity(6256):SocketException:没有已知的主机I/Unity(6256):在System.Net.Dns.GetHostByName(System.String主机名)[0 x00000]中:0 I/Unity(6256):在System.Net.Dns.GetHostEntry(System.String主机名或添加ress)[0x00000]中:0 I/Unity(6256):在System.Net.Dns.GetHostAddresses(System.String主机名)中[0x00000]in:0 I/Unity(6256):at System.Net.Sockets.TcpClient.Connect(System.String主机名,Int32端口)[0x00000]in:0 I/Unity(6256):at System.Net.Sockets.TcpClient..ctor(System.String主机名,Int32端口)[0x00000]in:0 I/Unity(6256):at System.Net.Mail.SmtpClient.SendInternal(System.Net.Mail.MailMessage)[0x00000]in:0 I/Unity(6256):在System.Net.Mail.SmtpClient.Send(System.Net.Mail.MailMes sage message)[0x00000]in:0 I/Unity(6256):作为SmtpException重新发送:无法发送消息。I/Unity(6256):在System.Net.Mail.SmtpClient.Send(System.Net.Mail.MailMes sage message)[0x00000]中:0 I/Unity(6256):在FeedbackSender+c__迭代器1.MoveNext()中
我已设法在以下行中找到错误位置:smtpServer.Send(mail)代码>
关于是什么导致了这个问题,有什么想法吗?我有android和iOS pro两个版本
干杯我刚刚用以下代码成功地从Unity 3D发送了一封电子邮件:
using System.Net;
using System.Net.Mail;
using UnityEngine;
public class SendMail : MonoBehaviour {
public string sender = "me@mymailaccount.com";
public string receiver = "me@mymailaccount.com";
public string smtpPassword = "mysmtppassword";
public string smtpHost = "mail.mymailacount.com";
// Use this for initialization
private void Start() {
using (var mail = new MailMessage {
From = new MailAddress(sender),
Subject = "test subject",
Body = "Hello there!"
}) {
mail.To.Add(receiver);
var smtpServer = new SmtpClient(smtpHost) {
Port = 25,
Credentials = (ICredentialsByHost)new NetworkCredential(sender, smtpPassword)
};
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
smtpServer.Send(mail);
}
}
}
用真实数据替换明显的位。。。它与您的没有什么不同,但我的邮件服务器不需要完全的SSL身份验证,因此我删除了它
我没有看到您以前遇到的错误,因此我唯一能建议的是检查并再次检查您的SMTP值是否正确—主机名、端口、用户名/密码。检查您是否在清单中具有internet权限或您是否具有网络连接。通常问题就这么简单 原来我是在将SMTPClient对象发送到另一个方法,然后该方法尝试使用该对象发送邮件。这就是导致错误的原因,因为该方法一创建自己的SMTPClient对象,它就停止抛出错误。也许它不是在寻找作为收件人的电子邮件,而是在您的联系人列表中查找姓名?这只是一个猜测,但你永远也不知道。这将是一个合乎逻辑的步骤。同样的代码在iOS中不会完美地工作吗?/你是从安卓设备发送的吗?这里的问题是,我发布的代码在iOS上运行良好,但在android上抛出了一个未知主机。这与互联网许可无关,但感谢您的建议。那么您的问题不在您发布的原始代码范围内?我有着非常相似的问题,尽管我似乎无法修复它。