Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a custom file attachment for an email from a string

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);

    }
like image 622
S. ten Brinke Avatar asked Nov 02 '25 12:11

S. ten Brinke


1 Answers

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);
}
like image 63
S. ten Brinke Avatar answered Nov 04 '25 02:11

S. ten Brinke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!