[英]How to send an appointment recurrence without using Outlook with C#
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>
C#:
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);
}
}
}
这是重复出现的iCalendar(invite.ics)文件的示例
请注意, RRULE:FREQ=WEEKLY;UNTIL=20150223T023000Z;INTERVAL=1;BYDAY=MO;WKST=MO
定义了重复周期。 有关RRULE的更多信息
BEGIN:VCALENDAR
METHOD:REQUEST
BEGIN:VTIMEZONE
TZID:Singapore Standard Time
BEGIN:STANDARD
DTSTART:16010101T000000
TZOFFSETFROM:+0800
TZOFFSETTO:+0800
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:16010101T000000
TZOFFSETFROM:+0800
TZOFFSETTO:+0800
END:DAYLIGHT
END:VTIMEZONE
BEGIN:VEVENT
ORGANIZER;CN= organizer's name :MAILTO: organizers email
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=recipient's name:MAILTO: email of recipient
DESCRIPTION: event description
RRULE:FREQ=WEEKLY;UNTIL=20150223T023000Z;INTERVAL=1;BYDAY=MO;WKST=MO
SUMMARY: event title
DTSTART;TZID=Singapore Standard Time:20141215T103000
DTEND;TZID=Singapore Standard Time:20141215T160000
UID: some unique id if you wish to update the event later by sending another invite
DTSTAMP:20140626T065810Z
LOCATION: location of your evennt
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:REMINDER
TRIGGER;RELATED=START:-PT15M
END:VALARM
END:VEVENT
END:VCALENDAR
因此,您需要根据需要准备一个具有重复规则的类似文件,并将其作为名为invite.ics
的附件发送给收件人,收件人的电子邮件客户端(outlook,gmail等)将相应地创建一个日历条目
有关iCalendar规范的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.