Sending Mail Along with Embedded Image Using ASP.NET

sending mail along with embedded image using asp.net

If you are using .NET 2 or above you can use the AlternateView and LinkedResource classes like this:

string html = @"<html><body><img src=""cid:YourPictureId""></body></html>";
AlternateView altView = AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html);

LinkedResource yourPictureRes = new LinkedResource("yourPicture.jpg", MediaTypeNames.Image.Jpeg);
yourPictureRes.ContentId = "YourPictureId";
altView.LinkedResources.Add(yourPictureRes);

MailMessage mail = new MailMessage();
mail.AlternateViews.Add(altView);

Hopefully you can deduce the VB equivalent.

ASP.Net : Send email with Images embedded in Rich Text HTML body

Try this template.

It helps to use smtp.port=25

  try 
{
MailMessage msg = new MailMessage ();
MailAddress fromAdd = new MailAddress("fromemail@email.com");
msg.[To].Add("toemail@email.com");
msg.Subject = "Choose Session Members";
msg.From = fromAdd;
msg .IsBodyHtml = true;
msg.Priority = MailPriority.Normal;
msg .BodyEncoding = Encoding.Default;
msg.Body = "<center><table><tr><td><h1>Your Message</h1><br/><br/></td></tr>";
msg.Body = msg.Body + "</table></center>";
SmtpClient smtpClient = new SmtpClient ("smtp.yourserver.com", "25");
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("yourname@yourserver.com", "password");
smtpClient .DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Send(msg);
smtpClient.Dispose();
}

Send inline image in email

Try this

 string htmlBody = "<html><body><h1>Picture</h1><br><img src=\"cid:filename\"></body></html>";
AlternateView avHtml = AlternateView.CreateAlternateViewFromString
(htmlBody, null, MediaTypeNames.Text.Html);

LinkedResource inline = new LinkedResource("filename.jpg", MediaTypeNames.Image.Jpeg);
inline.ContentId = Guid.NewGuid().ToString();
avHtml.LinkedResources.Add(inline);

MailMessage mail = new MailMessage();
mail.AlternateViews.Add(avHtml);

Attachment att = new Attachment(filePath);
att.ContentDisposition.Inline = true;

mail.From = from_email;
mail.To.Add(data.email);
mail.Subject = "Client: " + data.client_id + " Has Sent You A Screenshot";
mail.Body = String.Format(
"<h3>Client: " + data.client_id + " Has Sent You A Screenshot</h3>" +
@"<img src=""cid:{0}"" />", att.ContentId);

mail.IsBodyHtml = true;
mail.Attachments.Add(att);

sending image with other details in mail body

You should not add physical/relative path to image file in your email body as the email client will most likely be not able to access the image at the physical location. Right approach would be to embed image with your email by attaching the image as an attachment with the email and then using content id of the attached image in the images' source attribute in mail body HTML.

Code excerpt below:-

Attachment att = mail.AddAttachment( "d:\\img\\image.jpg" ); //Path to the image file
string contentId = "Image1";
att.ContentID = contentId; //content id can be any string value
mail.HtmlBody = "<html><body><img src=\"cid:" + contentId + "\"></body></html>"; //Use content id as image source

Reference links:-

https://www.emailarchitect.net/easendmail/ex/c/15.aspx

Send inline image in email

How to send an email with image that includes embedded auto-generated link?

You have to send Image as part HTML. Use following code

myemail.Body = "<h1>Quiz!</h1><img src=/fulladdress/someimage.png onclick="location.href='myPage.html'">";

myemail.IsBodyHtml = true; //Send this as plain-text

I have taken help from these links. Hope it would be also Helpfull to you

  1. http://www.intstrings.com/ramivemula/c/how-to-send-an-email-using-c-net-with-complete-features/

  2. Send a email with a HTML file as body (C#)

UPDATE

Store your Quiz URL in Database, so that it could be changed every week
Use image map to create a part of image clickable. Build the html as below.

//in this case your newLink would be default.aspx/testid=12
string newLink = GetNewLinkFromDB();

string html = "<h1>Quiz!</h1><img src=/fulladdress/someimage.png usemap ="#clickMap">";
html += "<map id =\"clickMap\" name=\"clickMap\">
<area shape =\"rect\" coords =\"0,0,82,126\" href ="+ newLink +" alt=\"Quiz\" />
</map>"

Update 2

protected void SendEmail(string toAddresses, string fromAddress, string MailSubject, string MessageBody, bool isBodyHtml)
{
SmtpClient sc = new SmtpClient("MailServer");
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("test@mailServer.com", "TestSystem");

//QuizLink is appSetting inside your web config
string newLink = System.Configuration.ConfigurationManager.AppSettings["QuizLink"].ToString();

string html = "<h1>Quiz!</h1><img src=/fulladdress/someimage.png usemap ="#clickMap">";
html += "<map id =\"clickMap\" name=\"clickMap\">
<area shape =\"rect\" coords =\"0,0,82,126\" href ="+ newLink +" alt=\"Quiz\" />
</map>"

msg.Bcc.Add(toAddresses);
msg.Subject = MailSubject;
msg.Body = html ;
msg.IsBodyHtml = isBodyHtml;
sc.Send(msg);
}
catch (Exception ex)
{
throw ex;
}

}

**UPDATE **

string html = "<h1>Quiz!</h1><img src='" + src + "' usemap ='#clickMap'>";
html += "<map id =\"clickMap\" name=\"clickMap\">" +
"<area shape =\"rect\" coords =\"0,0,82,126\" href =" + quickLink + "alt=\"Quiz\" title='Click For Quiz'/></map>";

how to send email with multiple embedded images with asp.net 4.0 with c#

class Program
{
static void Main(string[] args)
{
SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();

// Set sender email address, please change it to yours
oMail.From = "test@emailarchitect.net";

// Set recipient email address, please change it to yours
oMail.To = "support@emailarchitect.net";

// Set email subject
oMail.Subject = "test html email with attachment";

// Your SMTP server address
SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net");

// User and password for ESMTP authentication, if your server doesn't require
// User authentication, please remove the following codes.
oServer.User = "test@emailarchitect.net";
oServer.Password = "testpassword";

// If your SMTP server requires SSL connection, please add this line
// oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

try
{
// Import html body and also import linked image as embedded images.
oMail.ImportHtml( "<html><body>test <img src=\"test.gif\"> importhtml</body></html>",
"c:\\my picture", //test.gif is in c:\\my picture
ImportHtmlBodyOptions.ImportLocalPictures | ImportHtmlBodyOptions.ImportCss );

Console.WriteLine("start to send email with embedded image...");
oSmtp.SendMail(oServer, oMail);
Console.WriteLine("email was sent successfully!");
}
catch (Exception ep)
{
Console.WriteLine("failed to send email with the following error:");
Console.WriteLine(ep.Message);
}
}
}

}

Send mail with Embedded image win form c#

I guess you can store your pictures wherever you want. Simply modify your line:

LinkedResource pic1 = new LinkedResource("arrow.png", MediaTypeNames.Image.Jpeg);

... including the complete path. For example, if you got the path stored in a variable called PICTURES_PATH, you can do:

LinkedResource pic1 = new LinkedResource(System.IO.Path.Combine(PICTURES_PATH, "arrow.png"), MediaTypeNames.Image.Jpeg);

Hope that helps.



Related Topics



Leave a reply



Submit