I am trying to write a script which would send me an email using my own Gmail. account. now, I’ve found loads of examples which uses the GMail account, but for whaterver reason I still have problems of authentication and/or credentials. So, is anybody of you that have a working example on how to send an email using C# ?
Are you using smtpclient? The name is usually “yourname@whateveraddress”, and be sure to set the port correctly, and set “EnableSSL” to true.
Note: Depending on your programs behavior, google can (and will) shut off your email account for “suspicious activity”. Happened to us at really low volume (we just send out reminders to a few account), so we just set up our own smtp server, problem solved.
Hi,
well you can see the script below. and I just copy and pasted what I 've found online. The problem is that I always get this exception:
System.Net.Mail.SmtpException: Message could not be sent. —> System.IO.IOException: The authentication or decryption has failed. —> System.InvalidOperationException: SSL authentication error: RemoteCertificateNotAvailable, RemoteCertificateChainErrors
and I am pretty sure about the authentication I provided.
using UnityEngine;
using System;
using System.Collections;
using System.Net.Mail;
using System.Net;
public class SendEmail : MonoBehaviour {
// Use this for initialization
void Start () {
}
private void PrepareEmail() {
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("amatigianc@gmail.com");
mail.To.Add("g.amati@ucl.ac.uk");
mail.Subject = "Prova";
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.Port = 587;
SmtpServer.Credentials = (ICredentialsByHost)CredentialCache.DefaultNetworkCredentials; //(ICredentialsByHost) new NetworkCredential("GMAILACCOUNT","PASSWORD");
SmtpServer.EnableSsl = true;
SmtpServer.Timeout = 20000;
SmtpServer.UseDefaultCredentials = false;
try {
SmtpServer.Send(mail);
} catch (SmtpException myEx) {
Debug.Log ("Expection: \n" + myEx.ToString());
}
}
void OnGUI() {
if (GUI.Button(new Rect(0,0,100,100),"SendEmail") ) {
PrepareEmail();
}
}
// Update is called once per frame
void Update () {
}
}
Oh Yeah sorry, I do provide the actual credentials and the exception is at it follows:
System.Net.Mail.SmtpException: Message could not be sent. —> System.IO.IOException: The authentication or decryption has failed. —> System.InvalidOperationException: SSL authentication error: RemoteCertificateNotAvailable, RemoteCertificateChainErrors
at System.Net.Mail.SmtpClient.m__4 (System.Object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, SslPolicyErrors sslPolicyErrors) [0x00000] in :0
at System.Net.Security.SslStream+c__AnonStorey7.<>m__A (System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Int32[ ] certErrors) [0x00000] in :0
at Mono.Security.Protocol.Tls.SslClientStream.OnRemoteCertificateValidation (System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Int32[ ] errors) [0x00000] in :0
at Mono.Security.Protocol.Tls.SslStreamBase.RaiseRemoteCertificateValidation (System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Int32[ ] errors) [0x00000] in :0
at Mono.Security.Protocol.Tls.SslClientStream.RaiseServerCertificateValidation (System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Int32[ ] certificateErrors) [0x00000] in :0
at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.validateCertificates (Mono.Security.X509.X509CertificateCollection certificates) [0x00000] in :0
at Mono.Security.Protocol.Tls.Handshake.Client.TlsServerCertificate.ProcessAsTls1 () [0x00000] in :0
at Mono.Security.Protocol.Tls.Handshake.HandshakeMessage.Process () [0x00000] in :0
at (wrapper remoting-invoke-with-check) Mono.Security.Protocol.Tls.Handshake.HandshakeMessage:Process ()
at Mono.Security.Protocol.Tls.ClientRecordProtocol.ProcessHandshakeMessage (Mono.Security.Protocol.Tls.TlsStream handMsg) [0x00000] in :0
at Mono.Security.Protocol.Tls.RecordProtocol.InternalReceiveRecordCallback (IAsyncResult asyncResult) [0x00000] in :0
— End of inner exception stack trace —
at Mono.Security.Protocol.Tls.SslStreamBase.AsyncHandshakeCallback (IAsyncResult asyncResult) [0x00000] in :0
— End of inner exception stack trace —
at System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMessage message) [0x00000] in :0
at SendEmail.PrepareEmail () [0x00081] in C:\Documents and Settings\ucwegam\My Documents\SendingEmail\Assets\SendEmail.cs:31
UnityEngine.Debug:Log(Object)
And strangely regarding the smtpServer.Port if I set it to 465, Unity Crashes or goes to an endless loop. If I set the value to 587, I get the exception
I looked in our code. (C#, but not Unity). It’s very simple:
create the smtpclient for smtp.gmail.com, set EnableSsL to true. Set the port to 587. Set DeliveryMethod to Network.
Then create and send mail…
We use a google apps domain, so we specify the username as “username@ourdomain.com” (not just username) in the credentials.
If you’ve been testing sending email from this account, I suppose it’s possible that gmail locked it already. However, I don’t seem to recall that being the error we were recieving when that happened.
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");
}
}
Reviving an old post, for others looking for a solution using gmail, after testing this (and it failing) check the inbox of the account you’re using, you should get an email telling you that a connection was blocked, and you have to activate smpt connections in your gmail settings. The email from gmail will link you to the setting!
I want to send a mail in unity3d using the IMAP or POP3 or etc but not using SMTP ,
I want to send mail using IOS device in which SMTP server is not working properly.