How to add email in a email sender

  • Thread starter Thread starter Stanlyhalo
  • Start date Start date
S

Stanlyhalo

Guest
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class userSelectNav : MonoBehaviour
{

public TextMeshProUGUI emailField;

public string emailText;

public void sendMail()
{
try
{
emailText = emailField.text;

Debug.Log(emailText);

MailMessage mail = new MailMessage();

mail.From = new MailAddress("senderUsername@gmail.com");
mail.To.Add(emailText);
mail.Subject = "Game Name Verification Email";
mail.Body = "Body";

SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
smtpServer.Port = 587;
smtpServer.Credentials = new System.Net.NetworkCredential("senderUsername@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");
}
catch (Exception ex)
{
Debug.Log("Failed to send email.");
Debug.Log(ex);
}
}
}


So that's the code, and the problem isn't Unity, but c#, for some reason at the part where it says

mail.To.Add(emailText);

it can't send, but instead if I replaced emailText with a string with a email in it, it works, and how I know it's a string is that I Log it in the output by typing before hand

Debug.Log(emailText);

Can someone either tell me an alternative or a solution?

Continue reading...
 
Back
Top