[英]Why are attachments not showing up in emails sent with Mimekit?
編輯message.writeto() 完全按照預期創建了電子郵件,並附有文件。 它只是在 Outlook 中沒有那樣顯示。
我會保持簡短。 我正在嘗試使用 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);
}
}
現在.. 電子郵件發送得很好,但由於某種原因,沒有附件被添加到電子郵件中,也沒有出現調試時出現的異常或錯誤。
請問有人可以提供幫助嗎?
我是個白痴,留下一行代碼改變了消息正文。
這個問題現在是多余的。
感謝 Jstedfast,這幫助我快速找到它:-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.