Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Apr 26, 2018 at 10:29 PM by CurtisGM for the following reason:

This question has been answered by the many people who offered their support.

avatar image
5
Question by CurtisGM · Feb 08, 2011 at 04:49 PM · emailsendcs0266

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' toSystem.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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Andreas Rau · Feb 26, 2011 at 02:45 PM 2
Share

Heyy, did you have success with your code?? It would be really nice, if you could help me, i have the same problem.

cheeers :)

avatar image przem997 · Jul 03, 2013 at 12:26 PM 0
Share

How make it in JavaScript ?

6 Replies

  • Sort: 
avatar image
5

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)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UsmanAbbasi · Mar 04, 2016 at 05:32 PM 0
Share

@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?

avatar image amykaroline · Mar 04, 2016 at 09:29 PM 0
Share

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.

avatar image UsmanAbbasi · Mar 07, 2016 at 12:47 PM 0
Share

@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");
 
avatar image
3

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xandermacleod · Jun 20, 2012 at 10:20 AM 0
Share

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?

avatar image AR8402 · Apr 02, 2013 at 12:04 AM 0
Share

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.

avatar image
2

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by tgraupmann · Jun 20, 2012 at 03:34 AM

smtp.Credentials = (ICredentialsByHost) new System.Net.NetworkCredential("curtisgmurray@gmail.com", "JuicyFruit");

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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..

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Send an email from unity to email adress? 1 Answer

How To send Email with C# on Unity 3D 4.1.2? 8 Answers

Add a byte[] as attachment in a mail 1 Answer

send email from unity? 4 Answers

How do I send an email on IOS using Application.OpenURL with more than one line of body text? 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges