0

I have a working code from many years, but something wrong happened in windows 10 from 2 months (was working in windows 10 for few months then stopped working from 2 months on same pc's). I used to create image and save it in d:\ then send it as attachment in email. From 2 months the email stopped sending the image file, I noticed that the image saved on the hard disk, when SmtpServer1.Send(mail1); is activated, now I solved the problem with very stupid way, I send empty email to my self first, then the image saved, then I send the required email with the attachment later, what had happened from 2 months. Is it windows update or security or what? I tested the program in many PC's with Windows 10 and changed the image location, but I get the same result. Please some one help me and send code to force the created image to be saved. my code is:

var webBrowser = (WebBrowser)sender;

using( Bitmap bitmap = new Bitmap( 800, webBrowser.Height ) )
{
    webBrowser.DrawToBitmap( bitmap, new Rectangle(0, 0, 800, bitmap.Height ) );
    bitmap.Save( @"d:\wisetemp\wisesoftware.jpg", ImageFormat.Jpeg );
//next 2 lines solved the problem by forcing the file to be saved , thanks to M.Hassan
    var stream = new MemoryStream();
    bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
}

if( File.Exists( @"d:\wisetemp\wisesoftware.jpg" ) ) 
    mail.Attachments.Add( new Attachment( @"d:\wisetemp\wisesoftware.jpg" ) );

smtpServer.Port = 587;
smtpServer.Credentials = new NetworkCredential( Form1.sender_email, Form1.seder_email_pass );
smtpServer.EnableSsl = true;
smtpServer.Send( mail );
  • You can maybe try to look here: http://stackoverflow.com/a/18358680/6576684 http://stackoverflow.com/a/1113439/6576684 – J. Willumsen Jul 17 '16 at 14:19

1 Answers1

-1

modify your code and throw exception if not sent to trace the root of the problem

  if( File.Exists( @"d:\wisetemp\wisesoftware.jpg" ) ) 
  {
    mail.Attachments.Add( new Attachment( @"d:\wisetemp\wisesoftware.jpg"   ));

   smtpServer.Port = 587;
   smtpServer.Credentials = new NetworkCredential( Form1.sender_email,   Form1.seder_email_pass );
   smtpServer.EnableSsl = true;
 smtpServer.Send( mail );
}
else
{

  //print log information about image size ,location , email send status to help fix problem
   throw Exception ("somethong wrong ...");
 }

Edit:

I used your code and complete missing variables and it's working correctly. Can you try it I think the problem may be in creating image from the browser. i set the browser height

   public void Test1(string fromEmailAddress, string password)
    {
        SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
        WebBrowser webBrowser = new WebBrowser();
        webBrowser.Height = 100;
        var fname = @"wisesoftware.jpg";

        try
        {
            using (Bitmap bitmap = new Bitmap(800, webBrowser.Height))
            {
                webBrowser.DrawToBitmap(bitmap, new Rectangle(0, 0, 800, bitmap.Height));
                bitmap.Save(fname, ImageFormat.Jpeg);
            }
            Attachment attachment = new Attachment(fname, MediaTypeNames.Application.Octet);
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(fromEmailAddress);
            mail.To.Add(fromEmailAddress);
            mail.Subject = "Test send image";
            mail.Body = "mail with image attachment";

            mail.Attachments.Add(attachment);

            if (File.Exists(fname))
            {
                smtpServer.Port = 587;
                smtpServer.Credentials = new NetworkCredential(fromEmailAddress, password);
                smtpServer.EnableSsl = true;
                smtpServer.Send(mail);
            }
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.ToString());
        }
    }

Edit 2:

send image as stream

  public void Test2(string fromEmailAddress, string password)
    {
        SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
        WebBrowser webBrowser = new WebBrowser();
        webBrowser.Height = 100;
        var fname = @"wisesoftware.jpg";

        try
        {
            var stream = new MemoryStream();
            using (Bitmap bitmap = new Bitmap(800, webBrowser.Height))
            {
                webBrowser.DrawToBitmap(bitmap, new Rectangle(0, 0, 800, bitmap.Height));
                bitmap.Save(fname, ImageFormat.Jpeg);

                //saving the image to a MemoryStream, and then providing that MemoryStream to the attachment constructor:

                bitmap.Save(stream, ImageFormat.Jpeg);
                stream.Position = 0;

            }
            Attachment attachment = new Attachment(fname, MediaTypeNames.Application.Octet);
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(fromEmailAddress);
            mail.To.Add(fromEmailAddress);
            mail.Subject = "Test send image";
            mail.Body = "mail with image attachment";
            mail.Attachments.Add(new Attachment(stream, "image/jpg"));
           // mail.Attachments.Add(attachment);

                smtpServer.Port = 587;
                smtpServer.Credentials = new NetworkCredential(fromEmailAddress, password);
                smtpServer.EnableSsl = true;
                smtpServer.Send(mail);

        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.ToString());
        }
    }

Edit 3:

//saving image as stream , send email with stream online  and finally save stream to disk
    public void Test3(string fromEmailAddress, string password)
    {
        SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
        WebBrowser webBrowser = new WebBrowser();
        webBrowser.Height = 100;
        var fname = @"wisesoftware.jpg";

        try
        {
            var stream = new MemoryStream();
            using (Bitmap bitmap = new Bitmap(800, webBrowser.Height))
            {
                webBrowser.DrawToBitmap(bitmap, new Rectangle(0, 0, 800, bitmap.Height));
                //saving the image to a MemoryStream, and then providing that MemoryStream to the attachment constructor:
                bitmap.Save(stream, ImageFormat.Jpeg);
                stream.Position = 0;

            }
            //add stream with defined friendly filename and mediaType as attachment
            Attachment attachment = new Attachment(stream, fname, MediaTypeNames.Image.Jpeg);
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(fromEmailAddress);
            mail.To.Add(fromEmailAddress);
            mail.Subject = "Test send image";
            mail.Body = "mail with image attachment";
            mail.Attachments.Add(attachment);

            smtpServer.Port = 587;
            smtpServer.Credentials = new NetworkCredential(fromEmailAddress, password);
            smtpServer.EnableSsl = true;
            smtpServer.Send(mail);

            //write stream to file , close stream
            using (FileStream file = new FileStream(fname, FileMode.Create, FileAccess.Write))
            {
                stream.Position = 0;
                stream.WriteTo(file);
                stream.Close();
            }
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.ToString());
        }
    }
M.Hassan
  • 10,282
  • 5
  • 65
  • 84
  • their is no problem, just the file not created in the folder, i check the folder, the file created after smtpServer.Send( mail ); only, even if i waited for hours, i tested it step by step – Osama Eissa Jul 17 '16 at 15:12
  • so, "Just the file not created in the folder" , so the problem in creating the file before sending it. if File.Exists(...) insure that the file is physically exist on the disk whatever sent or not by email. Try to save the file to other location by code (comment your smtpserver code for the time beiing ), and feedback your result. – M.Hassan Jul 17 '16 at 16:09
  • i changed the location , the same, i changed the pc it self , the same, and as i said before, when the smtpServer.Send( mail ); executed, i can see the file correctly in the folder, something prevent it from creating until smtpServer.Send( mail ); is executed, so i used this bug to send fake email to me first , so the file is created in the harddisk and then i resend the email with the saved file to the required email , – Osama Eissa Jul 17 '16 at 16:24
  • the problem is not in the creating process or sending as attachment process as i can send the file correctly, the problem is that the image stay in cache or something like that unitl the smtpServer.Send( mail ); executed, and by the way the code was working for years on windows 7,8 and for months on windows 10, suddenly it stopped working on all pc's at same time , i think this happened by update from windows 10, but what smtpServer.Send( mail ); do? , if some one know, please help me – Osama Eissa Jul 17 '16 at 16:24
  • you use browser built-in capabilities to create the image based on browser height. So browser is one of the players of the problem (and may be the cause), except you insure that image really on disk and not in cache, and can be opened by painter., as a test. Simply if i want to make a unit test to be sure the code is working , i should inspect the image file information to isolate the problem. let us do that task ( as a unit test :) ) – M.Hassan Jul 17 '16 at 16:45
  • test1 not working , had u tested it on windows 10 updated pc?, as i said before their is nothing in the image as it is saved after first smtpServer.Send( mail ); executed, i need a code to force saving the file after bitmap.Save(fname, ImageFormat.Jpeg); in windows 10 – Osama Eissa Jul 17 '16 at 21:14
  • Try to save image as PNG or Bitmap. May be some restrictions for jpg format in Web browser, Microsoft Edge.. – M.Hassan Jul 17 '16 at 21:41
  • i tried this too , the same , file didnt save to harddisk – Osama Eissa Jul 17 '16 at 22:11
  • Alternative2: try send image as stream..Try Test2(.) – M.Hassan Jul 17 '16 at 22:48
  • my friend , test2 saved correctly but with black image, but i took the idea from u and modified it, i addedd : var stream = new MemoryStream(); bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); after bitmap.Save(@"d:\wisetemp\wisesoftware.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);, it seems this forced the image to be saved – Osama Eissa Jul 18 '16 at 10:30
  • Welcome friend, happy to hear that you resolved the problem. You can save the memory stream to a file. I will post new version Test3 to save stream to file. – M.Hassan Jul 18 '16 at 11:59
  • BTW, I run Test1() on windows 10 , and it's working correctly :). – M.Hassan Jul 18 '16 at 22:28