Click here to Skip to main content
15,896,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i am trying to send mail from asp.net by embedding images its coming fine in gmail account but in yahoo and out look its not coming i am receving the mail but no content inside the mAIL


if i dont write this line



AlternateView avImages = AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html);

mail.AlternateViews.Add(htmlView);//body of the mail

resources.ForEach(x => avImages.LinkedResources.Add(x));//add all the linked resources in alternative View

mail.AlternateViews.Add(avImages);// Add the views for image


and simply add
mail.body = EmailContent;
then the mail comes in the yahoo ,outlook and gmail
C#
 System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                       SmtpClient smtp = new SmtpClient();
                   //do the attachemts to the mail
                   if (fup1.HasFile)
                   {
                       mail.Attachments.Add(new Attachment(fup1.PostedFile.InputStream, fup1.FileName));
                   }
        mail.To.Add(new MailAddress(ds.Tables[0].Rows[i]["Email"].ToString()));//recipent aaddress

                   mail.From = new MailAddress(ddlpromoemail.SelectedItem.Text.ToString().Trim());
                   mail.Subject = txtsubjectline.Text;
                   mail.IsBodyHtml = true;
                   string docid = ds.Tables[0].Rows[i]["EmployeeCode"].ToString();

                   ViewState["storeContent"] = EmailContent;

                   Regex reg = new Regex(@"s]+))?)+\s*|\s*)/?>");

                   AlternateView htmlView = AlternateView.CreateAlternateViewFromString("", null, "text/html");

                   List<LinkedResource> resources = new List<LinkedResource>();//using this to collect all the resourcess file


                   foreach (Match match in reg.Matches(EmailContent))//loop through all the image tag within the content and gget the image tag from thee
                   {

                       //pass the one by one image tag this function will convert the base 64 string into image file and  ssave it int images folder
                       //and give u the



                       string path = SaveImage(match.Value,ref err);//get the image path  save the base 64 string image file in folder


                       // Create the image resource from image path using LinkedResource class..

                           LinkedResource LinkedImage = new LinkedResource(@path);
                           //get the height
                           string height = Regex.Match(match.Value, "<img.+?height=[\"'](.+?)[\"'].+?>", RegexOptions.IgnoreCase).Groups[1].Value;
                           //get the width
                           string width = Regex.Match(match.Value, "<img.+?width=[\"'](.+?)[\"'].+?>", RegexOptions.IgnoreCase).Groups[1].Value;
                           LinkedImage.ContentType = new ContentType(MediaTypeNames.Image.Jpeg);
                           LinkedImage.ContentId = imgn + "MyPic";
                           string imgtag = "<img height='" + height + "'  width='" + width + "' src=cid:" + imgn + "MyPic" + " />";
                           EmailContent = EmailContent.Replace(match.Value, imgtag);
                           resources.Add(LinkedImage);//add all the linked resources
                           imgn++;//imgn will be used to give unique name to each contentid
                   }
                   //add image a bug
                   //append the emil adress to image tag
                   IFormatProvider provider = new System.Globalization.CultureInfo("en-CA", true);

                   CultureInfo ci = new CultureInfo("en-GB");
                   Thread.CurrentThread.CurrentCulture = ci;
                  // string fdg = "<img src='http://www.yoursite.com/beacon.aspx?emailId=xxx";

                   string url ="http://localhost:54645/View/MarketingEngine/beacon.aspx?email=" + ds.Tables[0].Rows[i]["Email"].ToString() + "&empcode=" + ds.Tables[0].Rows[i]["EmployeeCode"].ToString() + "&template=" + txtpromoemailname.Text + "&Dateins=" + DateTime.Now.ToString("dd-MMM-yyyy", ci);
                   string bgimg = "<img height="2" width="2">.*?)(""|').*?>(?<value>.*?)");

                   foreach (Match match in r.Matches(EmailContent))
                   {
                       string href = match.Groups["href"].Value.ToString().Trim();
                       string link_url = "http://localhost:54645/View/MarketingEngine/LinksClicked.aspx?email=" + ds.Tables[0].Rows[i]["Email"].ToString() + "&address=" + href + "&empcode=" + ds.Tables[0].Rows[i]["EmployeeCode"].ToString() + "&template=" + txtpromoemailname.Text + "&Dateins=" + DateTime.Now.ToString("dd-MMM-yyyy", ci);
                       EmailContent = EmailContent.Replace(href, link_url);
                   }
//end region

                   string html = "<!DOCTYPE html><html><body>" + EmailContent +"</body></html>";
                   AlternateView avImages = AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html);

                   mail.AlternateViews.Add(htmlView);//body of the mail

                   resources.ForEach(x => avImages.LinkedResources.Add(x));//add all the  linked resources in alternative View

                   mail.AlternateViews.Add(avImages);// Add the views for image 

                   smtp.Host = host;

                   smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                   smtp.Credentials = new System.Net.NetworkCredential(ddlpromoemail.SelectedItem.Text.ToString().Trim(),password);

                   //Or your Smtp Email ID and Password
                   smtp.EnableSsl = true;

                   //To request a read receipt, we need add a custom header named 'Disposition-Notification-To'
                   //in this , read receipts will go back to 'ddlBounceEmailid.SelectedItem.Text.ToString().Trim()'
                   //it's important to note that read receipts will only be sent by those mail clients that
                   //a) support them
                   //and
                   //b)have them enabled.
                   mail.Headers.Add("Disposition-Notification-To", "<" + ddlBounceEmailid.SelectedItem.Text.ToString().Trim() + ">");

                       mail.BodyEncoding = System.Text.Encoding.UTF8;
                       mail.SubjectEncoding = System.Text.Encoding.UTF8;


                           smtp.Send(mail);
Posted
Updated 9-Jul-13 1:08am
v4
Comments
sahabiswarup 9-Jul-13 8:49am    
have you checked your spam folder??
surajemo 9-Jul-13 9:35am    
yes i am reeving the email but without any content
surajemo 9-Jul-13 12:30pm    
hey i removed this two line its working now
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("", null, "text/html");
mail.AlternateViews.Add(htmlView);//body of the mail

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900