我正在使用c#代码将邮件发送给其他用户。 它在我发送文本时工作正常.. 现在我想发送图像,以便我发送的图像应该在他的收件箱中打开。意味着图像应该作为消息体而不是附件。 我正在使用代码将图像发送为:
System.Net.Mail.Attachment attach =
new System.Net.Mail.Attachment(
"C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Winter.jpg");
Random Rgen = new Random();
attach.ContentId = Rgen.Next(100000, 9999999).ToString();
attach.ContentDisposition.Inline = true;
MailMessage m = new MailMessage();
m.From = new MailAddress("swe@gmail.com");
m.To.Add(new MailAddress("qwe@gmail.com"));
m.IsBodyHtml = true;
m.Body = "<html><body><h1>Picture</h1><br><img src='cid:" + attach.ContentId + "'></body></html>";
//m.Body = inline.ToString();
// m.Body = "<img src='cid:" + attach.ContentId + "'>";
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Credentials = new System.Net.NetworkCredential("swe@gmail.com", "swe");
client.EnableSsl = true;
client.Send(m);
但它没有将图像作为信息发送..
请帮帮我..
答案 0 :(得分:0)
这适用于您构建附件
attach.ContentDisposition.Inline = true;
attach.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
attach.ContentId = contentID;
attach.ContentType.MediaType = "image/png";
从正文中删除img标记,然后执行m.Attachments.Add(attach);
答案 1 :(得分:0)
我认为您所要做的就是指定邮件的格式。即。
m.BodyFormat = MailFormat.Html;
也许这会给你一个改变主意。我有一个类似的任务(编码为内部网)。所以...我做的是 - 上传图像到网络服务器,所以我已经有了URI。在代码隐藏中,我使用这些URI作为图像的src。
mail.Body = "<html><body><img src='" + img_uri + "'></body></html>";
答案 2 :(得分:0)
请尝试:找到here
Public Sub EmbeddedImages()
'create the mail message
Dim mail As New MailMessage()
'set the addresses
mail.From = New MailAddress("from@fromdomain.com", " Display Name")
mail.To.Add("to@todomain.com")
'set the content
mail.Subject = "This is an embedded image mail"
'first we create the Plain Text part
Dim palinBody As String = "This is my plain text content, viewable by
those clients that don't support html"
Dim plainView As AlternateView =
AlternateView.CreateAlternateViewFromString(palinBody, Nothing,
"text/plain")
'then we create the Html part
'to embed images, we need to use the prefix 'cid' in the img src value
Dim htmlBody As String = "<b>This is the embedded image
file.</b><DIV> </DIV>"
htmlBody += "<img alt="""" hspace=0 src=""cid:uniqueId"" align=baseline
border=0 >"
htmlBody += "<DIV> </DIV><b>This is the end of Mail...</b>"
Dim htmlView As AlternateView =
AlternateView.CreateAlternateViewFromString(htmlBody, Nothing,
"text/html")
'create the AlternateView for embedded image
Dim imageView As New AlternateView("c:\attachment\image1.jpg",
MediaTypeNames.Image.Jpeg)
imageView.ContentId = "uniqueId"
imageView.TransferEncoding = TransferEncoding.Base64
'add the views
mail.AlternateViews.Add(plainView)
mail.AlternateViews.Add(htmlView)
mail.AlternateViews.Add(imageView)
'send mail
SendMail(mail)
End Sub ' End EmbedImages
HTH