Sending email from inside unity3d

瘦欲@ 提交于 2019-12-03 15:33:52

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!