System.Net.Mail.Attachment.CreateAttachmentFromString

Posted by Pmithun121085 under ASP.NET on 3/28/2012 | Points: 10 | Views : 3631 | Status : [Member] | Replies : 0
OK, This function is killing me, I have everthing setup correclty, I am passing string and attaching the file to an email. The file attaches and is there correctly.

BUT the body of the email also contains the plain test of the string. I have tried every possible solution that I know to get rid of the body that is is automatically putting in there. If i do message.body = "" it is still there. If I do message.body = "this is my body" it puts it above all the html.

I am at a loss, any help is appreciated.

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(ConfigurationSettings.AppSettings["Administrator"].ToString(), MailID, "Lab Report", "Lab Report is attached");

htmlTableContent += "<table><TR>";

foreach (DataColumn dCol1 in ds.Tables[0].Columns)
{
htmlTableContent += "<TD CLASS=printReciept>&nbsp;<B>" + dCol1.ColumnName.ToString() + "</B></TD>";
}

htmlTableContent += "</TR>";

foreach (DataRow dr in ds.Tables[0].Rows)
{
htmlTableContent += "<TR CLASS= printReciept>";

foreach (DataColumn dCol in ds.Tables[0].Columns)
{
string strToPrint;

if (dr[dCol.Ordinal].ToString().Trim() == "")
{
strToPrint = ".";
}
else
{
strToPrint = dr[dCol.Ordinal].ToString().Trim();
}
if (strToPrint.ToUpper() == "TRUE")
{
strToPrint = "Yes";
}
else if (strToPrint.ToUpper() == "FALSE")
{
strToPrint = "No";
}

htmlTableContent += "<TD>&nbsp;" + strToPrint + "</TD>";
}

htmlTableContent += "</TR>";
}
htmlTableContent += "</table>";

message.IsBodyHtml = false;

var attachment = System.Net.Mail.Attachment.CreateAttachmentFromString(htmlTableContent.ToString(), "application/vnd.xls");

message.Attachments.Add(attachment);
SmtpClient smtp = new SmtpClient();

smtp.Host = ConfigurationSettings.AppSettings["SMTPServerName"].ToString();
smtp.Port = 25;
smtp.Credentials = new System.Net.NetworkCredential(ConfigurationSettings.AppSettings["Administrator"].ToString(), ConfigurationSettings.AppSettings["Mailpwd"].ToString());
message.Body = "";
smtp.Send(message);


Mithun P


Responses

(No response found.)

Login to post response