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

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