Sending email using gmail account

hi everyone,

Thanks for taking your time for reading my issue. I doing now is about forgot password so when the username enter his/her username the system will send the email to user using my existing gmail account(mymail@gmail.com). So the issue i facing now it unable to send message to the user instead it throw exception “InvalidOperationException: SSL authentication error: RemoteCertificateNotAvailable, RemoteCertificateChainErrors”

 private void SendEmail(string nEmail, string nPassword) {
        // Create a System.Net.Mail.MailMessage object
        MailMessage message = new MailMessage();

        // Add a recipient
        message.To.Add("\"" + nEmail + "\"");

        // Add a message subject
        message.Subject = "Email message from Curtis sent by Unity";

        // Add a message body
        message.Body = "Your password is: " + nPassword;

        // Create a System.Net.Mail.MailAddress object and set the sender email address and display name.
        message.From = new MailAddress("mymail@gmail.com", "Curtis in Unity");

        // Create a System.Net.Mail.SmtpClient object and set the SMTP host and port number
        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);

        // Create a System.Net.NetworkCredential object and set the username and password required by your SMTP account
        smtp.Credentials = new System.Net.NetworkCredential("mymail@gmail.com", "12345678");

        //Enable Secure Socket Layer (SSL) for connection encryption
        smtp.EnableSsl = true;

        // Do not send the DefaultCredentials with requests
        smtp.UseDefaultCredentials = false;

        // Send the message
        smtp.Send(message);        
    }

Thanks… :slight_smile:

anyone know solution for this problem???

The problem: gmail is a security account, and Unity not suported security account. Try in Yahoo, or other.
This was the solution I found.

with unity 4.1.2 I’ve sucseed with this code:

using UnityEngine;
using System;
using System.Net;
using System.Net.Mail;
using System.Collections;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
//TestingUnity3DEmail
public class SendEmail : MonoBehaviour {

	void Start(){
		SendEmailF();
	}
	private void SendEmailF(){
		MailMessage mail = new MailMessage();
		
		mail.From = new MailAddress("Your.Email@gmail.com");
		mail.To.Add("Their.Email@gmail.com");
		mail.Subject = "Test Mail Subject";
		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("Your.Email@gmail.com", "Password") as ICredentialsByHost;
		smtpServer.EnableSsl = true;
		ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors){
			return true;
		};
		smtpServer.Send(mail);
		Debug.Log("success");
	}
}

and the 1 that helped me:

Unity Answers