Search code examples
c#smtpclient

part of the network path is omitted from the email body because of the space in network path


I am trying to send an email using C# console application to our employees. In the body of the email, I need to embed a network path. I am using stringBuilder to create the body of the email. Network path is like this:

\\pcm-test2\shared\PCM_GEO_TEST\NGS Forms

I created the C# code to embed the network path like this:

StringBuilder TelURL = new StringBuilder().Append("<a href=" + @"\\pcm-test2\shared\PCM_GEO_TEST\NGS Forms
").Append(">HERE</a>"); 

I put the above string in the body of the email in C#. When I receive the email, I see the link that says "HERE", but when I hover over "HERE", I see, the network path like this: \pcm-test2\shared\PCM_GEO_TEST\NGS

somehow the "Forms" part is not showing up in the body of the email. This is my complete code:

public void sendEmail(List<string> toList)
    {
        string from = "[email protected]";
        string UserName = ConfigurationManager.AppSettings["UserName"].ToString();
        string password = ConfigurationManager.AppSettings["password"].ToString();
        StringBuilder TelURL = new StringBuilder().Append("<a href=" + @"\\pcm- test2\shared\PCM_GEO_TEST\NGS Forms
").Append(">HERE</a>"); 
    
        using (MailMessage mm = new MailMessage())
        {
            mm.From = new MailAddress(from);

            mm.To.Add(new MailAddress("[email protected]"));
          
            mm.Subject = "Test subject";
            mm.Body = TelURL.ToString();
            mm.IsBodyHtml = true;

            SmtpClient client = new SmtpClient();

            client.Credentials = new NetworkCredential(UserName, password);
            client.Port = 111;
            client.EnableSsl = true;
            client.Host = "smtp.office365.com";
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Send(mm);

        }


    }

How can I put the entire network path with "Forms" in the body of the email. when I try using

var URL = $"<a href='{System.Net.WebUtility.UrlEncode(@"\\pcm-test2\shared\PCM_GEO_TEST\NGS Forms")}'>HERE</a>"; 

It shows like this in email body with a plus sign as you can see the image:

enter image description here

Any help will be appreciated.


Solution

  • Replace the TelURL string with this:

    StringBuilder TelURL = new StringBuilder().Append("<a href=\"" + @"\\pcm-test2\shared\PCM_GEO_TEST\NGS Forms" + "\">HERE</a>");
    

    entire code will be like this:

    public void sendEmail(List<string> toList)
        {
            string from = "[email protected]";
            string UserName = ConfigurationManager.AppSettings["UserName"].ToString();
            string password = ConfigurationManager.AppSettings["password"].ToString();
       StringBuilder TelURL = new StringBuilder().Append("<a href=\"" + @"\\pcm-test2\shared\PCM_GEO_TEST\NGS Forms" + "\">HERE</a>"); 
        
            using (MailMessage mm = new MailMessage())
            {
                mm.From = new MailAddress(from);
    
                mm.To.Add(new MailAddress("[email protected]"));
              
                mm.Subject = "Test subject";
                mm.Body = TelURL.ToString();
                mm.IsBodyHtml = true;
    
                SmtpClient client = new SmtpClient();
    
                client.Credentials = new NetworkCredential(UserName, password);
                client.Port = 111;
                client.EnableSsl = true;
                client.Host = "smtp.office365.com";
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Send(mm);
    
            }
    
    
        }