0

So, I've got an I.T Request program, that works by passing a few strings to the database. However, this only works within the same network (or at least with access to the network).

So I'm writing in a method that generates an email and sends that, with a batch file as an attachment that will then run the db insert method. I know how to create said batch file, and attach it, however the problem is that all the methods that I've seen physically create the file in a defined location, then attach it.

How can I create a temporary file, that will not be saved on the user's PC, and sent off as part of the auto-generated email?

void SendEmail(Request_View view)
{
    MailMessage mail = new MailMessage("from@email.com", "to@email.com");
    SmtpClient client = new SmtpClient();
    client.Port = 25;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Host = "gmail.com";
    mail.Subject = "Subject";
    mail.Body = ""Message";

    // Create temp batch file

    mail.Attachments.Add(new Attachment(BatchFile.bat));
    client.Send(mail);
}
M.Armoun
  • 1,075
  • 3
  • 12
  • 38
Ben
  • 2,433
  • 5
  • 39
  • 69
  • Have a look at:https://stackoverflow.com/questions/19689054/is-it-possible-for-a-c-sharp-built-exe-to-self-delete/19689415 – Prashant Pimpale Dec 21 '18 at 07:11
  • This also:https://stackoverflow.com/questions/1305428/self-deletable-application-in-c-sharp-in-one-executable – Prashant Pimpale Dec 21 '18 at 07:11
  • How can you create a file that is not, by definition, _already_ saved on the local PC? You either have to delete the file yourself or not create it at all. I see that System.Net.Mail.Attachment has a [constructor](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.attachment.-ctor?view=netframework-4.7.2#System_Net_Mail_Attachment__ctor_System_IO_Stream_System_Net_Mime_ContentType_) that takes a stream as input, rather than a file. See [How Do I Generate a Stream From a String](https://stackoverflow.com/questions/1879395/how-do-i-generate-a-stream-from-a-string). – mojo Dec 21 '18 at 15:20
  • You want to send an executable file? Probably with some automation to run it at the destination? **SHREEK** Save your job and rethink. You need to extract it at the destination anyway. So send just data and keep the script at the destination (check the data for plausibility before inserting it to the database) (Most companies are blocking executable code in Emails anyway for security reasons) – Stephan Dec 21 '18 at 18:44
  • @Stephan, it's OK, it's an automated email to me. Lol – Ben Dec 22 '18 at 03:18

0 Answers0