c# - 使用 session 邀请 C# 在正文中发送 Html

标签 c# asp.net asp.net-mvc asp.net-mvc-4

我需要发送带有 html 正文标记的 session 请求。我已成功发送 session 请求,它运行良好。

这是ICS文件格式

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//A//B//EN
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
ORGANIZER;CN="Organizer":mailto:#FROM#
ATTENDEE;CN="Attendee";CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=
 NEEDS-ACTION;RSVP=TRUE:mailto:#TO#
DTSTART:#DTSTART#
DTEND:#DTEND#
LOCATION:#LOCATION#
SUMMARY: Invitation for Meeting
TRANSP:OPAQUE
SEQUENCE:0
UID:#UID#
DTSTAMP:#CREATED-AT#
CREATED:#CREATED-AT#
LAST-MODIFIED:#CREATED-AT#
DESCRIPTION: #DESCRIPTION#
X-ALT-DESC;FMTTYPE=text/html: #X-ALT-DESC#
PRIORITY:5
CLASS:PUBLIC
END:VEVENT
END:VCALENDAR

我找到了一些链接来发送带有 X-ALT-DESC 选项的 html 链接,但它对我不起作用

Send an Outlook Meeting Request with C#

Send an Outlook Meeting Request with C#

Send email to Outlook with ics meeting appointment

Send email to Outlook with ics meeting appointment

这是发送带有 ICS 文件的电子邮件的 C# 代码

filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "Email/calenderInvitation.ics");
                fileContent = System.IO.File.OpenText(filePath).ReadToEnd();
                fileContent = fileContent.Replace("#TO#", receiver);
                fileContent = fileContent.Replace("#FROM#", fromAddress.Address);
                fileContent = fileContent.Replace("#LOCATION#", eventVenue);
                fileContent = fileContent.Replace("#UID#", Guid.NewGuid().ToString().Replace("-", ""));
                fileContent = fileContent.Replace("#CREATED-AT#", Convert.ToDateTime(meetingDate).ToString(TimeFormat));
                fileContent = fileContent.Replace("#DTSTART#", Convert.ToDateTime(startTime).ToString(TimeFormat));
                fileContent = fileContent.Replace("#DTEND#", Convert.ToDateTime(finishTime).ToString(TimeFormat));
                filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "Email/InvitationEmail.html");
                fileInviationContent = System.IO.File.OpenText(filePath).ReadToEnd();
                String body = AppendRedirectURl(fileInviationContent, callBackUrl);
                fileContent = fileContent.Replace("#X-ALT-DESC#", body);
                MailMessage message = new MailMessage();
               // message.IsBodyHtml = true;
                message.From = new MailAddress(fromAddress.Address);
                message.To.Add(new MailAddress(receiver));
                message.Subject = string.Format("{0}  {1} @ {2} {3} {4} {5} - {6}", "Invitation: ", meetingTypeName,
                    Convert.ToDateTime(meetingDate).ToString("dddd"), Convert.ToDateTime(startTime).ToString("MMMM"),
                    Convert.ToDateTime(startTime).ToString("yyyy"), startTime,
                    finishTime);

                var iCalendarContentType = new ContentType("text/calendar; method=REQUEST");
                var calendarView = AlternateView.CreateAlternateViewFromString(fileContent, iCalendarContentType);
                calendarView.TransferEncoding = TransferEncoding.SevenBit;
                message.AlternateViews.Add(calendarView);
                await smtp.SendMailAsync(message);

这是调试器的响应

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//A//B//EN
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
ORGANIZER;CN="Organizer":mailto:admin@wordflow.info
ATTENDEE;CN="Attendee";CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=
 NEEDS-ACTION;RSVP=TRUE:mailto:anandjaisy@gmail.com
DTSTART:20171206T081500Z
DTEND:20171206T090000Z
LOCATION:asdasd
SUMMARY: Invitation for Meeting
TRANSP:OPAQUE
SEQUENCE:0
UID:b17f15326c5343ff98d76bf6092ed2b4
DTSTAMP:20171212T000000Z
CREATED:20171212T000000Z
LAST-MODIFIED:20171212T000000Z
DESCRIPTION: #DESCRIPTION#
X-ALT-DESC;FMTTYPE=text/html: <html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
    <br><br><br>
    <form class="container">
        <div class="form-group">
            <label>Click on  Accept or Decline to add event to your Email Calender </label>
        </div>
        <div class="form-group">
            <label>Will you Attend the Meeting</label>
            <a href="https://localhost:44380/Meetings/GotMeetingConfirm?userId=08c5bb4f-f4f8-4183-a99a-e82544970dd4&meetingId=6c9747e2-aeb9-4d27-8db6-077ee1d9cee9&resposne=True" id="YesConfirmMeeting" class="btn btn-primary">Yes</a>
            <a href="https://localhost:44380/Meetings/GotMeetingConfirm?userId=08c5bb4f-f4f8-4183-a99a-e82544970dd4&meetingId=6c9747e2-aeb9-4d27-8db6-077ee1d9cee9&resposne=False" id="NoConfirmMeeting" class="btn btn-danger">No</a>
        </div>
        <div class="form-group text-center">

        </div>
    </form>


</body>
</html>
PRIORITY:5
CLASS:PUBLIC
END:VEVENT
END:VCALENDAR

它发送了邀请,但没有发送正文

最佳答案

我在此链接 (visual basic) 上找到了解决方案 https://social.msdn.microsoft.com/Forums/en-US/2cd0dcff-7d6c-493e-bf49-87a3e3248d01/create-meeting-request-with-html-body-in-aspnet?forum=netfxnetcom

基本上,您添加第二个 AlternateView。以下是我是如何做到的。我的“正文”字符串有我的 html。我确实注意到,如果我在“avHtmlBody”之前添加“avCalendar”,我的 session 在 Outlook 中会中断。所以我想顺序很重要。

在我的 meetingRequestString 中,我删除了 DESCRIPTION 和 X-ALT-DESC 部分。

MailMessage msg = new MailMessage(from, to);

var htmlContentType = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Html);
var avHtmlBody = AlternateView.CreateAlternateViewFromString(body, htmlContentType);
msg.AlternateViews.Add(avHtmlBody);

string meetingRequestString = GetMeetingRequestString(from, to, subject, body, startTime, endTime);
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("text/calendar");
ct.Parameters.Add("method", "REQUEST");
ct.Parameters.Add("name", "meeting.ics");
AlternateView avCalendar = AlternateView.CreateAlternateViewFromString(meetingRequestString, ct);
msg.AlternateViews.Add(avCalendar);
client.Send(msg);

关于c# - 使用 session 邀请 C# 在正文中发送 Html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47667812/

相关文章:

asp.net - COMETchat 应用程序 - IIS 7 随着时间的推移变慢

javascript - 将对象传递给计算函数的 knockout

c# - 如何使用MTM将参数值从TFS中的测试用例传递到单元测试方法中的测试方法?

c# - ASP.NET MVC 重定向 OnEnd

c# - ASP.NET CORE 中有意义的 URL 路由

c# - 在 EntityDataSource 中使用参数化的 LIKE 子句

asp.net-mvc - ASP.NET MVC : Returning a view with querystring intact

asp.net - WindowsAzure html utf-8 编码问题

c# - 将路径拆分为列表

c# - 创建 Windows 应用商店应用程序时出现问题。 (Windows 8)