locked
Unable to send clickable link in my email content [href portion gets stripped automatically] -- RRS feed

  • Question

  • User-215451226 posted

    Hi,

    Posting my problem here;
    I am trying to send an email from my app. It contains [supposed to] a clickable 'a href' link in the content. But as the mail is being delivered, the link is only being rendered as a plain text and not a <a href>. I am using a gmail acc. as the 'from'. But I tried sending emails to live, outlook as well as gmail and in all 3 this is happening. I have used IsBodyHtml=true and all that but to no avail. Does not work. What is the trick?

    Let me post my email code stub so you can evaluate --

    public IActionResult EmailInvoice([FromBody]EmailInvoice emlI)
            {
                string webRoot = _env.WebRootPath;
                string contentRootPath = _env.ContentRootPath;
    
                string invoiceHtml = emlI.invoice_content;
    ****this--> string linktoClick = "<a href='" + emlI.fdback_link.ToString() + "' style='color:indigo; font-size:24px'><b>Click here to visit feedback page and submit your feedback!</b></a>";
                
                try
                {
                    string email_from = _conf.GetValue<string>("MailSettings:email_from");
                    string email_to = _conf.GetValue<string>("MailSettings:email_to");
                    string password = _conf.GetValue<string>("MailSettings:password");
    
                    var contentID1 = "Image1";
                    var inlineLogo1 = new Attachment(webRoot+"/img/inv_logo.png");
                    inlineLogo1.ContentId = contentID1;
                    inlineLogo1.ContentDisposition.Inline = true;
                    inlineLogo1.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;
    
                    var contentID2 = "Image2";
                    var inlineLogo2 = new Attachment(webRoot + "/img/sig.png");
                    inlineLogo2.ContentId = contentID2;
                    inlineLogo2.ContentDisposition.Inline = true;
                    inlineLogo2.ContentDisposition.DispositionType = System.Net.Mime.DispositionTypeNames.Inline;
    
                    using (MailMessage msg = new MailMessage(email_from, email_to))
                    {
                        msg.Subject = "Invoice WebApp - Sales Invoice";
                        msg.Attachments.Add(inlineLogo1);
                        msg.Attachments.Add(inlineLogo2);
    		    msg.Body = invoiceHtml+linktoClick;
                        msg.IsBodyHtml = true;
                        msg.Priority = MailPriority.High;
                        msg.BodyEncoding = System.Text.Encoding.Default;
                        SmtpClient clnt = new SmtpClient();
                        clnt.Host = "smtp.gmail.com";
                        clnt.EnableSsl = true;
                        NetworkCredential ntwrkCr = new NetworkCredential(email_from, password);
                        clnt.Credentials = ntwrkCr;
                        clnt.Port = 587;
                        clnt.Send(msg);
                        string statusMesg = "Sales invoice sent to registered email address!";
    
    
                        return Json(statusMesg);
                    }
                }
                catch(Exception exp)
                {
                    return Json(new StandardError("Unable to send sales invoice"+exp.GetBaseException()));
                }
                
            }

    Do you see anything? What am I missing? Do I have to go and alter something from my mail sender 'from' account so that it does not strip off my href='' portions automatically? What needs to be done? I am sure others have faced this scenario before also. Kindly suggest a good workaround/solution.

    Thanks

    Friday, September 21, 2018 7:09 AM

All replies

  • User409696431 posted

    There is nothing in the code you presented that can tell us what is going on.  You are creating an HTML message body with a link in it.  (I'm assuming emlI.fdback_link.ToString() is a valid URL with no characters in it that would cause the HTML parsing to break.)  Where does that link get stripped?  We can't see that.

    Additional:  Does invoiceHtml (as the name would imply) contain any HTML (other than links)? Does that HTML go through correctly? If is does, it would point to something disabling links in your received emails.

    Friday, September 21, 2018 11:49 PM
  • User-215451226 posted

    Hi @KathyW Kathleen,

    In my scenario, 'invoiceHtml is a div rom a page in my app, from which I am reading the content as a stream and using that to create my email content. Yes, it gets sent okay, correctly. Although the images and their alignment don't quite appear in the email as they are originally in the div. But that's different issue.

    Yes, the [a href] that I am trying to create looks something like -->

    <a href="http://localhost:16303/Home/Feedback?fd_id=&55tyhzG011$C201871617GGtvcxzOpPTRV21" style="color:indigo; font-size:24px">
        <b>Click here to visit feedback page and submit your feedback!</b>
    </a>

    I verified it okay during debugging and using breakpoint. Upto that point it is getting created fine and is okay. It is during sending that this all gets stripped [I am guessing].  Could it be the characters in the fd_id querystring that's making it working wrongly while sending the email? I used a gmail acc. as my [from]. Any settings/modifications needed there?


    That I verified using breakpoints.

    Saturday, September 22, 2018 11:27 AM
  • User475983607 posted

    I'm unable to reproduce this issue and have no problem SMTPing an HTML formatted email with a link through gmail.

    Either there is a bug in the code, a gmail client configuration issue, or the gmail app stripped the href for some reason.  Since you verified the code is working then it must gmail.  You have the email so start by viewing the email source.  Does the source match what was sent?  If source matches then you have a gmail client configuration issue.  If the source does not match, you're probably violating some gmail security rule.  Either case and assuming the ASP.NET code is working properly, this is not an ASP.NET issue.

    What client are you using to read email?  Can you post the email HTML source sent and received?

    Saturday, September 22, 2018 12:26 PM
  • User-215451226 posted

    Hi there,

    To read the mail I used gmail, live and outlook. All 3. Same case in all three.
    Let me post the stuff that is going into building the email content --

    1.  this is the source I get during debugging msg.Body --

    [https://pastebin.com/NaDr9wPU]

    2. this is the email source code finally delivered --

    [https://pastebin.com/NY5j7Lye]

    To help you understand how the thing looks while debugging in msg.Body --

    [https://imagebin.ca/v/4GbFDiLwzryU]
    And that link at the very bottom is very much clickable cuz when I move my cursor over it, it changes to 'hand'.

    So the msg.Body does not exactly 100% match the email body delivered. But it is mostly negligible and not much related to the hyperlink. I think it is still some gmail security. What could it be, any guesses?


    [nb] - sorry for using external links.

    Saturday, September 22, 2018 5:09 PM
  • User-271186128 posted

    Hi Sir,

    I suggest you could try to refer to the following code to send emails from outlook to gmail, and verify whether the link works well.

            public async Task<ActionResult> Contact()
            {
                ViewBag.Message = "Your contact page.";
    
    
                var body = "Hello " + "<a href='" + "https://www.google.com/" + "' style='color:indigo; font-size:24px'><b>Click here to visit feedback page and submit your feedback!</b></a>";
                ;
                var message = new MailMessage();
                message.To.Add(new MailAddress("recipient@outlook.com"));  // replace with valid value 
                message.From = new MailAddress("sender@gmail.com");  // replace with valid value
                message.Subject = "Your email subject";
                message.Body = string.Format(body);
                message.IsBodyHtml = true;
    
                using (var smtp = new SmtpClient())
                {
                    var credential = new NetworkCredential
                    {
                        UserName = "user@gmail.com",  // replace with valid value
                        Password = "Password"  // replace with valid value
                    };
                    smtp.Credentials = credential;
                    smtp.Host = "smtp-mail.outlook.com";
                    smtp.Port = 587;
                    smtp.EnableSsl = true;
                    await smtp.SendMailAsync(message);
                    return RedirectToAction("About");
                };
    
                    //return View();
            }

    Then change the Host and from/To mail address to send email from gmail to outlook, to verify whether the link works well.

    If the link button works well when you send email from outlook mail, it means your issue is related to the gmail, I suggest you could contact the gmail team.

    Here are some related articles about send emails in asp.net mvc, you could refer to them:

    https://www.mikesdotnetting.com/article/268/how-to-send-email-in-asp-net-mvc

    https://www.aspsnippets.com/Articles/GMAIL-Error-The-SMTP-server-requires-a-secure-connection-or-the-client-was-not-authenticated.aspx 

    Best regards,
    Dillion

    Monday, September 24, 2018 7:37 AM