Is it possible to send a string in mail.Body?
I want to send the string survey in mail.body. Everytime I "Send Email" mail.Body as empty. When I use Debug.Log(survey); i get all the correct data in the console but nothing shows up in the email.
using System.IO; using System.Text; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Networking; using System.Net; using System.Net.Mail; using System.Net.Security; using System.Security.Cryptography.X509Certificates;
public class SubmittingBehaviour : UI_AbstractMenuBehaviour {
[SerializeField] Text InfoText;
[SerializeField] Text CloseButtonText;
public int savedCount = 0;
public MainMenuBehaviour MainBehaviour;
public string survey;
// Use this for initialization
void Start () {
CloseButtonText.text = MainMenuBehaviour.CurrentDictionary.GetKeyValue("cancel");
//activate ths, if you want to send data
InfoText.text = MainMenuBehaviour.CurrentDictionary.GetKeyValue("sending_data",false);
Questionaire questionaire = MainBehaviour.GetCurrentQuestionaire();
SaveSurveyToJson saveSurvey = new SaveSurveyToJson(questionaire);
survey = JsonUtility.ToJson(saveSurvey);
**Debug.Log(survey);**
//how many saves so far?
int savedCount = 0;
if (PlayerPrefs.HasKey("SavedCount"))
{
savedCount = PlayerPrefs.GetInt("SavedCount");
}
SaveLocal(survey, savedCount);
//TODO
//Activate this, if you want to send the data to a server
/* StartCoroutine( PushToServer(survey,savedCount));*/
savedCount++;
PlayerPrefs.SetInt("SavedCount", savedCount);
}
/// <summary>
/// Save results as a json file in persistant data path
/// </summary>
/// <param name="survey">Survey.</param>
/// <param name="id">Identifier.</param>
void SaveLocal(string survey, int id) { //save to disk string fileName = "results" + id.ToString("D4"); print("Filename: " + fileName); Debug.Log(Application.persistentDataPath);
string filePath = Application.persistentDataPath + "/"+fileName + ".json";
//string path = Application.persistentDataPath;
//SendEmail.(MailMessage);
byte[] JsonStringBytes = Encoding.UTF8.GetBytes(survey);
File.WriteAllBytes(filePath, JsonStringBytes);
InfoText.text += "\n\n" + MainMenuBehaviour.CurrentDictionary.GetKeyValue("saved_locally", false);
}
public void SendEmail()
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("sender@gmail.com");
mail.To.Add("reciever1@gmail.com");
mail.To.Add("reciever2@gmail.com");
mail.To.Add("reciever3@gmail.com@gmail.com");
mail.Subject = "Test Mail";
// mail.Body = "This is for testing SMTP mail from GMAIL";
mail.Body = survey;
Debug.Log(mail.Body);
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
smtpServer.Port = 587;
smtpServer.Credentials = new System.Net.NetworkCredential("sender@gmail.com", "senderpassword") as ICredentialsByHost;
smtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback =
delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
smtpServer.Send(mail);
Debug.Log("success");
}
}