Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Send a mail direct from Unity

Discussion in 'Scripting' started by xandeck, Jun 25, 2012.

  1. xandeck

    xandeck

    Joined:
    Apr 2, 2009
    Posts:
    563
    Hello all,

    I was searching for a way to send an email from Unity. I had a couple of posts from Search tool here, but nothing came real solid to me... and I found some results googling it...

    So, if anyone wants to send an email direct from Unity, here is the code, I tested and it works... I sent with my own web company services, you could even send by your gmail account (I did not try yet), but this way works good...

    Just sharing :)

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Net.Mail;
    4. using System.Net;
    5.  
    6. public class MailManager : MonoBehaviour {
    7.  
    8.     private MailMessage message;
    9.  
    10.     // ########################
    11.     void Update()
    12.     {
    13.         //send your mail just typing space
    14.         if (Input.GetKeyDown(KeyCode.Space))
    15.         {
    16.             SendMail();
    17.         }
    18.     }
    19.     // ########################
    20.     private void SendMail()
    21.     {
    22.        
    23.         message = new MailMessage();
    24.         message.To.Add("to_your_destiny@gmail.com, another_address@domain.com");
    25.        
    26.         message.Subject = "Test message from Unity";
    27.         message.Body = "Message test:<br>Second line";
    28.         message.IsBodyHtml = true;
    29.  
    30.         message.From = new MailAddress("my_mail@mydomain.com", "Im the guy");
    31.  
    32.         SmtpClient smtp = new SmtpClient("mail.yoursmtpservice.com", 587);
    33.         smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    34.  
    35.         //If you dont need authentication, just leave the next fields
    36.         // =========================================================
    37.         // Enable Secure Socket Layer (SSL) for connection encryption
    38.         smtp.EnableSsl = false;
    39.         // Do not send the DefaultCredentials with requests
    40.         smtp.UseDefaultCredentials = false;
    41.         // Create a System.Net.NetworkCredential object and set
    42.         // the username and password required by your SMTP account
    43.         smtp.Credentials = new NetworkCredential("mymail@mydomain.com", "mypassword") as ICredentialsByHost;
    44.         smtp.Timeout = 20000;
    45.         // =========================================================
    46.  
    47.         smtp.Send(message);
    48.         Debug.Log("Mail sent");
    49.     }
    50.     // #########################
    51.  
    Cheers
     
  2. andorov

    andorov

    Joined:
    Feb 10, 2011
    Posts:
    1,061
    If you deploy to web, this will only work if the remote server is running a socket policy server. (i think!)
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    why would you hand out your private business mail credentials to everyone? (thats exactly what you do there basically)

    The Mail api is total blurp for anything but ASP websites or letting users configure it with their data.
    You better push the mail sending to your backend, use WWW to call the webservice which actually facilitates the sending. that way your credential remain your credentials and you can additially take the required spam prevention steps to not get sued for spaming
     
  4. xandeck

    xandeck

    Joined:
    Apr 2, 2009
    Posts:
    563
    @andorov
    Maybe, I did not test it yet... there are examples with google mail, I did not try it...

    @dreamora
    There are a lot of examples to do it using WWW call or another way, but with this way there is no solid example in Unity foruns (I did not find), I just wanted to give the code...
    I'm not saying if it is secure or not... I work with advergames, sometimes we need to make an application with Unity that it will run just for a day or two, and so this works really good if I dont want to make any WWW calls or read/write to PHP or anything...

    Why dont you send an example about what you saying instead of keep sending posts/replies to others users posts saying that you know a very superb better way to do it all the time? Of course there is a better way, it is very obvious that my private business credentials are there... and thats what the example stands for... just an example ;)
     
  5. minevr

    minevr

    Joined:
    Mar 4, 2008
    Posts:
    1,018
    Send ok. Thanks for share. :)