- Home /
unable to send email from android device
Hello friends. I am desperate for an answer please
I am using a lenovo headset to send emails. it works on android. The email is sent from my editor but not from the headset. her is my code. I have tried ti change the label text and see, the code seems to work until the code for setting the email address.
What do you think may be going wrong after this point.
using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; //using System.Net; //using System.Net.Mail; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using UnityEngine; using EASendMail; using System.Text; using UnityEngine.SceneManagement; using UnityEngine.UI;
public class mailme : MonoBehaviour {
SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();
private string mtcid;
private string mtclevel;
private string mailscore;
private Text vtitle;
Dropdown m_Dropdown;
string m_level;
int m_DropdownValue;
public Text labeltext;
public void SendMail()
{
if ((GameObject.Find("InputField").GetComponent<InputField>().text == "") || (GameObject.FindGameObjectWithTag("dropdown").GetComponentInChildren<Dropdown>().value == 0))
{
Debug.Log("Enter MTC ID");
labeltext.text = "Enter ID and Level!";
}
else if (GameObject.FindGameObjectWithTag("dropdown").GetComponentInChildren<Dropdown>().value == 0)
{
labeltext.text = "Enter ID and Level!";
Debug.Log("Select a level");
}
else
{
m_Dropdown = GameObject.FindGameObjectWithTag("dropdown").GetComponentInChildren<Dropdown>();
Debug.Log("Starting Dropdown Value : " + m_Dropdown.value);
m_DropdownValue = m_Dropdown.value;
m_level = m_Dropdown.options[m_DropdownValue].text;
Debug.Log(m_level);
mtcid = GameObject.Find("InputField").GetComponent<InputField>().text;
// mtclevel = GameObject.Find("KeyboardPanel").GetComponentInChildren<Dropdown>().options[index].text.ToString();
mtclevel = m_level;
mailscore = GameObject.Find("Textscore").GetComponent<Text>().text.ToString();
vtitle = GameObject.Find("VidTitle").GetComponent<Text>();
Debug.Log(vtitle.text);
**//The code works in headset until this comment below
//labeltext.text = "Check3";**
// Set sender email address, please change it to yours
oMail.From = "sender@gmail.com";
// Set recipient email address, please change it to yours
oMail.To = "receiver@abc.com";
// Set email subject
oMail.Subject = "LRC VR Video Result for MTC ID " + mtcid;
// Set email body
oMail.TextBody = "Hi, \n Please find the Score of the VR Video for user with MTC ID " + mtcid + " below:\n\n Video Title: " + vtitle.text + "\n\nMTC ID: " + mtcid + "\nLEVEL: " + mtclevel + "\n" + mailscore;
// Your SMTP server address
SmtpServer oServer = new SmtpServer("smtp.gmail.com");
// User and password for ESMTP authentication, if your server doesn't require
// User authentication, please remove the following codes.
oServer.User = "sender@gmail.com";
oServer.Password = "password";
// If your smtp server requires TLS connection, please add this line
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
oServer.Port = 465;
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
try
{
Console.WriteLine("start to send email ...");
oSmtp.SendMail(oServer, oMail);
Debug.Log("email was sent successfully!");
SceneManager.LoadScene("SelectLevel");
}
catch (Exception ep)
{
labeltext.text = "Failed!";
Console.WriteLine("failed to send email with the following error:");
Console.WriteLine(ep.Message);
SceneManager.LoadScene("SelectLevel");
}
}
} }
Answer by juhainamtc · Jan 10, 2019 at 06:55 AM
Finally it works! So I removed all references to the EASendMail form my project and replaced the code with the code to use what .Net has to provide and the mail is being sent from my VR headset. using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Net; using System.Net.Mail; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using UnityEngine; using System.Text; using UnityEngine.SceneManagement; using UnityEngine.UI;
public class mailme : MonoBehaviour { private string mtcid; private string mtclevel; private string mailscore; private Text vtitle; Dropdown m_Dropdown; string m_level; int m_DropdownValue; public Text labeltext;
public void SendMail()
{
if ((GameObject.Find("InputField").GetComponent<InputField>().text == "") || (GameObject.FindGameObjectWithTag("dropdown").GetComponentInChildren<Dropdown>().value == 0))
{
Debug.Log("Enter MTC ID");
labeltext.text = "Enter ID and Level!";
}
else if (GameObject.FindGameObjectWithTag("dropdown").GetComponentInChildren<Dropdown>().value == 0)
{
labeltext.text = "Enter ID and Level!";
Debug.Log("Select a level");
}
else
{
m_Dropdown = GameObject.FindGameObjectWithTag("dropdown").GetComponentInChildren<Dropdown>();
Debug.Log("Starting Dropdown Value : " + m_Dropdown.value);
m_DropdownValue = m_Dropdown.value;
m_level = m_Dropdown.options[m_DropdownValue].text;
Debug.Log(m_level);
mtcid = GameObject.Find("InputField").GetComponent<InputField>().text;
mtclevel = m_level;
mailscore = GameObject.Find("Textscore").GetComponent<Text>().text.ToString();
vtitle = GameObject.Find("VidTitle").GetComponent<Text>();
Debug.Log(vtitle.text);
var smtp = new SmtpClient()
{
Host = "mail.mtc.edu.om",
Port = 25,
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network
};
var credentials = new NetworkCredential();
smtp.UseDefaultCredentials = false;
smtp.Credentials = credentials;
var from = new MailAddress("senderemail", "Sendernametoappear");
var to = new MailAddress("receiveremail", "Receivername");
var msg = new MailMessage(from, to)
{
Subject = "LRC VR Video Result for MTC ID " + mtcid,
Body = "Hi, <br> Please find the Score of the VR Video for user with MTC ID<b> " + mtcid + " </b>below:<br/><br/><b> Video Title:</b> " + vtitle.text + "<br><b>MTC ID: </b>" + mtcid + "<br><b>Level:</b> " + mtclevel + "<br><h1><font color='red'>" + mailscore +"</font></h1>",
IsBodyHtml = true,
Priority = MailPriority.Normal
};
smtp.Send(msg);
SceneManager.LoadScene("SelectLevel");
}
}
}
Answer by Sabre-Runner · Jan 06, 2019 at 05:59 AM
First of all, you do a lot of object finds. That is a very expensive operation that can be handled at start up or simply by setting references in the inspector.
Secondly, check out Native Share. It does most of the work for you.
thanks. but I am looking for a solution to my problem of sending email which contains text body.
I have just imported it. but honestly i did not understand how to use it. if you have used this,m then pls let me know how it works. Do I have to change my script? or will my script work now with this?
Your answer
Follow this Question
Related Questions
HTML Email Android 1 Answer
Sending email with Smtp client not working on Android 0 Answers
Send email works in Editor, not on tablet 2 Answers
Unity Android Streaming Assets Save/Recall w/ WWW class? 0 Answers
Application.OpenURL for email on mobile 4 Answers