I am using sendgrid to send images in mails, but I am not sure what's wrong with my code. Here is my code:
string imgPath= Server.MapPath(@"~/Images/logo-img.png");
StringBuilder sb = new StringBuilder();
sb.Append("Hi There! I am using sendgrid to send images");
sb.Append("<a href='someurl'><img src='cid:myImage.png> Login in</a>');
LinkedResource lr = new LinkedResource(imgPath, "image/png");
lr.ContentId = "myImage";
AlternateView AV = AlternateView.CreateAlternateViewFromString(sb.ToString(), null, "image/png");
AV.LinkedResources.Add(lr);
var from = new EmailAddress("[email protected]");
var subject = "Hi There!";
var to = new EmailAddress("[email protected]");
var plainTextContent = "Hello";
var htmlContent = sb.ToString();
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
msg.SetFrom(from, "abc");
var sendingResult = SendMail(msg);
CodePudding user response:
You put quote in wrong place:
sb.Append("<a href='someurl'><img src='cid:myImage.png'> Login in</a>");
CodePudding user response:
Your main problem is your image url. In email section you can't load image by using direct from your directory for that you must have to upload on some server and use that link of image in your img src. You can use like that
sb.Append("<a href='https://someserverurl.com/front-layout/images/confirmpass_Artboard 1.png'> Login in</a>");
This will solve your problem. Your image must be on some live link to be accessable.