- Home /
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");
}
}
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?
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.
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;
.....
}
Oh I am getting one error here, it says the name "UI" does not exist in the current context.
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;
....
}
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
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");
}
}
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!
You could make a button that calls the function with the input's text value.
Add this to the top:
[RequireComponent(typeof(Button))]
and this to the Start function:
void Start() {
GetComponent<Button>().onClick.AddListener(YourDisposeFunction);
}
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?
Your answer
Follow this Question
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