Sending email from inside unity3d

2019-03-29 08:14发布

问题:

I am trying to send an email from inside unity on smartphones using the SmtpClient class. I have achieved this quite simple task on iOS but the exact same code does not work on android. Here's the code:

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;
}

Where sender, password and receiver are my Gmail account, my Gmail password and the person I wish to email respectively.

So this works perfectly on iOS but on android throws the following error:

I/Unity ( 6256): SocketException: No such host is known 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 hostNameOrAdd ress) [0x00000] in :0 I/Unity ( 6256): at System.Net.Dns.GetHostAddresses (System.String hostNameO rAddress) [0x00000] in :0 I/Unity ( 6256): at System.Net.Sockets.TcpClient.Connect (System.String host name, Int32 port) [0x00000] in :0 I/Unity ( 6256): at 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): at System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMes sage message) [0x00000] in :0 I/Unity ( 6256): Rethrow as SmtpException: Message could not be sent. I/Unity ( 6256): at System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMes sage message) [0x00000] in :0 I/Unity ( 6256): at FeedbackSender+c__Iterator1.MoveNext ()

I have managed to pintpoint the error location at the line: smtpServer.Send(mail);

Any ideas as to what might be causing this issue? I have both android and iOS pro versions.

Cheers

回答1:

I just successfully sent an email from Unity 3D using the following code:

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);
        }
    }
}

With the obvious bits replaced with real data... it's not very different from yours, but my mail server didn't need full SSL authentication, hence why I removed it.

I've not seen the error you're experiencing before, so the only thing I can suggest is to check and double check that your SMTP values are correct - host name, port, username / password.



回答2:

check if u have internet permission in the manifest or ur have net connection. usually the problem is as simple as that.



回答3:

Turns out I was sending the SMTPClient object to a different method which was then trying to send a mail using that object. This is what was causing the error as it stopped throwing an error as soon as that method created its own SMTPClient object.