0

Please I have a working code which send mails correctly only when I execute the application from visual studio, but when I generate the .exe file and install it, I'm not able to recieve the email ! I think it's not problem coming from code, but maybe something else.
a PDF file generated using NReco.Generator and attached to each mail (it's not problem of access because i'm able to send the same files to Box without problem).
What can be the problem ? everything working well in Visual Studio, but not after installing !
This is the code to generate and send file :

  void EnvoyerDOCAsync(string path)
    {

        SmtpClient MyMail = new SmtpClient(ConfigurationManager.AppSettings["server"], Convert.ToInt16(ConfigurationManager.AppSettings["port"]));
        MyMsg = new MailMessage();

        MyMsg.Priority = MailPriority.High;
        MyMsg.From = new MailAddress(ConfigurationManager.AppSettings["mail"], "Sesrvice Mailing");

        foreach (Fonction item in fonction.getFonctions())
        {
            MyMsg.To.Add(new MailAddress(item.FonctionMail, item.FonctionName));
        }

       MyMsg.Subject = "Hello";
        MyMsg.Body    = "Bonjour";
        MyMsg.SubjectEncoding = Encoding.UTF8;
        MyMsg.IsBodyHtml = true;
        MyMsg.BodyEncoding = Encoding.UTF8;
        MyMail.UseDefaultCredentials = false;
        MyMail.Timeout = (60 * 5 * 1000);
        NetworkCredential MyCredentials = new NetworkCredential(ConfigurationManager.AppSettings["sso"], ConfigurationManager.AppSettings["pass"]);
        MyMail.Credentials = MyCredentials;


        if (File.Exists(path))
        {
            if (path != null)
            {
                    Attachment attachment = new Attachment(path, MediaTypeNames.Application.Octet);

                    System.Net.Mime.ContentDisposition disposition = attachment.ContentDisposition;
                    disposition.CreationDate = File.GetCreationTime(path);
                    disposition.ModificationDate = File.GetLastWriteTime(path);
                    disposition.ReadDate = File.GetLastAccessTime(path);
                    disposition.FileName = Path.GetFileName(path);
                    disposition.Size = new FileInfo(path).Length;
                    disposition.DispositionType = DispositionTypeNames.Attachment;

                    MyMsg.Attachments.Add(attachment);
                    MyMail.SendAsync(MyMsg, null);
                    MyMail.SendCompleted += MyMail_SendCompleted;
            }
        }

    }



//Execute this after mail is sended to dispose the Msg and show validation message.<br/>
  private void MyMail_SendCompleted(object sender, AsyncCompletedEventArgs e)
    {

        MyMsg.Dispose();

       MessageBox.Show("The mail was sended succesfully", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }


Thank you.

  • How are you setting your path @Soufiane ? – Afnan Ahmad Nov 28 '18 at 07:15
  • Something like this : \\server\data\somewhere\Public\file.pdf It is working well because i'm able to send the file to box using this link, it is a file in the shared network –  Nov 28 '18 at 07:18
  • Did you debug your code? – koviroli Nov 28 '18 at 07:19
  • @koviroli yes, everything is working well, it doesn't work only when I install the application –  Nov 28 '18 at 07:20
  • When you debug the application, it runs under your account. When it is installed, it may run under another account, that doesn't have access to that path – Hans Kesting Nov 28 '18 at 07:22
  • (not the issue, but:) you should switch the null-check with the File.Exists check so you don't check whether a null-path exists – Hans Kesting Nov 28 '18 at 07:23
  • @Soufiane I am pretty sure problem is with your path. You need to make sure path is valid. Catch the exception and you will find the issue. – Afnan Ahmad Nov 28 '18 at 07:23
  • Click to your App.config > Properties > Copy to Output Directory and set Copy Always – koviroli Nov 28 '18 at 07:23
  • @HansKesting Thank you I switched the tests. –  Nov 28 '18 at 07:31
  • @AfnanAhmad I will catch it but why i'm able to send the same file to Box if there is a problem with the link ? –  Nov 28 '18 at 07:31
  • @koviroli I didn't really inderstand exactly what you mean, please. –  Nov 28 '18 at 07:31
  • @Soufiane When you will catch it you will find your answer. – Afnan Ahmad Nov 28 '18 at 07:33
  • @AfnanAhmad I have no errors, mail sended succesfully, and file sended to Box, but afrter generating .exe it send it to box, but don't send the mail. weird ! –  Nov 28 '18 at 07:41
  • @Soufiane no exception in the `try catch` ? – Afnan Ahmad Nov 28 '18 at 07:42

0 Answers0