使用内部超链接在C#中创建Outlook .ics文件

时间:2015-03-12 19:43:40

标签: c# html outlook

下面的代码假设生成一个内置链接的.ics文件。 它生成.ics但问题是,链接显示的是html代码而不是Hyperlink。任何想法都可以提供帮助。

(这是一个重新发布,因为我之前的帖子没有显示整个代码) 感谢。

public ActionResult ExportToOutlook(Guid id)
{
    var query = new GetActivityByIdQuery();
    var queryResult = query.Execute(id);
    var outlookCalendarFileCreator = new OutlookCalendarFileCreator();
    var contentData = outlookCalendarFileCreator.CreateOutlookCalendarFile(queryResult);

    var fileName = String.Format("Activity.ics", queryResult.ActivityType.Name, queryResult.Date) ;
    return File( contentData,"text/plain", queryResult.Notes + fileName);
}


public class OutlookCalendarFileCreator
{    
    public Byte[] CreateOutlookCalendarFile(ActivityDetailsResponseDto responseDto)
    {
    byte[] contentData;
    HttpRequest request = System.Web.HttpContext.Current.Request;
    string baseUrl = request.Url.GetLeftPart(UriPartial.Authority);
    var hLink = string.Format("{0}/Activities/Details?id={1}", baseUrl, responseDto.Id);
    var sb = new StringBuilder();
    sb.Append(@"X-ALT-DESC;FMTTYPE=text/html:<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 3.2//EN"">\n<HTML><BODY>");
    sb.Append(string.Format("<a href='{0}'>{1}</a>", hLink, responseDto.Notes));
    sb.Append("</BODY></HTML>");

    using (var memoryStream = new MemoryStream())
    {
        using (var writer = new StreamWriter(memoryStream))
        {
            writer.AutoFlush = true;
            //HEADER
            writer.WriteLine("BEGIN:VCALENDAR");
            writer.WriteLine("VERSION:1.0");
            writer.WriteLine("BEGIN:VEVENT");

            //BODY
            writer.WriteLine("DTSTART:" + responseDto.Date.ToUniversalTime().ToString("o"));
            writer.WriteLine("DTEND:" + responseDto.Date.AddHours(1).ToUniversalTime().ToString("o"));
            writer.WriteLine("SUMMARY:" + responseDto.ActivityType.Name);
            writer.WriteLine("DESCRIPTION:" + sb.ToString());

            //FOOTER
            writer.WriteLine("END:VEVENT");
            writer.WriteLine("END:VCALENDAR");
            writer.Flush();
            contentData = new byte[(int)memoryStream.Length];
            memoryStream.Position = 0;
            memoryStream.Read(contentData, 0, (int)memoryStream.Length);
            return contentData;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

再次,你有&#34;:&#34;在描述之后。你需要&#34 ;;&#34; - 分号与结肠。

    sb.Append(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 3.2//EN"">\n<HTML><BODY>");
    sb.Append(string.Format("<a href='{0}'>{1}</a>", hLink, responseDto.Notes));
    sb.Append("</BODY></HTML>");
...
    writer.WriteLine("DESCRIPTION;X-ALT-DESC;FMTTYPE=text/html:" + sb.ToString());