Skip to content

Instantly share code, notes, and snippets.

@NovaSurfer
Last active February 21, 2024 13:44
Show Gist options
  • Save NovaSurfer/0c24b4ab5578814d2d1d to your computer and use it in GitHub Desktop.
Save NovaSurfer/0c24b4ab5578814d2d1d to your computer and use it in GitHub Desktop.
unity3d Email script
/*
For more details, please follow the links below:
https://msdn.microsoft.com/en-us/library/System.Net.Mail.MailMessage(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient(v=vs.110).aspx
*/
using UnityEngine;
using System;
using System.Collections;
using System.Net;
using System.Net.Mail;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class MailSender
{
public MailSender (string smtpServer, int port, string from, string fromName, string password, string username,
string mailto, string subject, string message)
{
SmtpClient _smtpServer = new SmtpClient(smtpServer);
MailMessage _message = new MailMessage();
_message.From = new MailAddress(from, fromName);
_message.To.Add(mailto);
_message.Subject = subject;
_message.Body = message;
_smtpServer.Port = port;
_smtpServer.Credentials = new System.Net.NetworkCredential(username, password) as ICredentialsByHost;
_smtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback =
delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
_smtpServer.Send(_message);
_message.Dispose();
Debug.Log("Success");
}
}
/*
Example:
1. Create two inputFields (one for the title and one for the message).
2. Create a button that will be responsible for sending a message.
3. Attach the UIScript to any GameObject on the scene.
4. Fill out all the required fields.
5. Go to the submit button, click plus icon under OnClick() event,
select GameObject with attached UIScript, then select the OnClick() method in UIScript.
(or use eventSystems + public void OnPointerClick(EventSystems.PointerEventData eventData))
-- smtpServer - smtp.gmail.com
-- port - 587
-- from - yourmail.gmail.com
-- fromName - sender name
-- password - yourMailPassword
-- username - yourmail(username, or full mail address)
-- mailto - receiverMail.mailhoting.com
-- subject - title of your message
-- message - message(lol)
*/
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class UIScript : MonoBehaviour {
public InputField TitleField, Messagefield;
public string smtpServer,from, fromName, mailto, username, password;
public int port; // 587
public void OnClick()
{
MailSender mail = new MailSender(smtpServer, port, from, fromName, password, username, mailto, TitleField.text, Messagefield.text);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment