如何在身体内容中附加图像.我写了下面的代码
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); string UserName = "xyz@someorg.com"; string Password = "my password"; message.To.Add(new System.Net.Mail.MailAddress("toaddress@toadddress.com")); message.From = new System.Net.Mail.MailAddress("fromaddress@fromaddress.com"); message.Subject = "test subject"; message.Body = "<img src=@'C:\\Sunset.jpg'/>"; message.IsBodyHtml = true; System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(); smtpClient.Host = "hostname"; smtpClient.Port = 25; smtpClient.Credentials = new System.Net.NetworkCredential(UserName,Password); smtpClient.Send(message);
解决方法
string attachmentPath = Environment.CurrentDirectory + @"\test.png"; Attachment inline = new Attachment(attachmentPath); inline.ContentDisposition.Inline = true; inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline; inline.ContentId = contentID; inline.ContentType.MediaType = "image/png"; inline.ContentType.Name = Path.GetFileName(attachmentPath); message.Attachments.Add(inline);