Send mail

Hello, i have getting this script on google

using UnityEngine;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

public class SendMail : MonoBehaviour
{

    void Start()
    {
        MailMessage mail = new MailMessage();

        mail.From = new MailAddress("darklou31@gmail.com");
        mail.To.Add("Inconnue@gmail.com");
        mail.Subject = "Info";
        mail.Body = "Test mail";
        SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
        smtpServer.Port = 587;
        smtpServer.Credentials = new System.Net.NetworkCredential("youraddress@gmail.com", "yourpassword") as ICredentialsByHost;
        smtpServer.EnableSsl = true;
        ServicePointManager.ServerCertificateValidationCallback =
            delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            { return true; };
        smtpServer.Send(mail);
        Debug.Log("success");

    }
}

but i don’t understand the line next:

smtpServer.Credentials = new System.Net.NetworkCredential("youraddress@gmail.com", "yourpassword") as ICredentialsByHost;

what is “youraddress@gmail.com” and “yourpassword”
how getting that

Thanks

https://docs.microsoft.com/en-us/dotnet/api/system.net.networkcredential?view=netframework-4.8

Your code is used to connect to an SMTP server which requires authentication.

Those are the credentials used by the SMTP server. Replace those with a valid email address or username, and password, for your SMTP server. If you’re using Gmail, I would assume that would be the credentials for your own Gmail account.

1 Like