using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Text;
using System.IO;
public partial class Test3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string ics = new Appointment().CreateIcs("行事曆主旨", "行事曆地點",
new DateTime(2017, 8, 7, 23, 0, 0), new DateTime(2017, 8, 7, 23, 30, 0));
MemoryStream ms = new MemoryStream();
UTF8Encoding enc = new UTF8Encoding();
byte[] arrBytData = enc.GetBytes(ics);
ms.Write(arrBytData, 0, arrBytData.Length);
ms.Position = 0;
// Be sure to give the name a .ics extension here, otherwise it will not work.
Attachment attachment = new Attachment(ms, "Appointment.ics");
new Mailing().SendMail("123@hinet.net", "456@hinet.net",
"信件主旨", "<strong>信件內容</strong>", attachment);
}
}
public class Mailing
{
public void SendMail(string from, string to, string subject, string body, Attachment attachment)
{
using (MailMessage mailClient = new MailMessage(from, to, subject, body))
{
mailClient.To.Add("123@com.tw");
mailClient.Attachments.Add(attachment);
mailClient.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("msa.hinet.net");
smtp.Send(mailClient);
}
}
}
public class Appointment
{
private string GetFormatedDate(DateTime date)
{
return string.Format("{0:00}{1:00}{2:00}", date.Year, date.Month, date.Day);
}
private string GetFormattedTime(DateTime dateTime)
{
return string.Format("T{0:00}{1:00}{2:00}", dateTime.Hour, dateTime.Minute, dateTime.Second);
}
public string CreateIcs(string subject, string location, DateTime startDate, DateTime endDate)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("BEGIN:VCALENDAR");
sb.AppendLine("VERSION:2.0");
sb.AppendLine("PRODID:-//hacksw/handcal//NONSGML v1.0//EN");
sb.AppendLine("BEGIN:VEVENT");
string startDay = string.Format("VALUE=DATE:{0}{1}",
GetFormatedDate(startDate), GetFormattedTime(startDate));
string endDay = string.Format("VALUE=DATE:{0}{1}",
GetFormatedDate(endDate), GetFormattedTime(endDate));
sb.AppendLine("DTSTART;" + startDay);
sb.AppendLine("DTEND;" + endDay);
sb.AppendLine("DESCRIPTION:" + "日曆內容"); //add
sb.AppendLine("SUMMARY:" + subject);
sb.AppendLine("LOCATION:" + location);
sb.AppendLine("END:VEVENT");
sb.AppendLine("END:VCALENDAR");
return sb.ToString();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Text;
using System.IO;
public partial class Test3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string ics = new Appointment().CreateIcs("行事曆主旨", "行事曆地點",
new DateTime(2017, 8, 7, 23, 0, 0), new DateTime(2017, 8, 7, 23, 30, 0));
MemoryStream ms = new MemoryStream();
UTF8Encoding enc = new UTF8Encoding();
byte[] arrBytData = enc.GetBytes(ics);
ms.Write(arrBytData, 0, arrBytData.Length);
ms.Position = 0;
// Be sure to give the name a .ics extension here, otherwise it will not work.
Attachment attachment = new Attachment(ms, "Appointment.ics");
new Mailing().SendMail("123@hinet.net", "456@hinet.net",
"信件主旨", "<strong>信件內容</strong>", attachment);
}
}
public class Mailing
{
public void SendMail(string from, string to, string subject, string body, Attachment attachment)
{
using (MailMessage mailClient = new MailMessage(from, to, subject, body))
{
mailClient.To.Add("123@com.tw");
mailClient.Attachments.Add(attachment);
mailClient.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("msa.hinet.net");
smtp.Send(mailClient);
}
}
}
public class Appointment
{
private string GetFormatedDate(DateTime date)
{
return string.Format("{0:00}{1:00}{2:00}", date.Year, date.Month, date.Day);
}
private string GetFormattedTime(DateTime dateTime)
{
return string.Format("T{0:00}{1:00}{2:00}", dateTime.Hour, dateTime.Minute, dateTime.Second);
}
public string CreateIcs(string subject, string location, DateTime startDate, DateTime endDate)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("BEGIN:VCALENDAR");
sb.AppendLine("VERSION:2.0");
sb.AppendLine("PRODID:-//hacksw/handcal//NONSGML v1.0//EN");
sb.AppendLine("BEGIN:VEVENT");
string startDay = string.Format("VALUE=DATE:{0}{1}",
GetFormatedDate(startDate), GetFormattedTime(startDate));
string endDay = string.Format("VALUE=DATE:{0}{1}",
GetFormatedDate(endDate), GetFormattedTime(endDate));
sb.AppendLine("DTSTART;" + startDay);
sb.AppendLine("DTEND;" + endDay);
sb.AppendLine("DESCRIPTION:" + "日曆內容"); //add
sb.AppendLine("SUMMARY:" + subject);
sb.AppendLine("LOCATION:" + location);
sb.AppendLine("END:VEVENT");
sb.AppendLine("END:VCALENDAR");
return sb.ToString();
}
}