Jack Smith
Jack Smith

Reputation: 33

Why are attachments not showing up in emails sent with Mimekit?

Edit The message.writeto() created the email exactly as expected, with file attached. it's just not showing up like that in outlook.

i'll keep it short. i'm trying to send an email from winforms using MimeKit, and have opted for the builder approach after getting a parse error when manually attempting to code the filestream.

So, what i've tried:

Public string as the "filepath"

        public string ReturnAttachment1
        {
            get { return attachment1; }
            set { attachment1 = value; }
        }

Event for open file Dialog:

        private void button2_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.InitialDirectory = "c:\\";
                openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                openFileDialog.FilterIndex = 2;
                openFileDialog.RestoreDirectory = true;

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    ReturnAttachment1 = openFileDialog.FileName;
                    textBox19.Text = ReturnAttachment1.ToString();
                    //Note: textBox19 correctly displays filepath string
                }
            }    
        }

Then inside the message creation:

            TextPart body1 = new TextPart("html")
            {
                Text = @"Please See Below Information" + "<br/>" +
                      "<h4>Return ID: " + "  " + Returnid + "</h4>" + "<br/>" +
                      "<b>Fabricator Name:</b>" + "  " + Fname + "<br/>" + Environment.NewLine +
                      "<b>Account Number:</b>" + "  " + Facc + "<br/>" + Environment.NewLine +
                      "<b>Address Line 1:</b>" + "  " + Fadd1 + "<br/>" + Environment.NewLine +
                      "<b>Address Line 2:</b>" + "  " + Fadd2 + "<br/>" + Environment.NewLine +
                      "<b>Town:</b>" + "  " + Ftown + "<br/> " + Environment.NewLine +
                      "<b>County:</b>" + "  " + Fcounty + "<br/>" + Environment.NewLine +
                      "<b>Postcode:</b>" + "  " + Fpostcode + "<br/>" + Environment.NewLine +
                      "<b>Phone:</b>" + "  " + Fphoneno + "<br/>" + Environment.NewLine +
                      "<b>Email:</b>" + "  " + Femail + "<br/>" + Environment.NewLine + "<br/>" +
                      "<br/>" +

                      "<b>Invoice: </b>" + "  " + Inv + "<br/>" +
                      "<b>Material Information:</b>" + "<br/>" +
                      //slab 1
                      "<b>Thickness: </b>" + "  " + Thick1 + "mm" + "<br/>" +
                      "<b>Material Name: </b>" + "  " + Material1 + "<br/>" +
                      "<b>Batch No: </b>" + "  " + Batch1 + "<br/>" +
                      "<b>Reason for Return: </b>" + "  " + Reason1 + "<br/>" +
                      "<br/>" +
                      "<b>Notes:" + "  " + Notes
            };

            //check for return attachment and if found, assign attachment to message via MultiPart()
            if (ReturnAttachment1.Length > 7)
            {
                var builder = new BodyBuilder();
                builder.TextBody = body1.Text;
                builder.HtmlBody = body1.Text;
                builder.Attachments.Add(ReturnAttachment1.ToString());

                //now set the multipart mixed as the message body
                message.Body = builder.ToMessageBody();
            }
            else
            {
                message.Body = body1;
            }
                //Connection to SMTP and Criteria to Send
            using (var client = new SmtpClient())
            {
                // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
                client.ServerCertificateValidationCallback = (s, c, h, e) => true;

                client.Connect("smtp.gmail.com", 587, false);

                // Note: only needed if the SMTP server requires authentication
                client.Authenticate("[email protected]", "notthispassword");

                client.Send(message);
                client.Disconnect(true);

            }
        }

Now.. The email sends perfectly fine, but for some reason, no attachments are being added to the email, and no exceptions or errors are occuring that show up on debug.

Is anyone able to assist please?

Upvotes: 0

Views: 900

Answers (1)

Jack Smith
Jack Smith

Reputation: 33

i was an idiot and left a line of code in that changed the message body.

this question is now redundant.

Thanks to Jstedfast, this helped me to find it fast :-)

Upvotes: 0

Related Questions