【问题标题】:Sending email from inside unity3d从unity3d内部发送电子邮件
【发布时间】:2014-01-10 10:29:40
【问题描述】:

我正在尝试使用 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): 在 System.Net.Dns.GetHostByName (System.String hostName) [0 x00000] in :0 I/Unity (6256 ): 在 System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00000] in :0 I/Unity (6256): 在 System.Net.Dns.GetHostAddresses (System.String hostNameO rAddress) [0x00000] 在: 0 I/Unity(6256):在 System.Net.Sockets.TcpClient.Connect(System.String 主机名,Int32 端口)[0x00000] 在: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.MailMessage 消息)[0x00000] 中:0 I/Unity(6256):作为 SmtpException 重新抛出:消息无法发送。 I/Unity (6256): 在 System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMes sage 消息) [0x00000] in :0 I/Unity (6256): 在 FeedbackSender+c__Iterator1.MoveNext ()

我已经设法查明了该行的错误位置:smtpServer.Send(mail);

关于可能导致此问题的任何想法?我有安卓和 iOS 专业版。

干杯

【问题讨论】:

  • 也许它不是在寻找作为收件人的电子邮件,而是在您的联系人列表中寻找一个名字?只是一个疯狂的猜测,但你永远不会知道。

标签: c# android smtp unity3d


【解决方案1】:

我刚刚使用以下代码从 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 值是否正确 - 主机名、端口、用户名/密码。

【讨论】:

  • 如果完全相同的代码在 iOS 中无法完美运行,那将是合乎逻辑的步骤:/
  • 您也是从安卓设备发送的吗?这里的问题是我发布的代码在 iOS 上运行良好,但它抛出了一个在 android 上未知的主机。
【解决方案2】:

检查您是否在清单中具有 Internet 权限或您是否有网络连接。通常问题就这么简单。

【讨论】:

  • 这与互联网许可无关,但感谢您的建议。
【解决方案3】:

原来我正在将 SMTPClient 对象发送到另一个方法,然后该方法尝试使用该对象发送邮件。这就是导致错误的原因,因为一旦该方法创建了自己的 SMTPClient 对象,它就会停止抛出错误。

【讨论】:

  • 所以您的问题超出了您发布的原始代码?我遇到了非常相似的问题,尽管我似乎无法修复它。
猜你喜欢
  • 2017-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-07
  • 2018-06-09
  • 2016-08-28
  • 2016-03-16
  • 2012-12-16
相关资源
最近更新 更多