santosh kumar
santosh kumar

Reputation: 43

need to embed the url inside hyperlink in mail

I have a button on an ASP.NET webpage. When the button is clicked an email is sent to users. Inside the body of the mail message I have the following URL; https://synerg.abc.com/abctool/Login.aspx.

Instead of having the full link visible, I just want to show a "Click Here" hyperlink which will take the user to the above URL when clicked. The URL should then be redirected to https://synerg.abc.com/documenttrackingtool/Login.aspx

protected void Uab_abc_Click(object sender, EventArgs e)
{
    SmtpClient smtpClnt;
    MailMessage objMail;

    // Prepare Email  
    // Load Email Template from HTML File. 

    // Send Email
    // Create new Mail Object
    objMail = new MailMessage(); // Create new Mail Object
    objMail.From = new MailAddress("[email protected]");

    //test mailers
    objMail.To.Add(new MailAddress("[email protected]"));
    objMail.CC.Add(new MailAddress("[email protected]"));
    objMail.Bcc.Add(new MailAddress("[email protected]"));

    objMail.Subject = "abc Delivery HNC";
    string EMailBody = @"  
        <table>  
            <tr>  
                <td>Dear L2 / L3 Heads,
                </td>
            <br/>  
            </tr> 
            <tr>  
                <td>Please find below pending abc Status.  Request delivery leaders to closely track UBR and ensure invoiced at the earliest.  
                </td>
            <br/>  
            </tr> 
            <tr>  
                <td>Recovery Status Date : 13th November 2019
                </td>  
            </tr>    
            <tr>  
                <td>  
                  <img src=""cid:InlineImageID"" /> 
                </td>
            </tr>
            <tr>  
                <td>You can also access this report using below link / Path
                </td> 
            </tr>
            <tr>  
                <td>Link : https://synerg.abc.com/tool/Login.aspx
                </td> 
            </tr>
            <tr>  
                <td>User Id : your Myabc User id or SAP code
                </td> 
            </tr>
            <tr>  
                <td>PWD : your Myabc password.
                </td> 
            </tr>
            <tr>  
                <td>Navigate to path :  DTT  Dashboard -> abc -> abc Delivery -> L1 / L2 files.
                </td> 
            </tr>
            <br/>
            <tr>  
                <td>Regards,
                </td> 
            </tr>
            <tr>  
                <td>abc Team.
                </td> 
            </tr></table>  
          ";

    // Now we'll create two email views: HTML and plain text
    // First, create HTML view
    AlternateView HTMLEmail = AlternateView.CreateAlternateViewFromString(EMailBody,
            null, "text/html");
    // Create plain text view for those visitors who prefer text only messages
    AlternateView PlainTextEmail = AlternateView.CreateAlternateViewFromString(EMailBody, null, "text/plain");

    // We'll need LinkedResource class to place an image to HTML email
    LinkedResource MyImage = new LinkedResource(Server.MapPath("~/images/abc_Delivery_L2_abcmail.png"));
    // Set ContentId property. Value of ContentId property must be the same as
    // the src attribute of image tag in email body. In this case it is 
    // <img src="cid:InlineImageID" />
    MyImage.ContentId = "InlineImageID";

    // Add this linked resource to HTML view
    HTMLEmail.LinkedResources.Add(MyImage);

    // Add plain text and HTML views to an email
    objMail.AlternateViews.Add(HTMLEmail);
    objMail.AlternateViews.Add(PlainTextEmail);

    // Open SMTP Connection to Mail Server and send email.
    smtpClnt = new SmtpClient();
    smtpClnt.Host = "10.99.134.21";
    smtpClnt.Port = Convert.ToInt16("25");
    smtpClnt.EnableSsl = false;
    smtpClnt.UseDefaultCredentials = false;

    smtpClnt.Send(objMail);

    smtpClnt.Dispose(); // Close the SMTP Mail server connection.
    objMail.Dispose(); // Clear the mail object

}

Upvotes: 0

Views: 1168

Answers (2)

Lutti Coelho
Lutti Coelho

Reputation: 2264

You should change this:

<tr>  
    <td>Link : https://synerg.abc.com/tool/Login.aspx
    </td> 
</tr>

To this:

<tr>  
    <td><a href="https://synerg.abc.com/tool/Login.aspx">click here</a>
    </td> 
</tr>

And this:

<tr>  
    <td>Recovery Status Date : 13th November 2019
    </td>  
</tr>  

To this:

<tr>  
    <td>Recovery Status Date : #dateToReplace
    </td>  
</tr>  

Also:

EMailBody = EMailBody.Replace("#dateToReplace", DateTime.Now.ToString("d MMMM yyyy"));

// Now we'll create two email views: HTML and plain text
// First, create HTML view
AlternateView HTMLEmail = AlternateView.CreateAlternateViewFromString(EMailBody,
        null, "text/html");

Upvotes: 1

citronas
citronas

Reputation: 19365

Replace

<td>Link : https://synerg.abc.com/tool/Login.aspx</td> 

with

<td>Link : <a href='https://synerg.abc.com/tool/Login.aspx'>Login</a></td> 

Replace Login with the text you want to display, e.g., click here


Edit: Also Replace

<td>Recovery Status Date : 13th November 2019</td> 

with

<td>Recovery Status Date : [RECOVERYDATE]</td> 

Also include

EMailBody = EMailBody.Replace("[RECOVERYDATE]", DateTime.Now.ToString("d MMMM yyyy"));

See https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings for more details about formatting dates.

Upvotes: 2

Related Questions