- Home /
Sending Email From Unity Works In Editor, Not In Standalone?
Hello Everyone, I have been trying my hand at sending simple emails out from Unity. The account doing the sending would be a dummy account that is just used for sending maybe one email a day from Unity and nothing else, so security isn't a huge issue for me. This script works fine and is an amalgamation of other scripts found on here and the forums(with contact info taken out of course).
using UnityEngine;
using System.Net.Mail;
using System.Net;
public class SendEmail : MonoBehaviour {
private MailMessage message;
// ########################
void Update()
{
//send your mail just typing space
if (Input.GetKeyDown(KeyCode.Space))
{
SendMail();
}
}
// ########################
private void SendMail()
{
message = new MailMessage();
message.To.Add("EmailAddressToGoTo");
message.Subject = "SubjectTitle";
message.Body = "Message test:<br>Test Message";
message.IsBodyHtml = true;
message.Attachments.Add(new Attachment("something.jpg"));
message.From = new MailAddress("EmailAddress", "SenderName");
SmtpClient smtp = new SmtpClient("smtp.mail.yahoo.com", 587);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
//If you dont need authentication, just leave the next fields
// =========================================================
// Enable Secure Socket Layer (SSL) for connection encryption
smtp.EnableSsl = false;
// Do not send the DefaultCredentials with requests
smtp.UseDefaultCredentials = false;
// Create a System.Net.NetworkCredential object and set
// the username and password required by your SMTP account
smtp.Credentials = new NetworkCredential("username", "password") as ICredentialsByHost;
smtp.Timeout = 20000;
// =========================================================
smtp.Send(message);
Debug.Log("Mail sent");
}
}
Now, as I said, everything works great in the editor (though for some reason trying to use anything other than a yahoo account with Ssl on always gives me a cant find certificate error). When I try to go into standalone it doesnt send an email at all, Ive tried to comment out the attachment part just in case it was looking for an image that wasn't in the correct place, but it still isn't getting anything through.
I looked around a bit and I think it's to do with a missing DLL that is in the editor but not included in the built standalone. However I dont know which DLL includes system.net.mail or where to put it even if I had the file.
Some help would be greatly appreciated!
Hi! When I tried to use your codes in my demo project. I got this error:
SmtpException: 530 5.7.1 Authentication required 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)
Do you know how to fix it? Thank you!
Answer by Alayna · Feb 12, 2013 at 06:04 PM
I found out I had to set Api Compatibility Level to ".NET 2.0" instead of ".NET 2.0 Subset" under player settings. Now everything is working great! Such a strange thing to have to change, hope someone else can be helped by this as well.
This answer was the the thing I was looking for. Thank you.
Thank you so much, I would have never found this out on my own.
Answer by Dave-Carlile · Feb 12, 2013 at 04:07 PM
There should be a log file in the *_Data folder of your game. This might display error messages that will indicate what's going on.
It's giving me this error every time I try "SmtpException: 530 authentication required - for help go to http://help.yahoo.com/help/us/mail/pop/pop-11.html" I had this for a bit before I got it working in editor, but it was because I had accidentally typed the email wrong, but I checked right before I built it and it was fine. Is there some extra step that standalone requires for authentication?
Odd. Sounds like something Yahoo doesn't like about the request. Not sure why it would be different standalone vs. in the editor. Probably not something really realted to Unity. I would suggest searching for that error along with System.Net.$$anonymous$$ail
. There's probably just some configuration missing somewhere.
Your answer
Follow this Question
Related Questions
Why does Assembly-CSharp reference UnityEditor.dll 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Works in the editor, but not when I publish it.. what's wrong..? 0 Answers
How to edit a enumerator in the editor? 2 Answers
How do I reference variables from one editor script in another 0 Answers