- Home /
This question has been answered by the many people who offered their support.
Unity 3 - Sending Email with C#
I have been looking all over the interwebs for a solution to this problem. I am trying to send an email through Unity and this is the code I currently have:
private void SendEmaill() { // Create a System.Net.Mail.MailMessage object MailMessage message = new MailMessage();
// Add a recipient
message.To.Add("curtisgmurray@gmail.com");
// Add a message subject
message.Subject = "Email message from Curtis sent by Unity";
// Add a message body
message.Body = "Test email";
// Create a System.Net.Mail.MailAddress object and set the sender email address and display name.
message.From = new MailAddress("curtisgmurray@gmail.com", "Curtis in Unity");
// Create a System.Net.Mail.SmtpClient object and set the SMTP host and port number
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
//smtp.SendAsync(message, "Testing, from Curtis");
// Create a System.Net.NetworkCredential object and set the username and password required by your SMTP account
smtp.Credentials = new System.Net.NetworkCredential("curtisgmurray@gmail.com", "JuicyFruit");
//Enable Secure Socket Layer (SSL) for connection encryption
smtp.EnableSsl = true;
// Do not send the DefaultCredentials with requests
smtp.UseDefaultCredentials = false;
// Send the message
smtp.Send(message);
}
The error that Unity 3 is throwing is:
Cannot implicitly convert type System.Net.NetworkCredential' to
System.Net.ICredentialsByHost'. An explicit conversion exists (are you missing a cast?)
I tried sending an email without using authentication to avoid using this line but gmail requires one to provide authentication. Looking around the web, no one appears to have this issue.
Anyone know what is going on with this bug?
Thanks!
Note: JuicyFruit is not the actual password to my email, I changed it for posting purposes but in my actual code, I have the correct password inputted in.
Heyy, did you have success with your code?? It would be really nice, if you could help me, i have the same problem.
cheeers :)
Answer by amykaroline · Jul 17, 2013 at 09:37 PM
I spent over a day on this so I want to share my findings to make openURL or SMTPClient email sending work on iOS devices:
Set API Compatibility Level in Player Settings to NET 2.0 (not NET 2.0 Subset!)
Set Stripping Level in Player Settings to Disabled
After building the XCode project from Unity with the above settings, and then trying to run the build to device, you may get an error that there are duplicate symbols as a result of disabled stripping - manually comment out those symbols in the library that is conflicting. (for me, this was NGUI)
@amykaroline I know it is a very old comment but can you help me with the third step? I am getting duplicate symbols error in xcode. How do I search for / comment duplicate symbols? I am getting "ld: 3730 duplicate symbols for architecture armv7". Do I need to I comment 3730 symbols?
Hey Usman - so the first thing to check is if there are any files that might have been imported into the project twice. If you're getting the error though ONLY when you build with the stripping disabled, then you need to actually look at the list of symbols it outputs and then look in the libraries for the duplicates. I know, it's a tedious process of comparing the libs. Let me know if this works.
@amykaroline I build the project again from unity which created an xcode project. I ran the xcode project on iphone6 and it is running without any errors but still it is not sending email. Any idea why this is happening?
Can you post the code you used to send email on ios? This is the code I am using which is working on Android but not on Ios.
$$anonymous$$ail$$anonymous$$essage mail = new $$anonymous$$ail$$anonymous$$essage();
mail.From = new $$anonymous$$ailAddress("no-reply@alternate-universe.co.uk");
mail.To.Add("abasi117@gmail.com");
//mail.To.Add("info@alternate-universe.co.uk");
mail.Subject = "Quote";
mail.Body = "Name: " + quoteInfo.firstName + " " + quoteInfo.lastName + "\nEmail: " + quoteInfo.email +"\nCompany: " + quoteInfo.company + "\nCity: " + quoteInfo.city + "\nPhone: " + quoteInfo.phone + "\nEvent Type: " + quoteInfo.eventType + "\nConsole Games: " + quoteInfo.consoleGames.ToString() + "\n$$anonymous$$ids Cars: " + quoteInfo.kidsCar.ToString() + "\nHoverBoard: " + quoteInfo.hoverBoard.ToString() + "\nAtvBikes: " + quoteInfo.atvBikesBool.ToString() + "\n6dCinema: " + quoteInfo._6dCinema.ToString() + "\nEstimated Number of People: " + quoteInfo.numberOfPeople + "\nAdditional Info: " + quoteInfo.additionalInfo + "\n";
SmtpClient smtpServer = new SmtpClient ();//("smtp.live.com");
//smtpServer.Host = "smtp.live.com";
smtpServer.Host = "smtpout.europe.secureserver.net";
//smtpServer.Port = 587;
smtpServer.Port = 80;
//smtpServer.Credentials = new System.Net.NetworkCredential("usman.abasi@hotmail.com", "") as ICredentialsByHost;
smtpServer.Credentials = new System.Net.NetworkCredential("no-reply@alternate-universe.co.uk", "password") as ICredentialsByHost;
//smtpServer.EnableSsl = true;
ServicePoint$$anonymous$$anager.ServerCertificateValidationCallback =
delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
smtpServer.Send(mail);
Debug.Log("success");
Answer by Skjalg · Feb 08, 2011 at 05:02 PM
Try something like
smtp.Credentials = CredentialCache.DefaultNetworkCredentials;
Taken from: http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.credentials.aspx
http://msdn.microsoft.com/en-us/library/system.net.credentialcache.aspx
Or if you want to use your own email and password you can try something like this:
NetworkCredential myCred = new NetworkCredential( SecurelyStoredUserName,SecurelyStoredPassword,SecurelyStoredDomain);
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri("www.contoso.com"), "Basic", myCred);
WebRequest wr = WebRequest.Create("www.contoso.com"); smtp.Credentials = myCache;
Taken from: http://msdn.microsoft.com/en-us/library/system.net.networkcredential.aspx
Hi there. Thanks for posting up this issue, as it's very helpful for my current project.
This is going to sound probably like a somewhat noobish question, so I apologise in advance.
I've copied your code into a separate C# script and replaced the line:
smtp.Credentials = new System.Net.NetworkCredential("curtisgmurray@gmail.com", "JuicyFruit");
with
smtp.Credentials = (ICredentialsByHost) new System.Net.NetworkCredential("example@gmail.com", "examplePassword");
However when I try to run the script I get two errors. Both related to each other. The first says:
The type or namespace name $$anonymous$$ail$$anonymous$$essage' could not be found. Are you missing a using directive or an assembly reference? the second says: The type or namespace name
SmtpClient' could not be found. Are you missing a using directive or an assembly reference?
I looked online and saw that I might need to put the script in my standard assets folder, but that didnt change anything (plus all my scripts are C#). Can anyone help?
I have this working on PC can it be adapted to Android/iOS? I would rather not use the Application.OpenURL method to send email as the user could change the email when sending and my idea is to check the email against the database to ensure it is only sent to who we intend. Any help would be great.
Answer by Andy-Korth · Jun 07, 2013 at 03:56 PM
Cannot implicitly convert type System.Net.NetworkCredential' toSystem.Net.ICredentialsByHost'. An explicit conversion exists (are you missing a cast?)
This error can be fixed by in the Player Settings by choosing "API Compatibility Level" of .NET 2.0- not the subset. The subset does not include all the classes you need. When I built to iOS, I found that I also needed to reduce the stripping level.
Since it's not in the .NET 2.0 subset, adding an explicit cast, as suggested by tgraupmann will result in the same error.
Additionally, a few other minor problems with your code. In order to work with Gmail, I believe you need to add something like this:
ServicePointManager.ServerCertificateValidationCallback =
delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
return true;
};
There's an excellent discussion about sending via gmail's smtp here: http://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp
Answer by tgraupmann · Jun 20, 2012 at 03:34 AM
smtp.Credentials = (ICredentialsByHost) new System.Net.NetworkCredential("curtisgmurray@gmail.com", "JuicyFruit");
Answer by pn.mayaa · Apr 21, 2013 at 07:15 PM
Simple and superb.. Even this website http://www.compiletimeerror.com/2013/03/c-sharp-send-email-from-c-program.html addresses something similar... Have a look... May help..