- Home /
Send email with Unity
Hi. I am using Unity 5.6.1f1 and I want ot send an email from unity on the Click of a button, I do not have any email form to be filled in, the message body and Subject are aall hard coded. I am unable toi send the email. I have tried several times, also changed player settings APi to 2.0. I get the error IOException Connection closed when I use Send(mail)n method, but with SendAsync I dont get that message but the email is not sent to the recepient email. this is my script which I have attached to an ampty game object and then assigned that game object to a a button in the scene and selected the function name of the script for the Onclick event of the button.I get the message "Sending Email..."in my console but no email is sent.
public void SendMail()
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("sender@gmail.com");
mail.To.Add("receiver@mtc.edu.om");
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("sender@gmail.com", "senderpassword") as ICredentialsByHost;
smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback =
delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
smtpServer.SendCompleted += new SendCompletedEventHandler(MailDeliveryComplete);
smtpServer.Send(mail);
Debug.Log("success");
}
static void MailDeliveryComplete(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
Console.Write("Message \"{0}\".", e.UserState);
if (e.Error != null)
Console.WriteLine("Error sending email.");
else if (e.Cancelled)
Console.WriteLine("Sending of email cancelled.");
else
Console.WriteLine("Message sent.");
}
}
If I use port 587 Unity freezes. I have tried the SMTP server and port for yahoo mail also, and get the same error. Please help.
This is not a Unity problem, Unity's managed environment has no control over .NET frameworks, this is a .NET question and sorry but I don't know the answer to why it freezes. You should ask on Stackoverflow.
Although don't forget to dispose of your mail object when finished.
@RobAnthem thanks for your reply. I am new to unity and still developing my program$$anonymous$$g skills. COuld oyu please xplain what you mean by dispose the mail object?
out of topic, i hear that if you use your computer as server to send email. There is a huge chance your email will be counted as spam
Answer by juhainamtc · Dec 19, 2018 at 04:08 AM
Ok so I have found a solution to it through this link: https://www.emailarchitect.net/easendmail/ex/c/7.aspx#c-send-email-using-queue-service-example
and https://www.emailarchitect.net/easendmail/kb/csharp.aspx#send-email-in-a-simple-c-project-using-smtp-protocol (this has the script for sending the email)
the first link guides you through the steps. Finally I am able to send emails without any issues! I have replaced the emailarchitect.net in the script in links above with gmail.com wherever required: oMail.From = "senderemail@gmail.com"; SmtpServer oServer = new SmtpServer("smtp.gmail.com"); oServer.User = "senderemail@gmail.com"; Just make sure to change the gmail settings to allow less secure apps and also to enable access .
also, Add the EASend$$anonymous$$ail20.dll and SecurityInterface.dll into your project for the script to work fine.
Answer by sushanta1991 · Oct 07, 2021 at 06:28 AM
Check this solution, It is working fine with unity 2021.
https://sushanta1991.blogspot.com/2021/10/how-to-send-email-from-unity-using.html
There seems to be an error when i change the values. Im getting the message: SendMessage has no receiver!
Issue is fixed i had to disable 2nd step security in Gmail first
Answer by codemaker2015 · Dec 21, 2021 at 03:08 PM
There is a demo application created for sending mail from Unity3D. Checkout the following git repo.