2014-07-23 40 views
0
  • 我在這裏所做的是與復發發送電子郵件和 這已被自動添加到接收器的Outlook日曆 這相當奏效。
  • 另一方面,我的服務器沒有Outlook所以我想 做沒有使用Outlook發送預約重複。

HTML:如何發送預約復發而不使用Outlook與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> 

C#:

protected void btSent_Click(object sender, EventArgs e) 
    { 
     SendMail("[email protected]", "ITD"); 
    } 

    public void SendMail(string targetMail, string shownTargetName) 
    { 
     MailAddress fromAddress = new MailAddress("[email protected]", "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); 
      } 
     } 
    } 
+0

,什麼是這裏的問題? –

+1

你可以手動創建一個[ICalendar](http://en.wikipedia.org/wiki/ICalendar)文件,並通過電子郵件發送名爲invite.ics的附件,該文件應該可以做到這一點 – pushpraj

+0

@ LasseV.Karlsen:My問題是「我希望在不使用Outlook的情況下發送預約重複」。 –

回答

0

這裏是重複的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 Specification

+0

謝謝你的樣品。正如我試圖發送一個附件,即invite.ics到收件人的Outlook的電子郵件客戶端。但是,它顯示錯誤,因爲「該文件無法預覽,因爲沒有安裝它的預覽器」,並且它不起作用。 –

+0

這是由於不正確的MIME類型,將以下標題設置爲附件'Content-Type:text/calendar;字符集= 「UTF-8」;方法= REQUEST',這應該將其識別爲日曆請求 – pushpraj