.net 下发送calendar

2018-06-22 07:55:56来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

前段时间公司系统中有一块需要发送邮件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 = "XXX@qq.com";
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 = "XXX@qq.com";
string ToAddress = "XXX@qq.com";
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分两封邮件发出,如果有小伙伴有什么好的实施方案,欢迎指教。

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:C# WebApi+Task+WebSocket实战项目演练(四)

下一篇:富文本编辑器 CKeditor 配置使用+上传图片