前段時間公司系統中有一塊需要發送郵件calendar outlook可以接受查看calendar 發送outlook主要是有rrule腳本的邊界 網上找過一些資料,主要有兩種實現方式 1、一種是已ics的附件格式發送附件,但是這樣用戶接收到的calendar 需要點開附件才能查看calendar,用 ...
前段時間公司系統中有一塊需要發送郵件calendar
outlook可以接受查看calendar
發送outlook主要是有rrule腳本的邊界
網上找過一些資料,主要有兩種實現方式
1、一種是已ics的附件格式發送附件,但是這樣用戶接收到的calendar
需要點開附件才能查看calendar,用戶體驗不是特別好。
2、編輯一段rrule,在郵件發送的時候以( System.Net.Mime.ContentType contype = new System.Net.Mime.ContentType("text/calendar");)格式發送
用戶即可收到calendar
我項目中採用了第二種方式
ruule 替換代碼
//替換 生成calenar 字元串(RRule格式 可使用DDay.iCal插件 生成 ,目前為手動拼接)
public class CalendarFormat
{
public string GetCalendar(CalendarContent cont)
{
#region RRule字元串 拼接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\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;
}
}
下麵是郵件發送時的代碼
執行
public void MySendMail(List<MailAttach> att)
{
SendNeed need=new SendNeed();
need.MailServer = "smtp.qq.com";
need.UserName = "[email protected]";
need.PasssWord = "XXX";
need.interval = 10000;
CalendarContent cont=new CalendarContent();
cont.body = "nihao";
cont.StartTime = DateTime.Now;
cont.EndTime = DateTime.Now.AddHours(2);
cont.subject = "開會";
CalendarFormat calformat=new CalendarFormat();
string str = calformat.GetCalendar(cont);
string senderAddress = "[email protected]";
string ToAddress = "[email protected]";
string CCAddress = "";
string mailSubject = "HELLO";
string mailBody = "開會 你還";
EmailCalendarHelper emial=new EmailCalendarHelper();
emial.SendMessage(senderAddress, ToAddress, CCAddress, mailSubject, mailBody, att, str, need);
}
//發送郵件
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)
{
//郵件發送類
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
//是誰發送的郵件
mail.From = new System.Net.Mail.MailAddress(senderAddress);
//發送給誰
int i;
string[] arrTo = ToAddress.Split(new char[] { ';' });
int iLength = arrTo.Length;
for (i = 0; i < iLength; i++)
{
mail.To.Add(arrTo[i]);
}
//抄送
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]);
}
}
//標題
mail.Subject = mailSubject;
//內容
mail.Body = mailBody;
//附件
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));
}
}
}
//內容編碼
mail.BodyEncoding = Encoding.Default;
//發送優先順序
mail.Priority = System.Net.Mail.MailPriority.High;
//calendar字元串
string bodyCalendar = CalendarFromat;
//是否HTML形式發送
mail.IsBodyHtml = true;
//郵件伺服器和埠
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(account.MailServer, 25);
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = true;
//指定發送方式
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
//指定登錄名和密碼
smtp.Credentials = new System.Net.NetworkCredential(account.UserName, account.PasssWord);
//能不能發送看到效果全靠它了
{
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);
}
//超時時間
smtp.Timeout = account.interval;
smtp.Send(mail);
mail.Dispose();
smtp.Dispose();
return true;
}
}
第二種方式有一種弊端,因為項目需求,發送calendar的時候,要同事發送table
但是calendar是以 以"text/calendar"格式發送郵件,所以table 格式無法呈現,測試直接用
outlook進行相同的操作也是會出現無格式。
後來被迫把calendar和table分兩封郵件發出,如果有小伙伴有什麼好的實施方案,歡迎指教。