我正在尝试使用SmtpClient类从智能手机内部发送一封电子邮件。我已经在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):在System.Net.Dns.GetHostByName(System.String hostName)[0 x00000] in:0 I / Unity(6256) ):在System.Net.Dns.GetHostEntry(System.String hostNameOrAdd ress)[0x00000] in:0 I / Unity(6256):at System.Net.Dns.GetHostAddresses(System.String hostNameO rAddress)[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 hostna me,Int32 port)[0x00000] in:0 I / Unity(6256):at System.Net.Mail.SmtpClient.SendInternal(System.Net.Mail .MailMessage message)[0x00000] in: 0 I / Unity(6256):在System.Net.Mail.SmtpClient.Send(System.Net.Mail.MailMes sage message)[0x00000] in:0 I / Unity(6256):Rethrow as SmtpException:Message not not be发送。 I / Unity(6256):在System.Net.Mail.SmtpClient.Send(System.Net.Mail.MailMes sage message)[0x00000] in:0 I / Unity(6256):在FeedbackSender + c__Iterator1.MoveNext()< / p>
我设法在行smtpServer.Send(mail);
关于可能导致此问题的任何想法?我有Android和iOS专业版。
干杯
答案 0 :(得分:0)
我刚刚使用以下代码成功发送了来自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值是否正确 - 主机名,端口,用户名/密码。
答案 1 :(得分:0)
检查清单中是否有互联网权限,或者您是否有网络连接。通常问题就是这么简单。
答案 2 :(得分:0)
原来我将SMTPClient对象发送到另一个方法,然后尝试使用该对象发送邮件。这是导致错误的原因,因为一旦该方法创建了自己的SMTPClient对象,它就会停止抛出错误。