Adding <img> HTML tag to C# String

扶醉桌前 提交于 2021-02-10 13:36:11

问题


I'm trying to email an image via Code behind but I can't get it to work. Basically trying to send the tag with a src so that it will display it in the email I sent:

Code behind

MailMessage mm = new MailMessage("mail-master@website.com", email);
mm.Subject = "Beta Signup";
string body = "<img src=\"\"http://popl.mpi-sws.org/2015/logos/google.png\"\" />";
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new System.Net.NetworkCredential("mail-master@website.com", "pass@123");
smtp.EnableSsl = true;
smtp.Send(mm);

However this doesn't send the image and I can't see the image showing up in the email I receive

string body = "<img src=\"\"http://popl.mpi-sws.org/2015/logos/google.png\"\" />";

回答1:


You are including two quotes to enclose the src attribute value.

"<img src=\"\"http://popl.mpi-sws.org/2015/logos/google.png\"\" />";

This will result in invalid HTML:

<img src=""http://popl.mpi-sws.org/2015/logos/google.png"" />

Try replacing with:

string body = "<img src=\"http://popl.mpi-sws.org/2015/logos/google.png\" />";


来源:https://stackoverflow.com/questions/32665354/adding-img-html-tag-to-c-sharp-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!