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 /
avatar image
0
Question by Calum1015 · Mar 25, 2015 at 10:46 PM · c#unity 5user interfaceinputfieldemail

Send inputfield entries via email

So I recently obtained a code from http://answers.unity3d.com/questions/433283/how-to-send-email-with-c.html

This just sends an email. But is it possible to take the text written from a user in an inputfield (unity 5 UI) and slot that in the code below as the message? Help would be greatly appreciated!

  using UnityEngine;
  using System.Collections;
  using System;
  using System.Net;
  using System.Net.Mail;
  using System.Net.Security;
  using System.Security.Cryptography.X509Certificates;
  
  public class mono_gmail : MonoBehaviour {
  
          void Main ()
          {
              MailMessage mail = new MailMessage();
  
              mail.From = new MailAddress("youraddress@gmail.com");
              mail.To.Add("youraddress@gmail.com");
              mail.Subject = "Test Mail";
              mail.Body = "This is for testing SMTP mail from GMAIL";
  
              SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
              smtpServer.Port = 587;
              smtpServer.Credentials = new System.Net.NetworkCredential("youraddress@gmail.com", "yourpassword") as ICredentialsByHost;
              smtpServer.EnableSsl = true;
              ServicePointManager.ServerCertificateValidationCallback = 
                  delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
                      { return true; };
              smtpServer.Send(mail);
              Debug.Log("success");
          
          }
  }
  
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 peteRunner · Mar 27, 2015 at 01:44 PM 0
Share

I am using also this code, but I am having problem with google. Users are sending me data, but google is blocking message because of suspect login. Do you know how to deal with this?

avatar image unity_XY9GfkpHMyaikw peteRunner · May 13, 2019 at 10:13 AM 0
Share

turn off less secure apps for gmail- https://myaccount.google.com/lesssecureapps Some apps and devices use less secure sign-in technology, which makes your account more vulnerable.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ExtremePowers · Mar 25, 2015 at 11:11 PM

I haven't been using the new UI myself, but couldn't you just do something like this?

 string MailText;
 
 void Update() {
       MailText = UI.Text;
 }
 
 void Main() {
      ....
      Mail.Body = MailText;
      .....
 }
Comment
Add comment · Show 7 · 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 Calum1015 · Mar 26, 2015 at 03:52 PM 0
Share

Oh I am getting one error here, it says the name "UI" does not exist in the current context.

avatar image MakakWasTaken · Mar 26, 2015 at 03:55 PM 0
Share

UI should be a reference to the UI object

avatar image Calum1015 · Mar 26, 2015 at 03:57 PM 0
Share

But how would I create that reference?

avatar image MakakWasTaken · Mar 26, 2015 at 04:03 PM 0
Share

Try this:

 InputField inpfld;
 string $$anonymous$$ailText;
 
 void Start() {
      inpfld = GetComponent<InputField>();
 }
 
 void Update() {
      $$anonymous$$ailText = inpfld.text;
 }
 
 void $$anonymous$$ain() {
      ....
      $$anonymous$$ail.Body = $$anonymous$$ailText;
      ....
 }
avatar image Calum1015 · Mar 26, 2015 at 04:11 PM 0
Share

This is adding even more errors. Assets/email.cs(13,43): error CS0246: The type or namespace name InputField' could not be found. Are you missing a using directive or an assembly reference? Assets/email.cs(13,17): error CS0103: The name inpfld' does not exist in the current context

Assets/email.cs(18,28): error CS0103: The name `inpfld' does not exist in the current context

Show more comments
avatar image
0

Answer by MakakWasTaken · Mar 26, 2015 at 04:49 PM

This should do it:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System;
 using System.Net;
 using System.Net.Mail;
 using System.Net.Security;
 using System.Security.Cryptography.X509Certificates;
 
 [RequireComponent(typeof(InputField))]
 public class SendMail : MonoBehaviour {
 
     InputField inpfield;
 
     void Start() {
         inpfield = GetComponent<InputField> ();
     }
 
     void Update() {
         if (Input.GetButtonDown ("Jump")) {
             SendTheMail(inpfield.text);
         }
     }
 
     void SendTheMail(string text) {
         MailMessage mail = new MailMessage();
         
         mail.From = new MailAddress("youraddress@gmail.com");
         mail.To.Add("youraddress@gmail.com");
         mail.Subject = "Test Mail";
         mail.Body = text;
         
         SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
         smtpServer.Port = 587;
         smtpServer.Credentials = new System.Net.NetworkCredential("youraddress@gmail.com", "yourpassword") as ICredentialsByHost;
         smtpServer.EnableSsl = true;
         ServicePointManager.ServerCertificateValidationCallback = 
             delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
         { return true; };
         smtpServer.Send(mail);
         Debug.Log("success");
     }
 }
 
Comment
Add comment · Show 9 · 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 Calum1015 · Mar 26, 2015 at 05:14 PM 0
Share

yes this works however I want it to send the email within the event system of the new unity UI. This just sends the email when you press the jump key which happens for me to be the spacebar, I can change it I guess. :/

Thank you so much for helping!

avatar image MakakWasTaken · Mar 26, 2015 at 05:16 PM 0
Share

You could make a button that calls the function with the input's text value.

avatar image Calum1015 · Mar 26, 2015 at 05:16 PM 0
Share

ok, thanks again.

avatar image MakakWasTaken · Mar 26, 2015 at 05:21 PM 0
Share

Add this to the top:

 [RequireComponent(typeof(Button))]

and this to the Start function:

 void Start() {
     GetComponent<Button>().onClick.AddListener(YourDisposeFunction);
 }
avatar image Calum1015 · Mar 26, 2015 at 05:25 PM 0
Share
 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System;
 using System.Net;
 using System.Net.$$anonymous$$ail;
 using System.Net.Security;
 using System.Security.Cryptography.X509Certificates;
 
 [RequireComponent(typeof(InputField))]
 public class email : $$anonymous$$onoBehaviour {
     
     InputField inpfield;
     
     void Start() {
         inpfield = GetComponent<InputField> ();
     }
     
     void OnGUI() {
         if (GUI.Button ( new Rect( Screen.height * 0.8f,Screen.width * 0.5f,100,75),"Send")) {
             SendThe$$anonymous$$ail(inpfield.text);
         }
     }
     
     void SendThe$$anonymous$$ail(string text) {
         $$anonymous$$ail$$anonymous$$essage mail = new $$anonymous$$ail$$anonymous$$essage();
         
         mail.From = new $$anonymous$$ailAddress("censored@gmail.com");
         mail.To.Add("censored@gmail.com");
         mail.Subject = "Test $$anonymous$$ail";
         mail.Body = text;
         
         SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
         smtpServer.Port = 587;
         smtpServer.Credentials = new System.Net.NetworkCredential("sencored@gmail.com", "censored") 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");
     }
 }

Like this?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

If i have a script how to I input an int from there to a text UI? 1 Answer

How do i access PlayerSettings through scripting? 1 Answer

Multiple Cars not working 1 Answer

How to add input field into this? 1 Answer

Distribute terrain in zones 3 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