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:
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!