0

I am trying to send mail through unity C#. I am using this code:

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

        mail.From = new MailAddress(from);
        mail.To.Add(to);
        mail.Subject = "Password Verification";
        mail.Body ="Password Number";

        SmtpClient smtpServer = new SmtpClient(smtp);
        smtpServer.Port = 587;
        smtpServer.EnableSsl = true;
        smtpServer.Credentials = new System.Net.NetworkCredential(from, password) as ICredentialsByHost;

        ServicePointManager.ServerCertificateValidationCallback = 
            delegate(object obj, X509Certificate cert, X509Chain chain, SslPolicyErrors sslerrors) 
        { return true; };

        smtpServer.Send(mail);

        #if UNITY_EDITOR
        Debug.Log("email sent");
        #endif
        SetEmailing(false);
}

But mail is not begin sent to the Recipient. I have also enabled the "Allow secure less apps" in my Gmail account.

But mail is not being sent....

Any help is appreciated

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
karthik keyan
  • 428
  • 4
  • 14
  • 2
    Can you show the value of your `smtp` variable? And also what happens when you try this code? You get any exception or error message? – Salah Akbari Nov 03 '18 at 08:32
  • https://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail --I think this link is help ful for you and this link will work because i have also done mail sending using gmail in C# – Dipak Rathod Nov 03 '18 at 09:44
  • I've forgotten the name of the service now, but its run by the same company that runs MailChimp, and it's effectively an outgoing SMTP proxy server. You might need something like that. – Draco18s no longer trusts SE Nov 03 '18 at 21:13
  • Thanks all for your reply.. @ Dipak Rathod i have tried your link.But mail is not send to the receipent....First i tried the code in unity editor. It sends mail for the First Time. After that i had tried 50 times. Mail is not sending to receipent. – karthik keyan Nov 08 '18 at 10:08
  • Is there any other way i can send mail without using SMTP.. – karthik keyan Nov 08 '18 at 10:08
  • I am receiving the error SocketException: Connection timed out. Is there any Settings i have to change. Like changing .net 2.0 subset to .net 2.0... Like that... – karthik keyan Nov 08 '18 at 10:14
  • public 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, DeliveryMethod = SmtpDeliveryMethod.Network, EnableSsl = true, UseDefaultCredentials = false, Credentials = (ICredentialsByHost)new NetworkCredential(sender, smtpPassword) }; ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; smtpServer.Send(mail); } } } – karthik keyan Nov 09 '18 at 06:54
  • I have used the above code but the mail is sending from unity Editor. But When i build my app and run myapp in ipad.. It s not working...Mail is not sending from Ipad... – karthik keyan Nov 09 '18 at 06:57

0 Answers0