Search Unity

Help with C# E-Mail Script

Discussion in 'Scripting' started by AddyDaDaddy, Aug 14, 2019.

  1. AddyDaDaddy

    AddyDaDaddy

    Joined:
    Jun 14, 2016
    Posts:
    7
    Hi All,

    I am developing an app for my construction company, it will be a simple start off app that hopefully I can develop into a fully operating field management app.

    Our engineers work with Job sheets which we give to them in order to fill out what they have done for a specific job and finally sign it off from the client. This then comes back to the office for our admin to process for invoicing (eventually).

    I have the app together but I am stuck at the moment, I have a digital copy of our job sheet for our engineers to fill out, then by digital signature our client can sign. Once the job sheet is filled out and signed the "Finalise" button will become active (providing all fields required are input with info). This button will then be pressed and will run checks to ensure input fields are not empty (I have not done this part yet) then send a copy of the job sheet to a specific e-mail (work) as an attachment. Our problem is our engineers do not always give the proper information and hopefully this can help completely alleviate this.

    I am using the below script attached to a button, this works when sending an e-mail with description to a specific e-mail as I have tested this by sending one from my e-mail to my e-mail but I am struggling on how to send the document over as an attachment so we can open it up in the office once received.

    Maybe if I was to set this whole sheet as an object and use that to refer to in the script? this one is a new one on me, any help is appreciated

    Thanks.

    Code (CSharp):
    1.    public void SendReceipt()
    2.     {
    3.         MailMessage mail = new MailMessage();
    4.  
    5.         mail.From = new MailAddress("Engineers E-Mail");
    6.         mail.To.Add("Work E-Mail");
    7.         mail.Subject = "Job Sheet from Engineer etc";
    8.         mail.Body = "This is just to send an e-mail with description. I do not need this but rather an attachment instead";
    9.  
    10.         SmtpClient smtpServer = new SmtpClient("Client Details");
    11.         smtpServer.Port = Port Details;
    12.         smtpServer.Credentials = new System.Net.NetworkCredential("E-Mail", "Password") as ICredentialsByHost;
    13.         smtpServer.EnableSsl = true;
    14.         ServicePointManager.ServerCertificateValidationCallback =
    15.         delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    16.         { return true; };
    17.         smtpServer.Send(mail);
    18.         Debug.Log("success");
    19.  
    20.     }
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    AddyDaDaddy likes this.
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    It sounds like one thing that might be tripping you up is that an email attachment needs to be a file on your computer, not just a data structure that exists inside of your application. For example, you can't attach a Unity GameObject, because GameObjects can only exist inside Unity.

    Before you can send your sheet as an attachment, you need to save it to your hard disk as a file.



    I'll also give the same warning I did in the last email thread: If you put the password to your email account inside your source code, you should assume that any moderately-clever hacker who gets their hands on your application will be able to extract that password and start sending out spam from your email account.

    Since this program is only for internal use at your company, that might be an acceptable risk, but you should still probably make sure you use a special email account exclusively for this purpose (e.g. NOT your personal account) and you should keep an eye on it so that you have a chance of noticing if it get suborned.

    Alternately, you could require the user to supply their own email credentials to use your software, or you could forget email and instead send the data by having your software directly contact some custom server that you've set up for this purpose.
     
    JeffDUnity3D and AddyDaDaddy like this.
  4. AddyDaDaddy

    AddyDaDaddy

    Joined:
    Jun 14, 2016
    Posts:
    7
  5. AddyDaDaddy

    AddyDaDaddy

    Joined:
    Jun 14, 2016
    Posts:
    7
    Appreciate the advice, after reading this I realise yes of course the Game Object would not work haha. Maybe if I make the finalise button a save button and save file to the device? then I can set up a different way of sending this to the work e-mail in a way they have to attach the saved file... got me thinking now, for good reason though.

    I did not know about the risk you mention so that is very helpful. I will probably set up an e-mail specifically for this and keep an eye on it. I will be purchasing all the devices and building the app straight onto the phone myself once this has been completed and tested.

    Again appreciate this advice.
     
  6. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    Of course, even a specifically made-up e-mail won't do if the application becomes "big" / "serious", as you'll end up being responsible for any sent virus / law infrigements made using this mail address. At that point you'll be forced to have something in the middle, like a website using php that will send the mails itself (then you only need to cURL from the app that website to send the data required for the mail) or an even more complex server-side solution, to let this little server check if the data provided in the form is legit and can be sent without risk, for example by checking if it's not asking to send the mail to someone outside the company.