Created
January 8, 2015 10:56
-
-
Save mvark/8c523eb47670c2fc8da4 to your computer and use it in GitHub Desktop.
C# code sample to send an email with a Word or Excel file attachment built on the fly. More info - http://mvark.blogspot.in/2007/01/how-to-send-email-with-word-or-excel.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Page Language="C#" %> | |
<script runat="server"> | |
void Page_Load(Object Src, EventArgs E) | |
{ | |
try | |
{ | |
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(); | |
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("[email protected]", "p@ssword"); | |
mail.To.Add("[email protected]"); | |
mail.Subject = "Invoice"; | |
mail.From = new System.Net.Mail.MailAddress("[email protected]"); | |
mail.IsBodyHtml = true; | |
mail.Body = "Invoice is attached"; | |
//Get some binary data | |
byte[] data = GetData(); | |
//save the data to a memory stream | |
System.IO.MemoryStream ms = new System.IO.MemoryStream(data); | |
//create the attachment from a stream. Be sure to name the data with a file and | |
//media type that is respective of the data | |
mail.Attachments.Add(new System.Net.Mail.Attachment (ms, "example.xls", "application/vnd.ms-excel")); | |
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.acme.com"); | |
smtp.UseDefaultCredentials = false; | |
//smtp.EnableSsl = true; | |
smtp.Credentials = cred; | |
smtp.Port = 587; | |
smtp.Send(mail); | |
Response.Write("Done!"); | |
} | |
catch(Exception ex) | |
{ | |
Response.Write(ex.ToString()); | |
} | |
} | |
static byte[] GetData() | |
{ | |
//this method just returns some binary data. | |
//it could come from anywhere, such as Sql Server | |
//string s = "this is some text"; | |
System.Data.DataTable workTable = new System.Data.DataTable(); | |
workTable.TableName = "Customers"; | |
workTable.Columns.Add("Id"); | |
workTable.Columns.Add("Name"); | |
System.Data.DataRow workRow; | |
for (int i = 0; i <= 9; i++) | |
{ | |
workRow = workTable.NewRow(); | |
workRow[0] = i; | |
workRow[1] = "CustName" + i.ToString(); | |
workTable.Rows.Add(workRow); | |
} | |
string strBody = DataTable2ExcelString(workTable); | |
byte[] data = Encoding.ASCII.GetBytes(strBody); | |
return data; | |
} | |
static string DataTable2ExcelString(System.Data.DataTable dt) | |
{ | |
StringBuilder sbTop = new StringBuilder(); | |
sbTop.Append("<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" "); | |
sbTop.Append("xmlns=\" http://www.w3.org/TR/REC-html40\"><head><meta http-equiv=Content-Type content=\"text/html; charset=windows-1252\">"); | |
sbTop.Append("<meta name=ProgId content=Excel.Sheet ><meta name=Generator content=\"Microsoft Excel 9\"><!--[if gte mso 9]>"); | |
sbTop.Append("<xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>" + dt.TableName + "</x:Name><x:WorksheetOptions>"); | |
sbTop.Append("<x:Selected/><x:ProtectContents>False</x:ProtectContents><x:ProtectObjects>False</x:ProtectObjects>"); | |
sbTop.Append("<x:ProtectScenarios>False</x:ProtectScenarios></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets>"); | |
sbTop.Append("<x:ProtectStructure>False</x:ProtectStructure><x:ProtectWindows>False</x:ProtectWindows></x:ExcelWorkbook></xml>"); | |
sbTop.Append("<![endif]-->"); | |
sbTop.Append("</head><body><table>"); | |
string bottom = "</table></body></html>"; | |
StringBuilder sb = new StringBuilder(); | |
//Header | |
sb.Append("<tr>"); | |
for (int i = 0; i < dt.Columns.Count; i++) | |
{ | |
sb.Append("<td>" + dt.Columns[i].ColumnName + "</td>"); | |
} | |
sb.Append("</tr>"); | |
//Items | |
for (int x = 0; x < dt.Rows.Count; x++) | |
{ | |
sb.Append("<tr>"); | |
for (int i = 0; i < dt.Columns.Count; i++) | |
{ | |
sb.Append("<td>" + dt.Rows[x][i] + "</td>"); | |
} | |
sb.Append("</tr>"); | |
} | |
string SSxml = sbTop.ToString() + sb.ToString() + bottom; | |
return SSxml; | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment