首页 > 解决方案 > 为什么附件不显示在使用 Mimekit 发送的电子邮件中?

问题描述

编辑 message.writeto() 完全按照预期创建了电子邮件,并附加了文件。它只是没有出现在前景中。

我会保持简短。我正在尝试使用 MimeKit 从 winforms 发送电子邮件,并且在手动尝试对文件流进行编码时遇到解析错误后选择了构建器方法。

所以,我尝试过的:

公共字符串作为“文件路径”

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

打开文件对话框的事件:

        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
                }
            }    
        }

然后在消息创建中:

            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("notthatstupid@notthisaddress.com", "notthispassword");

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

            }
        }

现在.. 电子邮件发送得非常好,但由于某种原因,电子邮件中没有添加任何附件,调试时也没有出现异常或错误。

请问有人可以帮忙吗?

标签: c#winformsmimekit

解决方案


我是个白痴,留下一行代码改变了消息正文。

这个问题现在是多余的。

感谢 Jstedfast,这帮助我快速找到它:-)


推荐阅读