• 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
0

Answer by CurtisGM · Apr 26, 2018 at 10:27 PM

Hello to all,

Thank you for everyone who has ever visited this question and replied to it. Many of your suggestions were very helpful, and my mind had slipped to close this question.


For those of you who still come here in the future looking for an answer, here was my solution code (Be mindful that the email address needs to be a gmail account):

 MailMessage mail = new MailMessage();
 
 mail.From = new MailAddress("[INSERT EMAIL HERE]");
 mail.To.Add("[INSERT EMAIL HERE]");
 mail.Subject = "[INSERT SUBJECT HERE]";
 
 string body = "[INSERT BODY HERE]";
 
 mail.Body = body;
 
 SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
 smtpServer.Port = 587;
 smtpServer.Credentials = new System.Net.NetworkCredential("[INSERT EMAIL HERE]", "[INSERT PASSWORD HERE]") as ICredentialsByHost;
 smtpServer.EnableSsl = true;
 ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
 try
 {
     smtpServer.Send(mail);
 }
 catch
 { 
     [INSERT FAILED SEND CODE HERE]
 }



Once again, thank you to all of you who posted answers :)

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

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

Send an email from unity to email adress? 1 Answer

How To send Email with C# on Unity 3D 4.1.2? 8 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