编辑 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);
}
}
现在。。电子邮件发送正常,但是由于某种原因,没有附件添加到电子邮件中,并且没有出现调试时显示的异常或错误。
有人可以协助吗?
答案 0 :(得分:0)
我是个白痴,在其中留下了一行代码,从而更改了邮件正文。
这个问题现在是多余的。
感谢Jstedfast,这有助于我快速找到它:-)