Advertisement
Muk99

Email Unity

Nov 6th, 2015
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Mail;
  4. using UnityEngine;
  5. using System.Threading;
  6.  
  7. public static class UnityMail {
  8.  
  9.     private const string username = "seuemail@email.com";
  10.     private const string password = "suaSenha123";
  11.     private const string client = "smtp.gmail.com";
  12.     private const int port = 587;
  13.  
  14.     /*
  15.     Usage:
  16.     UnityMail.Send("algumfdp@email.com", "Titulo", "Texto");
  17.     UnityMail.SendAsync("algumfdp@email.com", "Titulo", "Texto");
  18.     */
  19.  
  20.     public static bool Send(string receiver, string subject, string body) {
  21.         try {
  22.             using(var mail = new MailMessage()) {
  23.                 mail.From = new MailAddress(username);
  24.                 mail.To.Add(receiver);
  25.                 mail.Subject = subject;
  26.                 mail.Body = body;
  27.  
  28.                 var server = new SmtpClient(client);
  29.                 server.Port = port;
  30.                 server.Credentials = new NetworkCredential(username, password) as ICredentialsByHost;
  31.                 server.EnableSsl = true;
  32.  
  33.                 ServicePointManager.ServerCertificateValidationCallback += (sender, certificade, chain, sslErrors) => { return true; };
  34.  
  35.                 server.Send(mail);
  36.  
  37.                 Debug.Log("Send email to " + receiver);
  38.                 return true;
  39.             }
  40.         }
  41.         catch(Exception ex) {
  42.             Debug.LogWarning(ex);
  43.             Debug.LogWarning("Error sending email");
  44.             return false;
  45.         }
  46.     }
  47.    
  48.     public bool ready = true;
  49.  
  50.     public static void SendAsync(string receiver, string subject, string body) {
  51.          new Thread(() => {
  52.              ready = false;
  53.              try{
  54.                 Send(receiver, subject, body);
  55.              }
  56.              finally{
  57.                 ready = true;
  58.              }}).Start();
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement