string zipDirPath;
string zipDirPath2;
string outputFilePath;
string outputFilePath2;
DataSet ds = new DataSet();
DataTable dt = new DataTable();
try
{
dt = ds.Tables[0];
//this.GridView1.DataSource = dt;
//this.GridView1.DataBind();
//string path = Server.MapPath("TempMailFiles\\") + DateTime.Now.ToString("yyyyMMddHHmmss") + ".csv" ;
string path = Server.MapPath("TempMailFiles\\") + DateTime.Now.AddMonths(-1).ToString("yyyy") + "年" + DateTime.Now.AddMonths(-1).ToString("MM") + "月" + "test.csv";
FileStream fs = new FileStream(path,FileMode.OpenOrCreate,FileAccess.ReadWrite);
//FileStream fs = new FileStream(@"D:\BenYang\MIS\APPortalWeb\SC\TempMailFiles\1.txt" ,FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs,Encoding.GetEncoding("big5"));
sw.WriteLine("A,B,C,D");
foreach(DataRow dw in dt.Rows)
{
sw.WriteLine(Convert.ToString(dw["A"]) + "," + Convert.ToString(dw["B"]) + "," + Convert.ToString(dw["C"]) + "," + Convert.ToString(dw["D"]));
}
sw.Close();
fs.Close();
MailMessage mail = new MailMessage();
mail.From = new MailAddress(test@test.com.tw);
mail.To.Add("test@test.com.tw");
//mail.To.Add("test@test.com.tw");
//mail.CC.Add("test@test.com.tw");
//set the content
mail.SubjectEncoding = Encoding.GetEncoding("big5");
mail.Subject = "請協助提供" + DateTime.Now.AddMonths(-1).ToString("yyyy") + "年" + DateTime.Now.AddMonths(-1).ToString("MM") + "月" + "test";
mail.BodyEncoding = Encoding.GetEncoding("big5");
mail.Body = "Dear\n\r";
mail.Body += "檢附" + DateTime.Now.AddMonths(-1).ToString("yyyy") + "年" + DateTime.Now.AddMonths(-1).ToString("MM") + "月" + "test";
mail.Body += "";
//Attachment data = new Attachment(path, MediaTypeNames.Application.Octet);
//todo zip the attachement file
try
{
zipDirPath = Server.MapPath("TempMailFiles\\");
zipDirPath2 = Server.MapPath("TempMailFiles2\\");
outputFilePath = DateTime.Now.AddMonths(-1).ToString("yyyy") + "年" + DateTime.Now.AddMonths(-1).ToString("MM") + "月" + "test.csv";
outputFilePath2 = DateTime.Now.AddMonths(-1).ToString("yyyy") + "年" + DateTime.Now.AddMonths(-1).ToString("MM") + "月" + "test.zip";
ZipFile(zipDirPath + outputFilePath, zipDirPath2 + outputFilePath2, 6, 4096);
}
catch (Exception ex)
{
throw ex;
}
string mailAttachementFilePath = zipDirPath2 + outputFilePath2;
Attachment data = new Attachment(mailAttachementFilePath, MediaTypeNames.Application.Octet); //todo change path
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(path);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(path);
disposition.ReadDate = System.IO.File.GetLastAccessTime(path);
mail.Attachments.Add(data);
SmtpClient client = new SmtpClient("mail.xxx.com.tw",25);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(mail); //todo test
data.Dispose();
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "sendmail", "alert('Mail寄送完畢');", true);
}
catch (Exception ex)
{
throw ex;
}
finally
{
cn.Close();
if (File.Exists(zipDirPath + outputFilePath))
{
File.Delete(zipDirPath + outputFilePath);
}
if (File.Exists(zipDirPath2 + outputFilePath2))
{
File.Delete(zipDirPath2 + outputFilePath2);
}
}
public void ZipFile(string FileToZip, string ZipedFile ,int CompressionLevel, int BlockSize)
{
if (! System.IO.File.Exists(FileToZip))
{
throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd");
}
System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip,System.IO.FileMode.Open , System.IO.FileAccess.Read);
System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
ZipStream.Password = "7777";
ZipEntry ZipEntry = new ZipEntry(FileToZip.Substring(FileToZip.LastIndexOf("\\")+1));
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(CompressionLevel);
byte[] buffer = new byte[BlockSize];
System.Int32 size =StreamToZip.Read(buffer,0,buffer.Length);
ZipStream.Write(buffer,0,size);
try
{
while (size < StreamToZip.Length)
{
int sizeRead =StreamToZip.Read(buffer,0,buffer.Length);
ZipStream.Write(buffer,0,sizeRead);
size += sizeRead;
}
}
catch(System.Exception ex)
{
throw ex;
}
ZipStream.Finish();
ZipStream.Close();
StreamToZip.Close();
}
string zipDirPath2;
string outputFilePath;
string outputFilePath2;
DataSet ds = new DataSet();
DataTable dt = new DataTable();
try
{
dt = ds.Tables[0];
//this.GridView1.DataSource = dt;
//this.GridView1.DataBind();
//string path = Server.MapPath("TempMailFiles\\") + DateTime.Now.ToString("yyyyMMddHHmmss") + ".csv" ;
string path = Server.MapPath("TempMailFiles\\") + DateTime.Now.AddMonths(-1).ToString("yyyy") + "年" + DateTime.Now.AddMonths(-1).ToString("MM") + "月" + "test.csv";
FileStream fs = new FileStream(path,FileMode.OpenOrCreate,FileAccess.ReadWrite);
//FileStream fs = new FileStream(@"D:\BenYang\MIS\APPortalWeb\SC\TempMailFiles\1.txt" ,FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs,Encoding.GetEncoding("big5"));
sw.WriteLine("A,B,C,D");
foreach(DataRow dw in dt.Rows)
{
sw.WriteLine(Convert.ToString(dw["A"]) + "," + Convert.ToString(dw["B"]) + "," + Convert.ToString(dw["C"]) + "," + Convert.ToString(dw["D"]));
}
sw.Close();
fs.Close();
MailMessage mail = new MailMessage();
mail.From = new MailAddress(test@test.com.tw);
mail.To.Add("test@test.com.tw");
//mail.To.Add("test@test.com.tw");
//mail.CC.Add("test@test.com.tw");
//set the content
mail.SubjectEncoding = Encoding.GetEncoding("big5");
mail.Subject = "請協助提供" + DateTime.Now.AddMonths(-1).ToString("yyyy") + "年" + DateTime.Now.AddMonths(-1).ToString("MM") + "月" + "test";
mail.BodyEncoding = Encoding.GetEncoding("big5");
mail.Body = "Dear\n\r";
mail.Body += "檢附" + DateTime.Now.AddMonths(-1).ToString("yyyy") + "年" + DateTime.Now.AddMonths(-1).ToString("MM") + "月" + "test";
mail.Body += "";
//Attachment data = new Attachment(path, MediaTypeNames.Application.Octet);
//todo zip the attachement file
try
{
zipDirPath = Server.MapPath("TempMailFiles\\");
zipDirPath2 = Server.MapPath("TempMailFiles2\\");
outputFilePath = DateTime.Now.AddMonths(-1).ToString("yyyy") + "年" + DateTime.Now.AddMonths(-1).ToString("MM") + "月" + "test.csv";
outputFilePath2 = DateTime.Now.AddMonths(-1).ToString("yyyy") + "年" + DateTime.Now.AddMonths(-1).ToString("MM") + "月" + "test.zip";
ZipFile(zipDirPath + outputFilePath, zipDirPath2 + outputFilePath2, 6, 4096);
}
catch (Exception ex)
{
throw ex;
}
string mailAttachementFilePath = zipDirPath2 + outputFilePath2;
Attachment data = new Attachment(mailAttachementFilePath, MediaTypeNames.Application.Octet); //todo change path
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(path);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(path);
disposition.ReadDate = System.IO.File.GetLastAccessTime(path);
mail.Attachments.Add(data);
SmtpClient client = new SmtpClient("mail.xxx.com.tw",25);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(mail); //todo test
data.Dispose();
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "sendmail", "alert('Mail寄送完畢');", true);
}
catch (Exception ex)
{
throw ex;
}
finally
{
cn.Close();
if (File.Exists(zipDirPath + outputFilePath))
{
File.Delete(zipDirPath + outputFilePath);
}
if (File.Exists(zipDirPath2 + outputFilePath2))
{
File.Delete(zipDirPath2 + outputFilePath2);
}
}
public void ZipFile(string FileToZip, string ZipedFile ,int CompressionLevel, int BlockSize)
{
if (! System.IO.File.Exists(FileToZip))
{
throw new System.IO.FileNotFoundException("The specified file " + FileToZip + " could not be found. Zipping aborderd");
}
System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip,System.IO.FileMode.Open , System.IO.FileAccess.Read);
System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);
ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
ZipStream.Password = "7777";
ZipEntry ZipEntry = new ZipEntry(FileToZip.Substring(FileToZip.LastIndexOf("\\")+1));
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(CompressionLevel);
byte[] buffer = new byte[BlockSize];
System.Int32 size =StreamToZip.Read(buffer,0,buffer.Length);
ZipStream.Write(buffer,0,size);
try
{
while (size < StreamToZip.Length)
{
int sizeRead =StreamToZip.Read(buffer,0,buffer.Length);
ZipStream.Write(buffer,0,sizeRead);
size += sizeRead;
}
}
catch(System.Exception ex)
{
throw ex;
}
ZipStream.Finish();
ZipStream.Close();
StreamToZip.Close();
}