I was trying to send mail from c#. Its working fine, the problem right now i am facing is i have map and driving direction bottom of the mail. I need the entire text to be a hyperlink, currently i see only portion of the text is hyperlink, if the text have any space then the hyperlink got removed after that.

Here is my code any one help me please?

  public static void SendMail(AppointmentRequest appointment)
        {
            var objMailMessage = new MailMessage();

            try
            {
                var client = new SmtpClient
                {
                    Port = Convert.ToInt32(ConfigurationManager.AppSettings["SmtpPort"]),
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Host = ConfigurationManager.AppSettings["SmtpHost"]
                };

                var hospitalEmail = ConfigurationManager.AppSettings["HospitalEmail"];
                var address = new MailAddress(hospitalEmail, appointment.Hospital.HospitalName);

                objMailMessage.From = address;

                //for testing added this code
                if (!Convert.ToBoolean(ConfigurationManager.AppSettings["IsProduction"]))
                {
                    address = new MailAddress(ConfigurationManager.AppSettings["TestEmail1"]);
                    objMailMessage.To.Add(address);
                    address = new MailAddress(ConfigurationManager.AppSettings["TestEmail2"]);
                    objMailMessage.To.Add(address);
                }
                else
                {
                    address = new MailAddress(appointment.NewClientInfo.EmailAddress);
                    objMailMessage.To.Add(address);
                }

                objMailMessage.Subject = "Your appointment request to " + appointment.Hospital.HospitalName +
                                         " has been submitted.";
                var htmlMessage = MessageTemplate(appointment);
                objMailMessage.Body = htmlMessage;
                client.Send(objMailMessage);
            }
            catch (SmtpException exception)
            {
                Utilities.HandleException(exception);
            }
            catch (Exception exception)
            {
                Utilities.HandleException(exception);
            }
        }

 public static string MessageTemplate(AppointmentRequest appointment)
        {
            var sbBuilder = new StringBuilder();

            sbBuilder.Append("Hi, \r\r");
            sbBuilder.Append("Your request for an appointment has been submitted. ");
            sbBuilder.Append("Someone from " + appointment.Clinics.Name +
                             " will contact you within one business day during our operating hours.");
            sbBuilder.Append("\r\r");
            sbBuilder.Append(appointment.Clinics.Name).Append("\r");
            sbBuilder.Append(appointment.Clinics.Address.Address1).Append("\r");
            sbBuilder.Append(appointment.Clinics.Address.City).Append("\r");
            sbBuilder.Append(appointment.Clinics.Address.StateOrProvince).Append("\r");
            sbBuilder.Append(appointment.Clinics.Address.Zipcode).Append("\r");
            sbBuilder.Append(appointment.Clinics.Address.Telephone1).Append("\r");
            sbBuilder.Append(appointment.Hospital.HospitalURL).Append("\r");

            var address = appointment.Clinics.Name + " " + appointment.Clinics.Address.Address1 + " " + appointment.Clinics.Address.City + " " + appointment.Clinics.Address.StateOrProvince + " " + appointment.Clinics.Address.Zipcode;
            var googleMapUrl = string.Format("{0}{1}", ConfigurationManager.AppSettings["GoogleMapUrl"], "?&daddr=");
            var homeUrl = googleMapUrl + address;
            //string.Format("Click <a href='{0}'>" + homeUrl + "</a> to login", homeUrl);
            sbBuilder.Append("Maps & Driving Directions: " + homeUrl);
            //sbBuilder.Append("Maps & Driving Directions: " + string.Format("Click <a href='{0}'>" + homeUrl + "</a> to login", homeUrl)).Append("\r");

            return sbBuilder.ToString();
        }

I tried anchor tag not working. This is my output, copy pasting only the url rest of the body content deleted.

http://maps.google.com/?&daddr=XXX 8000 Los Angeles CA 90069

I see hyper link till http://maps.google.com/?&daddr=XXX link missing for the rest of the text due to space.

Thanks

Related posts

Recent Viewed