The emails sent under. net are accompanied by calendar and. netcalendar.

Source: Internet
Author: User
Tags mailmessage smtpclient

The emails sent under. net are accompanied by calendar and. netcalendar.

Some time ago, one of the company's systems needed to send an email calendar.

Outlook can view the calendar

Sending outlook mainly involves the boundaries of rrule scripts

 

I have found some materials online, mainly in two ways.

1. One is to send attachments in ics attachment format, but the calendar

You need to open the attachment to view the calendar. the user experience is not very good.

2. edit a rrule. net. mime. contentType contype = new System. net. mime. contentType ("text/calendar");) Format

The user can receive the calendar

The second method is adopted in my project.

Ruule replacement code


// Replace the generated calenar string (the RRule format can be generated using the DDay. iCal plug-in, which is currently manually spliced)
Public class CalendarFormat
{
Public string GetCalendar (CalendarContent cont)
{
# Region RRule String concatenation Calendar
String bodyCalendar = @ "BEGIN: VCALENDAR
PRODID: Microsoft CDO for Microsoft Exchange
VERSION: 2.0
METHOD: REQUEST
X-MS-OLK-FORCEINSPECTOROPEN: TRUE
BEGIN: VTIMEZONE
TZID: China Standard Time
BEGIN: STANDARD
DTSTART: 16010101T000000
TZOFFSETFROM: + 0800
TZOFFSETTO: + 0800
END: STANDARD
END: VTIMEZONE
BEGIN: VEVENT
ATTENDEE; CN = {10}; RSVP = TRUE: {9}
CLASS: PUBLIC
CREATED: {8}
DESCRIPTION: {6}
DTEND; TZID = 'China Standard time': {1}
DTSTAMP: {8}
DTSTART; TZID = 'China Standard time': {0}
LAST-MODIFIED: {8}
LOCATION: {2}
ORGANIZER; CN = {3}: mailto: {4}
PRIORITY: 5
SEQUENCE: 0
SUMMARY; LANGUAGE = zh-cn: {7}
TRANSP: OPAQUE
UID: {5}
X-ALT-DESC; FMTTYPE = text/html: <! Doctype html public '-// W3C // dtd html 3.2 // E
N'> \ N <HTML> \ n <HEAD> \ n <meta name = 'generator' CONTENT = 'Ms Exchange Server ve
Rsion 14.02.5004.000 '> \ n <TITLE> </TITLE> \ n </HEAD> \ n <BODY> \ n <! -- Converted f
Rom text/rtf format --> \ n <p dir = ltr align = JUSTIFY> <span lang = 'en-us'> <FO
Nt face = ' '>{6} </FONT> </SPAN> <span lang = 'en-us'> </SPAN> </P> </BODY> </HTML>
X-MICROSOFT-CDO-BUSYSTATUS: BUSY
X-MICROSOFT-CDO-IMPORTANCE: 1
X-MICROSOFT-DISALLOW-COUNTER: FALSE
X-MS-OLK-APPTSEQTIME: {8}
X-MS-OLK-AUTOFILLLOCATION: FALSE
X-MS-OLK-CONFTYPE: 0
BEGIN: VALARM
TRIGGER:-PT15M
ACTION: DISPLAY
DESCRIPTION: Reminder
END: VALARM
END: VEVENT
END: VCALENDAR ";
# Endregion

BodyCalendar = string. Format (bodyCalendar,
Cont. StartTime. ToString ("yyyyMMddTHHmmss "),
Cont. EndTime. ToString ("yyyyMMddTHHmmss "),
Cont. location + ""! = ""? Cont. location :"",
Cont. username + ""! = ""? Cont. username :"",
Cont. email + ""! = ""? Cont. email :"",
Guid. NewGuid (). ToString ("B "),
Cont. body + ""! = ""? Cont. body :"",
Cont. subject + ""! = ""? Cont. subject :"",
DateTime. Now. ToString ("yyyyMMddTHHmmssZ "),
Cont. attendeeList! = Null? String. Join (",", cont. attendeeList ):"",
"Shima"
);

Return bodyCalendar;
}
}

 

The code for sending an email is as follows:

Run
Public void MySendMail (List <MailAttach> att)
{
SendNeed need = new SendNeed ();
Need. MailServer = "smtp.qq.com ";
Need. UserName = "XXX@qq.com ";
Need. PasssWord = "XXX ";
Need. Integer = 10000;
CalendarContent cont = new CalendarContent ();
Cont. body = "nihao ";
Cont. StartTime = DateTime. Now;
Cont. EndTime = DateTime. Now. AddHours (2 );
Cont. subject = "meeting ";
CalendarFormat calformat = new CalendarFormat ();
String str = calformat. GetCalendar (cont );
String senderAddress = "XXX@qq.com ";
String ToAddress = "XXX@qq.com ";
String CCAddress = "";
String mailSubject = "HELLO ";
String mailBody = "you have a meeting ";
EmailCalendarHelper emial = new EmailCalendarHelper ();
Emial. SendMessage (senderAddress, ToAddress, CCAddress, mailSubject, mailBody, att, str, need );

}

// Send an email
Public class SendNeed
{
Public string MailServer;
Public string UserName;
Public string PasssWord;
Public int interval;
}

Public class MailAttach
{
Public string mailAttachmentName;
Public byte [] mailAttachment;

Public MailAttach (string mailAttachmentName, byte [] mailAttachment)
{
This. mailAttachmentName = mailAttachmentName;
This. mailAttachment = mailAttachment;
}
}

Public class EmailCalendarHelper
{
Public bool SendMessage (string senderAddress, string ToAddress, string CCAddress, string mailSubject,
String mailBody, List <MailAttach> mailatt, string CalendarFromat,
SendNeed account)
{
// Mail sending class
System. Net. Mail. MailMessage mail = new System. Net. Mail. MailMessage ();
// Who sent the email
Mail. From = new System. Net. Mail. MailAddress (senderAddress );
// Send
Int I;
String [] arrTo = ToAddress. Split (new char [] {';'});
Int iLength = arrTo. Length;
For (I = 0; I <iLength; I ++)
{
Mail. To. Add (arrTo [I]);
}

// CC
If (CCAddress. Trim (). Length> 0)
{
String [] arrCC = CCAddress. Split (new char [] {';'});
ILength = arrCC. Length;
For (I = 0; I <iLength; I ++)
{
If (arrCC [I]. Length> 0)
Mail. CC. Add (arrCC [I]);
}
}
// Title
Mail. Subject = mailSubject;
// Content
Mail. Body = mailBody;
// Attachment
If (mailatt! = Null)
{
Foreach (var ma in mailatt)
{
If (ma. mailAttachment! = Null & ma. mailAttachment. Length> 0)
{
MemoryStream attStream = new MemoryStream ();
AttStream. Write (ma. mailAttachment, 0, ma. mailAttachment. Length );
AttStream. Seek (0, SeekOrigin. Begin );
Mail. Attachments. Add (new System. Net. Mail. Attachment (attStream, ma. mailAttachmentName, System. Net. Mime. MediaTypeNames. Application. Octet ));
}
}
}
// Content Encoding
Mail. BodyEncoding = Encoding. Default;
// Sending priority
Mail. Priority = System. Net. Mail. MailPriority. High;
// Calendar string
String bodyCalendar = CalendarFromat;
// Whether to send messages in HTML Format
Mail. IsBodyHtml = true;
// Email server and Port
System. Net. Mail. SmtpClient smtp = new System. Net. Mail. SmtpClient (account. MailServer, 25 );
Smtp. EnableSsl = true;
Smtp. usedefacrecredentials = true;
// Specify the sending Method
Smtp. DeliveryMethod = System. Net. Mail. SmtpDeliveryMethod. Network;
// Specify the logon name and password
Smtp. Credentials = new System. Net. NetworkCredential (account. UserName, account. PasssWord );

// Whether the message can be sent or not depends on it.
{
System. Net. Mime. ContentType contype = new System. Net. Mime. ContentType ("text/calendar ");
Contype. Parameters. Add ("method", "REQUEST ");
Contype. Parameters. Add ("charset", "UTF-8 ");
System. Net. Mail. AlternateView avCal =
System. Net. Mail. AlternateView. CreateAlternateViewFromString (bodyCalendar, contype );
Mail. AlternateViews. Add (avCal );
}
// Timeout
Smtp. Timeout = account. interval;
Smtp. Send (mail );
Mail. Dispose ();
Smtp. Dispose ();
Return true;
}
}

The second method has one drawback. As the project needs to send the calendar, the colleague needs to send the table

However, calendar sends emails in the format of "text/calendar", so the table format cannot be displayed.

If outlook performs the same operation, no format is displayed.

Later, I was forced to separate the calendar and table emails. If you have any good implementation solutions, please kindly advise.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.