how to send an email in unity C#
so, i looked around for a proper c# emailing script, it just needs to send text. And i could not find anything that sent the email, i am now stuck with this code:
using System;
using UnityEngine;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class mono_gmail : MonoBehaviour
{
public static void Main (string[] args)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("email@gmail.com");
mail.To.Add("email@gmail.com");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
smtpServer.Port = 465;
smtpServer.Credentials = new System.Net.NetworkCredential("email@gmail.com", "password");
smtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback =
delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
smtpServer.Send(mail);
}
}
also, the email and password fields are filled in, but just not here for security purposes.
And i did the 465 port since that is the outgoing mail server port for ssl.
PS : nothing is printing errors either.
I hope you can help, thanks!
Duplicated question. Check here (or just google "unity send email smtp").
@Umresh nothing happens when it runs, im not exactly sure what is supposed to happen when sendmail is called, but i know it runs.
@Andres Fernandez I tried that solution, and it definitely runs, but gives me this error :
SmtpException: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754 b47sm11270994qge.44 - gsmtp System.Net.$$anonymous$$ail.SmtpClient.Authenticate (System.String Username, System.String Password) System.Net.$$anonymous$$ail.SmtpClient.Authenticate () System.Net.$$anonymous$$ail.SmtpClient.SendCore (System.Net.$$anonymous$$ail.$$anonymous$$ail$$anonymous$$essage message) System.Net.$$anonymous$$ail.SmtpClient.SendInternal (System.Net.$$anonymous$$ail.$$anonymous$$ail$$anonymous$$essage message) System.Net.$$anonymous$$ail.SmtpClient.Send (System.Net.$$anonymous$$ail.$$anonymous$$ail$$anonymous$$essage message)
From the exception, it looks like the client doesn't accept either the name or the password. I'm sorry, but other than that I can't help any further (I've only sent mail from unity within android apps).
Answer by NeverHopeless · Aug 25, 2015 at 06:53 AM
I faced these error while sending email from gmail, try sending by changing these two parameters:
smtpServer.Port = 587;
smtpServer.EnableSsl = true; // try toggling this value as well
and make sure the credentials are correct there shouldn't be any encryption to the data when passing to smtp object.
Also, wrap your code in try-catch block to catch the exception message.
@NeverHopeless thanks for the response, it is the exact same code as the two lines inside it, but toggling EnambleSsl gives me this error :
$$anonymous$$ust issue a STARTTLS command first.
Hi @UltraTechX ,
Unfortunately, Unity uses an outdated and buggy $$anonymous$$ono 2.0 SmtpClient, which doesn't fully support STARTTLS protocol used my many modern S$$anonymous$$TP servers. We've recently published a new asset UT$$anonymous$$ail - Email Composition and Sending Plugin, which doesn't have that issue and so works well with most S$$anonymous$$TP servers (we've tested it with number of popular ones, including Gmail). It also allows not only directly sending emails with S$$anonymous$$TP, but also composing emails using a system email client, with a content defined by your app. It works on multiple platforms and supports attachments and HT$$anonymous$$L / text messages.
Best regards,
Yuriy, Universal Tools $$anonymous$$m.
What if google doesn't let me sign in? @NeverHopeless