Sending a Memory Stream

    Date: 01/19/07 (C Sharp)    Keywords: html

    I'm trying to send an email attachment "Data.txt" which in the end, is a tab-delimited file. I want to do this without creating a file on the server, so I'm reading the data in to a memory stream, and sending it across. The problem is that when the email arrives, the file itself is empty. Here is my sample code:

    private void SendEmail()
    {
    try
    {
    string strEmailFrom = txtFrom.Text;
    string strEmailT0 = txtTo.Text;

    string strSubject = "Email test";


    MemoryStream msData = new MemoryStream();

    TextWriter twReport = new StreamWriter(msData);

    for (int r = 0; r < 10; r++)
    {
    for (int c = 0; c < 10; c++)
    {
    string sValue = "r=" + r.ToString() + ";c=" + c.ToString();
    twReport.Write(sValue);
    twReport.Write("\t");
    }
    twReport.Write(System.Environment.NewLine);
    }

    string strBody = "Email Test Body";

    twReport.Flush();

    SendEmailDirect(strEmailFrom, strEmailT0, strSubject, strBody, msData, "ReportDataEmail.txt", true);

    twReport.Close();
    }
    catch (Exception e)
    {
    MessageBox.Show(e.ToString());
    }
    }

    public static void SendEmailDirect(string strEmailFrom, string strEmailTo, string strSubject, string strBody, System.IO.Stream streamData, string strAttName, bool bIsHtml)
    {

    SmtpClient oSmtpClient = new SmtpClient(GetSMTPClient());
    MailMessage mailMessage = new MailMessage();
    mailMessage.IsBodyHtml = bIsHtml;

    mailMessage.To.Add(strEmailTo);
    mailMessage.From = new MailAddress(strEmailFrom);
    mailMessage.Subject = strSubject;
    mailMessage.Body = strBody;

    System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType();
    ct.MediaType = System.Net.Mime.MediaTypeNames.Text.Plain;
    ct.Name = strAttName;



    Attachment attReport = new Attachment(streamData, ct);


    mailMessage.Attachments.Add(attReport);

    oSmtpClient.Send(mailMessage);
    }


    What am I doing wrong?

    Source: http://community.livejournal.com/csharp/80618.html

« drag and drop outlook contacts || What Would a SQL Programmer... »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home