C# 如何在不使用Outlook和C的情况下发送约会周期# 我在这里所做的是发送一封带有重复和重复的电子邮件 这已自动添加到收件人的Outlook日历中 这很有效 另一方面,我的服务器没有Outlook,因此我希望 在不使用Outlook的情况下发送约会周期

C# 如何在不使用Outlook和C的情况下发送约会周期# 我在这里所做的是发送一封带有重复和重复的电子邮件 这已自动添加到收件人的Outlook日历中 这很有效 另一方面,我的服务器没有Outlook,因此我希望 在不使用Outlook的情况下发送约会周期,c#,asp.net,email,outlook,C#,Asp.net,Email,Outlook,HTML: <asp:Label ID="lbError" runat="server"></asp:Label> <br /> <asp:Button ID="btSent" runat="server" OnClick="btSent_Click" Text="Sent" /> <br /> <br /> <asp:Label ID="lbRecur" runat="server"></asp:Labe

HTML:

<asp:Label ID="lbError" runat="server"></asp:Label>
<br />
<asp:Button ID="btSent" runat="server" OnClick="btSent_Click" Text="Sent" />
<br />
<br />
<asp:Label ID="lbRecur" runat="server"></asp:Label>
    protected void btSent_Click(object sender, EventArgs e)
    {
        SendMail("huydq@abc.com", "ITD");
    }

    public void SendMail(string targetMail, string shownTargetName)
    {
        MailAddress fromAddress = new MailAddress("huydq@abc.com", "MailSendingProgram");
        MailAddress toAddress = new MailAddress(targetMail, shownTargetName);
        String fromPassword = "mypassword";
        String subject = "Test Recurrence";
        String body =
              @"
                    Here you can put in any text that will appear in the body
                    multilined and even in <html>
                ";
        SmtpClient smtp = new SmtpClient
        {
            Host = "RSC-MAIL2K7.abc.com",
            Port = 25,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
        };

        using (MailMessage message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body
        }
              )
        {
            try
            {
                smtp.Send(message);
                lbError.Text = "E-Mail sent!";
                Outlook.Application olApp = new Outlook.Application();
                CreateNewRecurringAppointment(olApp);
                Marshal.ReleaseComObject(olApp);
            }
            catch
            {
                lbError.Text = "Sending failed, check your internet connection!";
            }
        }
    }

    public void CreateNewRecurringAppointment(Outlook._Application OutlookApp)
    {
        Outlook.AppointmentItem appItem = null;
        Outlook.RecurrencePattern pattern = null;
        try
        {
            appItem = OutlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem)
               as Outlook.AppointmentItem;
            // create a recurrence
            pattern = appItem.GetRecurrencePattern();
            pattern.RecurrenceType = Outlook.OlRecurrenceType.olRecursWeekly;
            pattern.StartTime = DateTime.Parse("8:35:00 AM");
            pattern.EndTime = DateTime.Parse("9:35:00 PM");
            // we can specify the duration instead of using the EndTime property
            pattern.Duration = 60;
            pattern.PatternStartDate = DateTime.Parse("07/23/2014");
            pattern.PatternEndDate = DateTime.Parse("07/31/2014");
            appItem.Subject = "Meeting with the Boss";
            appItem.Body = "Test Appointment body";
            appItem.Location = "P1";
            appItem.ReminderSet = true;
            appItem.ReminderMinutesBeforeStart = 15;
            appItem.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh;
            appItem.BusyStatus = Microsoft.Office.Interop.Outlook.OlBusyStatus.olBusy;
            appItem.Save();
            appItem.Send();
            //appItem.Display(true);
        }
        catch (Exception ex)
        {
            lbRecur.Text = ex.Message;
        }
        finally
        {
            if (pattern != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pattern);
            }
            if (appItem != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(appItem);
            }
        }
    }




C#:

<asp:Label ID="lbError" runat="server"></asp:Label>
<br />
<asp:Button ID="btSent" runat="server" OnClick="btSent_Click" Text="Sent" />
<br />
<br />
<asp:Label ID="lbRecur" runat="server"></asp:Label>
    protected void btSent_Click(object sender, EventArgs e)
    {
        SendMail("huydq@abc.com", "ITD");
    }

    public void SendMail(string targetMail, string shownTargetName)
    {
        MailAddress fromAddress = new MailAddress("huydq@abc.com", "MailSendingProgram");
        MailAddress toAddress = new MailAddress(targetMail, shownTargetName);
        String fromPassword = "mypassword";
        String subject = "Test Recurrence";
        String body =
              @"
                    Here you can put in any text that will appear in the body
                    multilined and even in <html>
                ";
        SmtpClient smtp = new SmtpClient
        {
            Host = "RSC-MAIL2K7.abc.com",
            Port = 25,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
        };

        using (MailMessage message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body
        }
              )
        {
            try
            {
                smtp.Send(message);
                lbError.Text = "E-Mail sent!";
                Outlook.Application olApp = new Outlook.Application();
                CreateNewRecurringAppointment(olApp);
                Marshal.ReleaseComObject(olApp);
            }
            catch
            {
                lbError.Text = "Sending failed, check your internet connection!";
            }
        }
    }

    public void CreateNewRecurringAppointment(Outlook._Application OutlookApp)
    {
        Outlook.AppointmentItem appItem = null;
        Outlook.RecurrencePattern pattern = null;
        try
        {
            appItem = OutlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem)
               as Outlook.AppointmentItem;
            // create a recurrence
            pattern = appItem.GetRecurrencePattern();
            pattern.RecurrenceType = Outlook.OlRecurrenceType.olRecursWeekly;
            pattern.StartTime = DateTime.Parse("8:35:00 AM");
            pattern.EndTime = DateTime.Parse("9:35:00 PM");
            // we can specify the duration instead of using the EndTime property
            pattern.Duration = 60;
            pattern.PatternStartDate = DateTime.Parse("07/23/2014");
            pattern.PatternEndDate = DateTime.Parse("07/31/2014");
            appItem.Subject = "Meeting with the Boss";
            appItem.Body = "Test Appointment body";
            appItem.Location = "P1";
            appItem.ReminderSet = true;
            appItem.ReminderMinutesBeforeStart = 15;
            appItem.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh;
            appItem.BusyStatus = Microsoft.Office.Interop.Outlook.OlBusyStatus.olBusy;
            appItem.Save();
            appItem.Send();
            //appItem.Display(true);
        }
        catch (Exception ex)
        {
            lbRecur.Text = ex.Message;
        }
        finally
        {
            if (pattern != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pattern);
            }
            if (appItem != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(appItem);
            }
        }
    }
protectedvoidbtsent\u单击(对象发送方,事件参数e)
{
发送邮件(“huydq@abc.com“,“ITD”);
}
public void SendMail(字符串targetMail,字符串shownTargetName)
{
MailAddress fromAddress=新邮件地址(“huydq@abc.com“,“邮件发送程序”);
MailAddress toAddress=新邮件地址(targetMail,shownTargetName);
String fromPassword=“mypassword”;
String subject=“测试重复”;
弦体=
@"
在这里,您可以输入正文中出现的任何文本
多行且均匀
";
SmtpClient smtp=新SmtpClient
{
Host=“RSC-MAIL2K7.abc.com”,
端口=25,
EnableSsl=true,
DeliveryMethod=SmtpDeliveryMethod.Network,
UseDefaultCredentials=false,
凭据=新网络凭据(fromAddress.Address,fromPassword)
};
使用(MailMessage message=新的MailMessage(fromAddress,toAddress)
{
主语,
身体
}
)
{
尝试
{
smtp.Send(message);
lbError.Text=“已发送电子邮件!”;
Outlook.Application olApp=新建Outlook.Application();
创建新的复发药膏(OLAP);
Marshal.ReleaseComObject(olApp);
}
抓住
{
lbError.Text=“发送失败,请检查您的internet连接!”;
}
}
}
public void CreateNewRecurringApp(Outlook.\u应用程序Outlook应用程序)
{
Outlook.AppointmentItem appItem=null;
Outlook.RecurrencePattern模式=空;
尝试
{
appItem=OutlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem)
作为Outlook.AppointmentItem;
//创建一个循环
pattern=appItem.GetRecurrencePattern();
pattern.RecurrenceType=Outlook.OlRecurrenceType.olrecurseweekly;
pattern.StartTime=DateTime.Parse(“上午8:35:00”);
pattern.EndTime=DateTime.Parse(“9:35:00pm”);
//我们可以指定持续时间,而不是使用EndTime属性
模式:持续时间=60;
pattern.PatternStartDate=DateTime.Parse(“07/23/2014”);
pattern.patterneddate=DateTime.Parse(“2014年7月31日”);
appItem.Subject=“与老板会面”;
appItem.Body=“测试预约主体”;
appItem.Location=“P1”;
appItem.rementerset=true;
appItem.ReminderMinutesBeforeStart=15;
appItem.Importance=Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh;
appItem.BusyStatus=Microsoft.Office.Interop.Outlook.OlBusyStatus.olBusy;
appItem.Save();
appItem.Send();
//appItem.Display(true);
}
捕获(例外情况除外)
{
lbRecur.Text=例如消息;
}
最后
{
if(模式!=null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(模式);
}
if(appItem!=null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(appItem);
}
}
}

这是一个重复出现的iCalendar(invite.ics)文件示例

请注意,
RRULE:FREQ=每周;直到=20150223T023000Z;间隔=1;BYDAY=MO;WKST=MO
定义重复周期。更多关于

因此,您需要根据需要使用定期规则准备一个类似的文件,并将其作为名为
invite.ics
的附件发送给收件人,收件人的电子邮件客户端(outlook、gmail等)将相应地创建日历条目


有关

的详细信息以及这里的问题是什么?您可以手动创建一个文件,并通过电子邮件将其作为名为invite.ics的附件发送,这应该可以完成以下操作trick@LasseV.Karlsen:我的问题是“我想在不使用Outlook的情况下发送约会循环”@pushpraj:因为我认为这可能是一个很好的解决方案。@PMay1903我使用了类似的方法将约会推送到我的gmail上。但是我没有设置重复周期,它只是简单的开始/结束日期和时间。谢谢您的示例。因为我试图将一个附件invite.ics发送给拥有Outlook的收件人的电子邮件客户端。但是,它显示了一个错误,因为
无法预览该文件,因为没有为其安装预览程序。
并且无法工作。这是由于mime类型不正确,请将以下标题设置为附件
内容类型:文本/日历;charset=“utf-8”;method=REQUEST
,应将其识别为日历请求