HTML & Powershell - HTML Mail body, how to create a link from body to attachment (xlsx) in the same email

Posted on 2011-05-13
Last Modified: 2012-05-11

Please advice,

I created a mail body in html, and I woul like to link a link to the attachment which is included in the email. I would like to click the link in the HTML body of email and it woul automatically open the attached attachement. Would this be possible? I tried to google, but not much results. I am using Powershell to send email, and include attachments.

Many thanks

<body >
 <h2> Key: </h2>
 <ol  >
 <li> <span><a href="default2.aspx" >SubcontractorsExport1.csv</a> -- Export of subcontractors without the direct manager or accountexpires. </span></li>  
 <li> <span>SubcontractorsExport2.csv -- Export of subcontractors whose account expires later than in 4 months. </span></li> 

Open in new window

Question by:Radim88
    LVL 8

    Expert Comment

    Add this code to your html mail body.
    Attached file name and name in the href attribute must be identical
    <a href="attached_file_name.xlsx">open attached file</a>

    Open in new window


    Author Comment

    Sorry this doesnt work, It opens browser with empty page.
    LVL 8

    Accepted Solution

    Work for me.

    Look here:
    The solution is below, however, this only works for a single attachment. When there are multiple attachments my email client only wants to open the first one attached regardless of the fact that each file link has a unique content id... Uncertain of "why" this happens at this point but the main objective is resolved.

    MailMessage msg = new MailMessage("", "");
    msg.Subject = "Test";
    Attachment atch = new Attachment("c:\\SomeTestDoc.pdf");
    atch.ContentId = Guid.NewGuid().ToString();
    msg.IsBodyHtml = true;
    msg.Body = string.Format("This is my <a href=cid:{0}>file</a>", atch.ContentId);

    SmtpClient client = new SmtpClient("localhost", 25);

    Author Closing Comment

    thx it really works:)

    <a href=cid:subcontractorsExport3.csv>file</a>subcontractorsExport3.csv</a>

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
    Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
    In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now