Neo
Neo

Reputation: 16239

Web forms : sending image into email not displaying in outlook?

I'm sending an image through web form the image is displayed well into gmail ,yahoo etc. But i'm not able to view that image when I send it on Outlook what would be wrong please help.

Code:

//Variable mg have a html code with  'cid:uniqueId1’ calls image from function SendMail12


void birthday()
        {
            Coonection con = new Coonection();
            sql = "select es.PORTNO,es.SERVERNAME,es.EMAILID,es.PASSWORD,e.efrom,e.SerNo,e.eto,e.ecc,e.emessage,e.eflag,e.EmpID,e.FIRSTNAME,e.LASTNAME,e.DeptName,e.FromDate,e.Todate,e.Reason from EMAILSETTING es,EmailSender e";


            SqlDataAdapter da = new SqlDataAdapter(sql, con.GetConnection());
            DataSet ds = new DataSet();
            da.Fill(ds);

            string sub = "Message";
            int portnum, n,sn;
            string servername1, passwo, account, t, ecc, mg, flage,mg2;
            sql = "select count(srno)from EmailSender";
            SqlCommand cmd = new SqlCommand(sql, con.GetConnection());
            // dr = cmd.ExecuteReader();
            n = Convert.ToInt32(cmd.ExecuteScalar());
            //while (i)
            //{
            for (int i = 0; i < n; i++)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {

                    portnum = Convert.ToInt32(ds.Tables[0].Rows[0]["PORTNO"].ToString());
                    servername1 = ds.Tables[0].Rows[0]["SERVERNAME"].ToString();      //Table(0).Rows(0)("SmtpServerName").ToString();
                    account = ds.Tables[0].Rows[0]["EMAILID"].ToString();
                    passwo = ds.Tables[0].Rows[0]["PASSWORD"].ToString();
                    t = ds.Tables[0].Rows[i]["eto"].ToString();
                    ecc = ds.Tables[0].Rows[i]["ecc"].ToString();
                    mg2 = ds.Tables[0].Rows[i]["emessage"].ToString();
                    flage = ds.Tables[0].Rows[i]["eflag"].ToString();
                    sn = Convert.ToInt32(ds.Tables[0].Rows[i]["SerNo"].ToString());

                    if (flage == "N" && mg2 == "Wish You a Very Happy Returns of the Day.<P> From - Daccess Security Systems Pvt ")
                    {
                        mg = " <html> " +
                            "<body background= 'meet.jpg'>" +
                           "<head>  " +
                           "<title>Untitled Document</title>  " +
                           "<meta content='text/plain; charset=us-ascii' http-equiv='Content-Type' />" +
                           "<style type='text/css'>  " +
                           ".style1 {  " +
                               "font-family: Arial, Helvetica, sans-serif;  " +
                               "font-weight: bold;  " +
                               "font-size: 18px;  " +
                               "color: #3333CC;  " +
                           "}  " +
                           ".style3 {  " +
                               "font-family: 'Times New Roman', Times, serif;  " +
                               "color: #003399;  " +
                           "}    " +
                           ".style4 {  " +
                               "font-family: Arial, Helvetica, sans-serif;  " +
                               "font-weight: bold;  " +
                               "font-size: 12px;  " +
                           "}  " +
                           ".style11 {  " +
                               "color: #1A588D;  " +
                               "font-family: 'Times New Roman', Times, serif;  " +
                               "font-weight: bold;  " +
                           "}  " +
                           ".style15 {  " +
                               "color: #1A588D;  " +
                               "font-weight: bold;  " +
                           "}  " +
                           ".style18 {color: #FFFFFF; font-weight: bold; }  " +
                           "body {  " +
                               "margin-top: 1px;  " +
                               "margin-left: 1px;  " +
                               "margin-right: 1px;  " +
                               "margin-bottom: 1px;  " +

                           "}  " +
                           "</style>  " +
                           "<link href='Untitled-1.htm' id='1' title='view'>  " +
                           "<script language='JavaScript' type='text/JavaScript'>  " +
                           "function MM_reloadPage(init) {   " +
                             "if (init==true) with (navigator) {if ((appName=='Netscape')&&(parseInt(appVersion)==4)) {  " +
                               "document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}  " +
                             "else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();  " +
                           "}  " +
                           "MM_reloadPage(true);  " +
                           "</script>  " +
                           "<style type='text/css'>  " +
                           "a:link {  " +
                               "text-decoration: none;  " +
                           "}  " +
                           "a:visited {  " +
                               "text-decoration: none;  " +
                           "}  " +
                           "a:hover {  " +
                               "text-decoration: none;  " +
                           "}  " +
                           "a:active {  " +
                               "text-decoration: none;  " +
                           "}  " +
                           ".style20 {font-size: 12px}  " +
                           "</style></head>  " +
                           "<body>  " +
                           "<p class='style1'><img src='cid:uniqueId' width='145' height='111'></p>  " +
                           "<table border= 1 align ='left'>" +
                           "<tr>" + "<td>Employee ID :" + "</td>" +
                           "<td>" + ds.Tables[0].Rows[i]["EmpID"].ToString() + "</td>" +
                           "</tr>" +
                           "<tr>" + "<td>Employee Name :" + "</td>" +
                           "<td>" + ds.Tables[0].Rows[i]["FIRSTNAME"].ToString() + " " + ds.Tables[0].Rows[i]["LASTNAME"].ToString() + "</td>" +
                           "</tr>" +
                           "<tr>" + "<td>Department :" + "</td>" +
                           "<td>" + ds.Tables[0].Rows[i]["DeptName"].ToString() + "</td>" +
                           "</tr>" +
                           "<tr>" + "<td>Message :" + "</td>" +
                           "<td>" + ds.Tables[0].Rows[i]["emessage"].ToString() + "</td>" +
                           "</tr>" +
                           "<tr>"+
                           "<td>"+
                           "<pre align='left' class='style3'><strong><img src='cid:uniqueId1' width='286' height='177'></strong></pre>  " +
                           "<pre align='left' class='style4'> <a name='t'></a> Please do not reply to this email.</pre>" +
                           "<p align='left' class='style3'>&nbsp;</p>  " +
                           "</td>"+"</tr>"+"</table>"+
                           "</body>  " +
                           "</html>  ";

                        if (flage == "N" && mg2 == "Wish You a Very Happy Returns of the Day.<P> From - Daccess Security Systems Pvt ")
                        {
                            SendMail12(account, passwo, t, sub, mg, ecc, servername1, portnum);
                            sql = "Update EmailSender set eflag ='Y' where SerNo = '" + sn.ToString() + "'";
                            SqlCommand cmd4 = new SqlCommand(sql, con.GetConnection());
                            cmd4.ExecuteNonQuery();
                        }
                    }
                }
                else
                {

                    servername1 = "";
                    portnum = 0;
                    account = "";
                    passwo = "";
                    MessageBox.Show("Email Setting Is Not Stored");
                }
            }
        }

// Send mail function
public static bool SendMail12(string gMailAccount, string password, string to, string subject, string message, string cc, string server, int port)
      {
          try
          {


              NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password);
              MailMessage msg = new MailMessage();
              msg.From = new MailAddress(gMailAccount);
              msg.To.Add(new MailAddress(to));

              string[] s;
              s = cc.Split(';');

              for (int i = 0; i < s.Length; i++)
              {
                  msg.CC.Add(new MailAddress(s[i].ToString()));
              }

              msg.Subject = subject;
              msg.Body = message;
              msg.IsBodyHtml = true;

              SmtpClient client = new SmtpClient();
              client.Host = server;
              client.Port = port;
              client.Timeout = 100000;
              client.EnableSsl = true;
              client.UseDefaultCredentials = true;
              client.Credentials = loginInfo;
              string palinBody = "Plain text content, viewable by those clients that don't support html";
              AlternateView plainView = AlternateView.CreateAlternateViewFromString(palinBody, null, "text/plain");
              string htmlBody = message;
              AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, "text/html");
              //create the AlternateView for embedded image 
              AlternateView imageView = new AlternateView("D:\\Aniket Work\\New Folder Update\\EmailSender\\EmailSender\\images\\Daccess-logo.gif", MediaTypeNames.Image.Gif);
              imageView.ContentId = "uniqueId";
              imageView.TransferEncoding = TransferEncoding.Base64;

              AlternateView imageView1 = new AlternateView("D:\\Aniket Work\\New Folder Update\\EmailSender\\EmailSender\\images\\birthday1images.jpg", MediaTypeNames.Image.Jpeg);
              imageView1.ContentId = "uniqueId1";
              imageView1.TransferEncoding = TransferEncoding.Base64;
              //add the views 
              msg.AlternateViews.Add(plainView);
              msg.AlternateViews.Add(htmlView);
              msg.AlternateViews.Add(imageView);
              msg.AlternateViews.Add(imageView1);

              client.Send(msg);

              return true;
          }
          catch (Exception e)
          {
              return false;
          }
      }

enter image description here

Upvotes: 1

Views: 927

Answers (1)

ajay_whiz
ajay_whiz

Reputation: 17951

You should be giving absolute paths to your resources

e.g.

in above code, your body background should be something like

<body background= 'http://yourdomain.com/meet.jpg'>

same thing applies to your image tag or you can have inline content disposition

EDIT

Do you see something like this when you view the mail?

enter image description here

Upvotes: 1

Related Questions