3

What I want to do is to create a mail attachment from a string file and give it my own custom extension.

    public void CreateAttachment(string encryptedString, string fileName)
    {
        mail.Attachments.Add(Attachment.CreateAttachmentFromString(encryptedString, fileName));
    }

I want to create a ".bzk" file and it's gonna be opened in a mobile Android application later. How do I do this? I know there are different constructors as you can see here!

I do not know what constructors I have to use and even after trying different MIME types and Encodings, it won't work. Can anyone tell me how to do this?

edit:

I've solved my problem. Here is my solution:

public void CreateAttachment(string encryptedString, string fileName)
    {
        MemoryStream ms = new MemoryStream(
            ASCIIEncoding.ASCII.GetBytes(encryptedString)
        );

        Attachment attachment = new Attachment(ms, new ContentType("text/bzk"));
        attachment.Name = fileName + ".bzk";

        mail.Attachments.Add(attachment);

    }
Frederik Struck-Schøning
  • 12,981
  • 8
  • 59
  • 68
S. ten Brinke
  • 2,557
  • 4
  • 25
  • 50

2 Answers2

3

I've solved my problem. Here is my solution:

public void CreateAttachment(string encryptedString, string fileName)
{
    MemoryStream ms = new MemoryStream(
        ASCIIEncoding.ASCII.GetBytes(encryptedString)
    );

    Attachment attachment = new Attachment(ms, new ContentType("text/bzk"));
    attachment.Name = fileName + ".bzk";

    mail.Attachments.Add(attachment);
}
Frederik Struck-Schøning
  • 12,981
  • 8
  • 59
  • 68
S. ten Brinke
  • 2,557
  • 4
  • 25
  • 50
0

Try loosing the last string argument (filename).

I assume the following

  • string encryptedString is the encrypted content of the file
  • string fileName is the name you want the attachment to have.

If this is correct, then what I would do is, first create the file with the filename, and then attach the file.

terle
  • 86
  • 10