我正在尝试使用 SmtpClient 类在智能手机上从内部 Unity 发送电子邮件。我已经在 iOS 上完成了这个相当简单的任务,但完全相同的代码在 Android 上不起作用。这是代码:
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): at System.Net.Dns.GetHostByName (System.String hostName) [0 x00000] in :0 I/Unity ( 6256): at System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00000] in :0 I/Unity ( 6256): at System.Net.Dns.GetHostAddresses (System.String hostNameOrAddress) [0x00000] in :0 I/ Unity(6256):位于System.Net.Sockets.TcpClient.Connect(System.String主机名,Int32端口)[0x00000] in:0 I/Unity(6256):位于System.Net.Sockets.TcpClient..ctor( System.String主机名,Int32端口)[0x00000]在:0 I/Unity(6256):在System.Net.Mail.SmtpClient.SendInternal(System.Net.Mail.MailMessage消息)[0x00000]在:0 I/ Unity(6256):在System.Net.Mail.SmtpClient.Send(System.Net.Mail.MailMes sage消息)[0x00000]中:0 I/Unity(6256):重新抛出SmtpException:无法发送消息。 I / Unity(6256):在System.Net.Mail.SmtpClient.Send(System.Net.Mail.MailMes sage消息)[0x00000]中:0 I/Unity(6256):在FeedbackSender + c__Iterator1.MoveNext()
我已经设法查明该行的错误位置:smtpServer.Send(mail);
关于可能导致此问题的原因有什么想法吗?我有 Android 和 iOS 专业版。
Cheers
我刚刚使用以下代码成功从 Unity 3D 发送了一封电子邮件:
using System.Net;
using System.Net.Mail;
using UnityEngine;
public class SendMail : MonoBehaviour {
public string sender = "[email protected] /cdn-cgi/l/email-protection";
public string receiver = "[email protected] /cdn-cgi/l/email-protection";
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 值是否正确 - 主机名、端口、用户名/密码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)