sending image in mail in asp
i am using c# code to sent the mail to another user. it works fine when i send text in it.. now i want to send image so that the image what i sent should open in his inbox. means the image should go as message body not the attachments.. i am using the code to sent the image as:
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);
but it is not sending the image as message..
please help me out..
This should work for you build your attachment
attach.ContentDisposition.Inline = true;
attach.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
attach.ContentId = contentID;
attach.ContentType.MediaType = "image/png";
Remove the img tag from the body and just do m.Attachments.Add(attach);
Please try this: Found 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
i think all u have to do is to specify the format of ur mail. i.e.
m.BodyFormat = MailFormat.Html;
maybe this will give u an alter idea. I had a similar task (coded it for intranet). so... what i did was - uploaded images to webserver, so that i already had URI's. and in code-behind i used these URIs as a src of the image.
mail.Body = "<html><body><img src='" + img_uri + "'></body></html>";
精彩评论