Unable to create and send dynamic CSV file as attachment over mail

Sachin

I am trying to create and send dynamic csv file as attachment over mail, but it is not working. I am able to send mail with attachement, but file is going as empty. Below is my code:

var notProcessedRecords = FileFTPData.Where(x => x.LastName.StartsWith("9999")).ToList();

      string loggedInUserName = "Administrator";

      try
      {
      MemoryStream stream = new MemoryStream();
      TextWriter writer = new StreamWriter(stream, Encoding.Default);
      StringBuilder sb = new StringBuilder();
      sb.AppendFormat(string.Format("{0}, {1}, {2}, {3}, {4}, {5} ", "FIRSTNAME", "LASTNAME", "USERID", "COMPANYNAME", "EMAIL", "PHONE"));

      foreach (var item in notProcessedRecords)
      {
          sb.AppendLine();
          sb.AppendFormat(string.Format("{0}, {1}, {2}, {3}, {4}, {5} ", item.FirstName, item.LastName, item.UserId, item.CompanyName, item.Email, item.Phone));
      }
      writer.WriteLine(sb);

      MailMessage mail = new MailMessage();
      SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
      mail.From = new MailAddress("******@gmail.com");
      mail.To.Add("*******@yahoo.com");
      mail.Subject = "FTP file processing status";
      mail.Body = "<div>Hello " + loggedInUserName + ", </br></br> Following item are restricted from processing, due to some errors. Please check Process description for the same. </br></br>  From, </br>Streben Support </div>";
      mail.IsBodyHtml = true;

      Attachment attachment = new Attachment(stream, new ContentType("text/csv"));
      attachment.Name = "test.csv";
      mail.Attachments.Add(attachment);

      SmtpServer.Port = 587;
      SmtpServer.Credentials = new System.Net.NetworkCredential("******@gmail.com", "******");
      SmtpServer.EnableSsl = true;

      SmtpServer.Send(mail);

      }
      catch(Exception ex)
      {
      }

Can anybody help me in correcting the issue?

Mike Hixson

As you have just finished writing to the stream the Position property will be set to the end of the stream. Additionally, there may still be some data buffered in the writer that hasn't been flushed to the stream. Because of this, when the attachment is created from the stream it will appear to be empty. To fix this do:

// Done writing here //

writer.Flush();
stream.Position = 0;

// Create attachment here //

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to send mail in Katalon ( attachment Screenshot on failure )

Send mail with file attachment

Unable to send mail via Mailgun over api or smtp

If there is no attachment file is present at path mail should not send

Create ics file and send email with Attachment using c#

Send pdf in attachment (Laravel Mail)

Mailgun send mail with attachment

send mail attachment powershell

Attachment file name get's changed when send to IMAP mail box from JAVA mail

Send mail with large attachment?

send mail with attachment body

How do I send a generated csv file as an email attachment in React

In Python 3 when I send a .csv file as an attachment it is missing lines

mail: send email with attachment from commandline

PHPMailer send mail but file attachment is not with it

How to write a new CSV file and send as Email attachment

Unable to send attachment using PHPmailer

JMeter - Unable to send mail with "Subject", Body Messages & attachment using SMTP client

Send mail with an attachment and body of mail with table format

multple file attachment in mail with php

How to send wp_mail with attachment (file-path in variable)

Unable to send mail using python with multiple attachment and multiple recipients [to,cc,bcc]

mail with body/message from remote file over ssh + attachment

wait file to create before send mail

Convert Gmail mail into file and send over a webservice

Send mail with attachment CSV showing base64 format

Unable to send attachment to Discord channel

Unable to send image attachment to backend

Send attachment file in outgoing mail body in rails