How to add email in a email sender RRS feed

  • Question

  • 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()
    			emailText = emailField.text;
    			MailMessage mail = new MailMessage();
    			mail.From = new MailAddress("senderUsername@gmail.com");
    			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;
    		catch (Exception ex)
    			Debug.Log("Failed to send email.");

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


    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


    Can someone either tell me an alternative or a solution?

    Sunday, November 25, 2018 3:45 AM

All replies

  • It looks like you are looking for way to check if input was given and is a valid email address. If so, there are multiple ways to check that. See the below post:


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, November 25, 2018 4:44 AM
  • When you say "it can't send", what happens?

    Two things to try.

    • Set a breakpoint and examine the object you have the incoming send to address.
    • Set up SendCompleted event and check for issues (see sample code).

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, November 25, 2018 10:28 AM
  • Hi Stanlyhalo,

    Thank you for posting here

    For your question, what is the emailtext? The parameter of mail.To.Add() method is address. Please check it. If it is possible, please provide the emailtext and the error message when you could not send the mail.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 26, 2018 6:26 AM