1

In a C# desktop App I send a mail to a specific user and it has an image and the end of the body as shown below

MailMessage email = new MailMessage();
                        email.To.Add(new MailAddress(correo));
                        email.From = new MailAddress("email@email.com");
                        email.Subject = "some text ( " + DateTime.Now.ToString("dd / MMM / yyy hh:mm:ss") + " ) ";
                        email.Body = "some text";
                        email.IsBodyHtml = true;
                        email.Priority = MailPriority.Normal;

                        string text = "some text";
                        AlternateView plainView = AlternateView.CreateAlternateViewFromString(text, Encoding.UTF8, MediaTypeNames.Text.Plain);

                        LinkedResource LinkedImage = new LinkedResource(@"C:\Users\myUser\Documents\App\CPresentacion\Resources\comunicaciones.jpg");
                        LinkedImage.ContentId = "ImagenGCI";
                        LinkedImage.ContentType = new ContentType(MediaTypeNames.Image.Jpeg);
                        AlternateView htmlView = AlternateView.CreateAlternateViewFromString(
                          "some text <br/><img src=cid:ImagenGCI>", Encoding.UTF8,
                                                    MediaTypeNames.Text.Html);

                        htmlView.LinkedResources.Add(LinkedImage);
                        email.AlternateViews.Add(htmlView);
                        email.AlternateViews.Add(plainView);


                        SmtpClient smtp = new SmtpClient();
                        smtp.Host = "192.xxx.x.xxx";
                        smtp.Port = 25;
                        smtp.EnableSsl = false;
                        smtp.UseDefaultCredentials = false;
                        smtp.Credentials = new NetworkCredential("email@email.com", "");
                        string output = null;

                        try
                        {
                            smtp.Send(email);
                            email.Dispose();
                            output = "Correo electrónico fue enviado satisfactoriamente.";
                            CargarDGV();
                            Limpiar(this);
                        }
                        catch (Exception ex)
                        {
                            output = "Error enviando correo electrónico: " + ex.Message;
                        }

and it works fine when I send the email from my computer but it cannot find the image when another user in another computer tries to sen the email from the App, I know the problem is in the linkedResource path but don´t know how to specify the path to work in another computer, this is my project' folders, could you please help to specify the correct path in order to work in any specific computer

enter image description here

  • possible duplicate (https://stackoverflow.com/questions/3259583/how-to-get-files-in-a-relative-path-in-c-sharp) – Ryan Wilson Nov 09 '18 at 17:21
  • If I add both views, html and plain, then when receiving emails it always shows in plain mode. If I add only html then it always shows html. Is there a way to prioritize this, so it first tries to load html view if something goes wrong then it loads the plain? – Tiger Galo Jul 27 '20 at 18:35

1 Answers1

0

With this line of code:

            var path = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);

You can get the directory path when your app is installed and form the proper route to your resources.