Creating In-Memory Mail Attachments

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 8 months ago.

  • Author
    Posts
  • #5899

    by thepathfinder at 2012-08-17 05:57:05

    Hi,
    What I am trying is to send a string ($myString) in an email directly as attachment (.txt file). OR, writing string ($myString) into memory and creating mail attachment reading the memorystring directly and sending mail. I do not want any external file dependancy (eg. creating & deleting file). I found a C# code. Trying to convert this into powershell. Just need suggestion on conversion if it is possible?

    // Create a memory stream
    using (MemoryStream memoryStream = new MemoryStream())
    {
    byte[] contentAsBytes = Encoding.UTF8.GetBytes(fileContentTextBox.Text);
    memoryStream.Write(contentAsBytes, 0, contentAsBytes.Length);

    // Set the position to the beginning of the stream.
    memoryStream.Seek(0, SeekOrigin.Begin);

    // Create attachment
    ContentType contentType = new ContentType();
    contentType.MediaType = MediaTypeNames.Application.Octet;
    contentType.Name = fileNameTextBox.Text;
    Attachment attachment = new Attachment(memoryStream, contentType);

    // Add the attachment
    message.Attachments.Add(attachment);

    // Send Mail via SmtpClient
    smtpClient.Send(message);
    }

    by JeffH at 2012-08-17 06:13:43

    You don't want any file dependencies yet you want to end up with a file. Why bother with an attachment then? Why not simply send your string as the body of the mail message.

    by thepathfinder at 2012-08-17 08:11:53

    Hi, The reason is, the output that goes to logfile contains long-line texts, which basically got broken in email body and making unreadable. So wanted to send it in a TEXTFILE instead as email attachment, where I get original shape of the texts (just a horizontal scrollbar might appear, but purpose fulfilled).

    by JeffH at 2012-08-17 08:20:52

    Ok.Then I don't see where your original idea is any better. I also think the code sample you are looking at doesn't lend itself to a PowerShell script. So it seems the real issue here is getting the logfile into an email-friendly format. You could clean up the log file and send it as an attachment. Perhaps reformatting the log as html and sending it as an HTML body would work.

    Again, I don't see how doing this "in memory" solves anything.

    I hope this makes sense.

You must be logged in to reply to this topic.