安排任务或召开重大会议时的一种通知方式,因为它有提醒功能。
(新建约会邮件)
(录入信息,注意有一个提醒设置)
简单介绍完了,下面就来看看如何用代码实现,废话不多说直接上代码,因为也不是我写的。(当时在项目中为了实现这个功能可花了N长时间)
Code
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->public struct AppointMail
{
public string from; //发件人邮箱
public string to; //收件人邮箱;号分隔的一个串
public string fromDisplayName; //发件人姓名
public string subject; //标题
public string body; //正文
public string location; //地点
public DateTime sTime; //开始时间
public DateTime eTime; //结束时间
}
Code
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->public static void SendAppointMail(AppointMail mail)
{
MailMessage msg = new MailMessage();
MailAddressCollection attendeeList = new MailAddressCollection();
string[] sendTo = mail.to.Split(';');
foreach (string temp in sendTo)
{
msg.Bcc.Add(temp);
attendeeList.Add(temp);
}
// Set up the different mime types contained in the message
System.Net.Mime.ContentType textType = new System.Net.Mime.ContentType("text/plain");
System.Net.Mime.ContentType HTMLType = new System.Net.Mime.ContentType("text/html");
System.Net.Mime.ContentType RTFType = new System.Net.Mime.ContentType("text/rtf");
System.Net.Mime.ContentType calendarType = new System.Net.Mime.ContentType("text/calendar;");
//Add parameters to the calendar header
calendarType.Parameters.Add("method", "REQUEST");
calendarType.Parameters.Add("name", "meeting.ics");
//calendarType.Parameters.Add("charset", "utf-8");
// Create message body parts
/*
// create the Body in text format
string bodyText = "Type:Single Meeting\r\nOrganizer: {0}\r\nStart Time:{1}\r\nEnd Time:{2}\r\nTime Zone:{3}\r\nLocation: {4}\r\n\r\n*~*~*~*~*~*~*~*~*~*\r\n\r\n{5}";
bodyText = string.Format(bodyText,
mail.fromDisplayName,
mail.sTime.ToLongDateString() + " " + mail.sTime.ToLongTimeString(),
mail.eTime.ToLongDateString() + " " + mail.eTime.ToLongTimeString(),
System.TimeZone.CurrentTimeZone.StandardName,
mail.location,
mail.subject);
AlternateView textView = AlternateView.CreateAlternateViewFromString(bodyText, textType);
msg.AlternateViews.Add(textView);*/
//create the Body in HTML format
/*
string bodyHTML = "";
bodyHTML = string.Format(bodyHTML,
mail.subject,
mail.fromDisplayName,
mail.sTime.ToLongDateString() + " " + mail.sTime.ToLongTimeString(),
mail.eTime.ToLongDateString() + " " + mail.eTime.ToLongTimeString(),
System.TimeZone.CurrentTimeZone.StandardName,
mail.location,
mail.subject);
AlternateView HTMLView = AlternateView.CreateAlternateViewFromString(bodyHTML, HTMLType);
msg.AlternateViews.Add(HTMLView);
*/
//create the Body in VCALENDAR format
string calDateFormat = "yyyyMMddTHHmmssZ";
string bodyCalendar = "BEGIN:VCALENDAR\r\nMETHOD:REQUEST\r\nPRODID:Microsoft CDO for Microsoft Exchange\r\nVERSION:2.0\r\nBEGIN:VTIMEZONE\r\nTZID:(GMT-06.00) Central Time (US & Canada)\r\nX-MICROSOFT-CDO-TZID:11\r\nBEGIN:STANDARD\r\nDTSTART:16010101T020000\r\nTZOFFSETFROM:-0500\r\nTZOFFSETTO:-0600\r\nRRULE:FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=11;BYDAY=1SU\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nDTSTART:16010101T020000\r\nTZOFFSETFROM:-0600\r\nTZOFFSETTO:-0500\r\nRRULE:FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=3;BYDAY=2SU\r\nEND:DAYLIGHT\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nDTSTAMP:{8}\r\nDTSTART:{0}\r\nSUMMARY:{6}\r\nUID:{5}\r\nATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=\"{9}\":MAILTO:{9}\r\nACTION;RSVP=TRUE;CN=\"{4}\":MAILTO:{4}\r\nORGANIZER;CN=\"{3}\":mailto:{4}\r\nLOCATION:{2}\r\nDTEND:{1}\r\nDESCRIPTION:{7}\\N\r\nSEQUENCE:1\r\nPRIORITY:5\r\nCLASS:\r\nCREATED:{8}\r\nLAST-MODIFIED:{8}\r\nSTATUS:CONFIRMED\r\nTRANSP:OPAQUE\r\nX-MICROSOFT-CDO-BUSYSTATUS:BUSY\r\nX-MICROSOFT-CDO-INSTTYPE:0\r\nX-MICROSOFT-CDO-INTENDEDSTATUS:BUSY\r\nX-MICROSOFT-CDO-ALLDAYEVENT:FALSE\r\nX-MICROSOFT-CDO-IMPORTANCE:1\r\nX-MICROSOFT-CDO-OWNERAPPTID:-1\r\nX-MICROSOFT-CDO-ATTENDEE-CRITICAL-CHANGE:{8}\r\nX-MICROSOFT-CDO-OWNER-CRITICAL-CHANGE:{8}\r\nBEGIN:VALARM\r\nACTION:DISPLAY\r\nDESCRIPTION:REMINDER\r\nTRIGGER;RELATED=START:-PT00H15M00S\r\nEND:VALARM\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n";
bodyCalendar = string.Format(bodyCalendar,
mail.sTime.ToUniversalTime().ToString(calDateFormat),
mail.eTime.ToUniversalTime().ToString(calDateFormat),
mail.location,
mail.fromDisplayName,
mail.from,
Guid.NewGuid().ToString("B"),
mail.subject,//summary
mail.body,//正文
DateTime.Now.ToUniversalTime().ToString(calDateFormat),
attendeeList.ToString(),
mail.body
);
AlternateView calendarView = AlternateView.CreateAlternateViewFromString(bodyCalendar, calendarType);
calendarView.TransferEncoding = TransferEncoding.SevenBit;
msg.AlternateViews.Add(calendarView);
// Adress the message
msg.From = new MailAddress(mail.from, mail.fromDisplayName);
msg.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient(ConfigurationManager.AppSettings["mail"]);
smtpClient.Send(msg);
}
这个功能有一个小问题,就是无法在邮件正文中设置html样式,虽然设置了IsBodyHtml = true;但可惜还是不行,希望哪位高人能够解决一下~
额外附送我的邮件发送类 (普通的邮件)
Code
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
namespace Project.Common
{
/// <summary>
/// 静态类 提供发送邮件的方法
/// </summary>
public static class Mail
{
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="mail">MailEntity实体</param>
/// <param name="host">邮件服务器</param>
public static void sendMail(MailEntity mail,string host)
{
MailAddress fromAddress = new MailAddress(mail.From, mail.FromDisplayName);
MailMessage msg = new MailMessage();
msg.From = fromAddress;
string[] sendTo = mail.To.Split(';');
foreach (string temp in sendTo)
{
if (temp != string.Empty)
{
msg.Bcc.Add(temp);
}
}
msg.IsBodyHtml = true;
msg.Subject = mail.Subject;
msg.Body = mail.Body;
SmtpClient smtpClient = new SmtpClient(host);
smtpClient.Send(msg);
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="mail">MailEntity实体</param>
/// <param name="host">邮件服务器</param>
/// <param name="port">端口</param>
public static void sendMail(MailEntity mail, string host,int port)
{
MailAddress fromAddress = new MailAddress(mail.From, mail.FromDisplayName);
MailMessage msg = new MailMessage();
msg.From = fromAddress;
string[] sendTo = mail.To.Split(';');
foreach (string temp in sendTo)
{
if (temp != string.Empty)
{
msg.Bcc.Add(temp);
}
}
msg.IsBodyHtml = true;
msg.Subject = mail.Subject;
msg.Body = mail.Body;
SmtpClient smtpClient = new SmtpClient(host,port);
smtpClient.Send(msg);
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="mail">MailEntity实体</param>
/// <param name="host">邮件服务器</param>
/// <param name="port">端口</param>
/// <param name="adminName">权限验证所需的用户名</param>
/// <param name="adminPassword">权限验证所需的密码</param>
public static void sendMail(MailEntity mail, string host, int port, string adminName, string adminPassword)
{
MailAddress fromAddress = new MailAddress(mail.From, mail.FromDisplayName);
MailMessage msg = new MailMessage();
msg.From = fromAddress;
string[] sendTo = mail.To.Split(';');
foreach (string temp in sendTo)
{
if (temp != string.Empty)
{
msg.Bcc.Add(temp);
}
}
msg.IsBodyHtml = true;
msg.Subject = mail.Subject;
msg.Body = mail.Body;
SmtpClient smtpClient = new SmtpClient(host, port);
smtpClient.Credentials = new System.Net.NetworkCredential(adminName, adminPassword);
smtpClient.Send(msg);
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="mail">MailEntity实体</param>
/// <param name="host">邮件服务器</param>
/// <param name="port">端口</param>
/// <param name="adminName">权限验证所需的用户名</param>
/// <param name="adminPassword">权限验证所需的密码</param>
/// <param name="domain">域</param>
public static void sendMail(MailEntity mail, string host, int port,string adminName,string adminPassword,string domain)
{
MailAddress fromAddress = new MailAddress(mail.From, mail.FromDisplayName);
MailMessage msg = new MailMessage();
msg.From = fromAddress;
string[] sendTo = mail.To.Split(';');
foreach (string temp in sendTo)
{
if (temp != string.Empty)
{
msg.Bcc.Add(temp);
}
}
msg.IsBodyHtml = true;
msg.Subject = mail.Subject;
msg.Body = mail.Body;
SmtpClient smtpClient = new SmtpClient(host, port);
smtpClient.Credentials = new System.Net.NetworkCredential(adminName,adminPassword,domain);
smtpClient.Send(msg);
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="mail">MailEntity实体</param>
/// <param name="host">邮件服务器</param>
/// <param name="adminName">权限验证所需的用户名</param>
/// <param name="adminPassword">权限验证所需的密码</param>
public static void sendMail(MailEntity mail, string host, string adminName, string adminPassword)
{
MailAddress fromAddress = new MailAddress(mail.From, mail.FromDisplayName);
MailMessage msg = new MailMessage();
msg.From = fromAddress;
string[] sendTo = mail.To.Split(';');
foreach (string temp in sendTo)
{
if (temp != string.Empty)
{
msg.Bcc.Add(temp);
}
}
msg.IsBodyHtml = true;
msg.Subject = mail.Subject;
msg.Body = mail.Body;
SmtpClient smtpClient = new SmtpClient(host);
smtpClient.Credentials = new System.Net.NetworkCredential(adminName, adminPassword);
smtpClient.Send(msg);
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="mail">MailEntity实体</param>
/// <param name="host">邮件服务器</param>
/// <param name="adminName">权限验证所需的用户名</param>
/// <param name="adminPassword">权限验证所需的密码</param>
/// <param name="domain">域</param>
public static void sendMail(MailEntity mail, string host, string adminName, string adminPassword, string domain)
{
MailAddress fromAddress = new MailAddress(mail.From, mail.FromDisplayName);
MailMessage msg = new MailMessage();
msg.From = fromAddress;
string[] sendTo = mail.To.Split(';');
foreach (string temp in sendTo)
{
if (temp != string.Empty)
{
msg.Bcc.Add(temp);
}
}
msg.IsBodyHtml = true;
msg.Subject = mail.Subject;
msg.Body = mail.Body;
SmtpClient smtpClient = new SmtpClient(host);
smtpClient.Credentials = new System.Net.NetworkCredential(adminName, adminPassword, domain);
smtpClient.Send(msg);
}
}
/// <summary>
/// 邮件实体
/// </summary>
public struct MailEntity
{
private string from;
/// <summary>
/// 发送者邮箱
/// </summary>
public string From
{
get { return from; }
set { from = value; }
}
private string to;
/// <summary>
/// 收件者集合以;分号分隔
/// </summary>
public string To
{
get { return to; }
set { to = value; }
}
private string fromDisplayName;
/// <summary>
/// 发送者姓名
/// </summary>
public string FromDisplayName
{
get { return fromDisplayName; }
set { fromDisplayName = value; }
}
private string subject;
/// <summary>
/// 邮件标题
/// </summary>
public string Subject
{
get { return subject; }
set { subject = value; }
}
private string body;
/// <summary>
/// 邮件正文
/// </summary>
public string Body
{
get { return body; }
set { body = value; }
}
}
}
另外再PS一点VS的小功能,真的是非常小,所以不想再写一篇了。
在VS开发时,我们可以看到一个叫ASP的命名空间,看下面:
这个命名空间只有在页面的CS文件中才能看到,下面放着在该页面上注册过的usercontrol(不支持customcontrol)、页面使用的masterpage以及Global.asax文件。
看到了吗,就算给usercontrol指定了命名空间还是可以在ASP下看到。
over!
每个人都会经过这个阶段,见到一座山,就想知道山后面是什么。我很想告诉他,可能翻过山后面,你会发现没什么特别。回望之下,可能会觉得这一边更好。 每个人都会坚持自己的信念,在别人看来,是浪费时间,她却觉得很重要。