1

I trying to send email with attachment using ASP .Net Core 3.1 and C#. Initially the data's are loaded from database and attaching it to Mail-Message as a Memory Stream , I could able to receive the mail but the content inside attachment is always blank.

i tried text format , CSV format , PDF and every attachment i receive along the mail is blank , dont know what i am doing wrong , below is the code,

public void SendEmail(DataTable table)   
{
    MemoryStream _oMemStream = new MemoryStream();
    StreamWriter sw = new StreamWriter(_oMemStream);
    foreach (DataColumn column in table.Columns)
    {
        sw.Write(column.ColumnName.ToString() + ",");
    }
    foreach (DataRow row in table.Rows)
    {
        for (int i = 0; i < row.ItemArray.Length; i++)
        {
            string rowText = row.ItemArray[i].ToString();
            sw.Write(rowText + ",");
        }
        sw.WriteLine();
    }

    EmailDetailsModel emDetails = new EmailDetailsModel();
    emDetails.Body = "Body Of the Mail For Email Functionality";
    emDetails.Email = "xxxx";
    emDetails.To = "xxx";
    emDetails.Subject = "Testing Email Functionality";
    emDetails.Password = "xxx";

    using (MailMessage mm = new MailMessage(emDetails.Email, emDetails.To))
    {
        _oMemStream.Position = 0;
        mm.Subject = emDetails.Subject;
        mm.Body = emDetails.Body;
        var contentType = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Pdf);
        var reportAttachment = new Attachment(_oMemStream, contentType);
        reportAttachment.ContentDisposition.FileName = "yourFileName.pdf";
        mm.Attachments.Add(reportAttachment);
        mm.IsBodyHtml = false;

        using (SmtpClient smtp = new SmtpClient())
        {
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            NetworkCredential NetworkCred = new NetworkCredential(emDetails.Email, emDetails.Password);
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = xx;
            smtp.Send(mm);
            ViewBag.Message = "Email sent.";
        }
    }
}

Thank you for the suggestion

sayah imad
  • 1,507
  • 3
  • 16
  • 24
  • If it is blank and not getting an exception it could be a Virus Checker of Firewall is removing the attachment. – jdweng Jun 06 '20 at 12:35
  • 1
    Actually i am trying to send just one line of string in the attachment so the possibility of virus/firewall is not at all possible. There should be a programmatic issue which i am trying to fix – Prawin Arumugham Jun 06 '20 at 12:45
  • The firewall may be checking for text words. Try just sending HELLO and see what happens. – jdweng Jun 06 '20 at 12:53
  • I don't know how writing text to a stream suddenly makes this a PDF file. But the problem is you are not actually writing the stream properly. Have a look at the answer tranquil tarn posted [Attach a file from MemoryStream to a MailMessage in C#](https://stackoverflow.com/questions/5336239/attach-a-file-from-memorystream-to-a-mailmessage-in-c-sharp) – Barns Jun 06 '20 at 12:54
  • @Barns Even i tried as text file before PDF but for that also got a blank content in attachment. – Prawin Arumugham Jun 06 '20 at 13:18
  • @jdweng would give a try – Prawin Arumugham Jun 06 '20 at 13:19
  • Did you not try the solution I linked to in the comment above. The core of my comment was that you are not using the stream properly...which is why your file is "empty". – Barns Jun 06 '20 at 16:54
  • @Barns i tried those even before posting this question , since that did not worked i posted a separate question. – Prawin Arumugham Jun 06 '20 at 17:01

2 Answers2

1

This helped to solve this issue.

https://gist.github.com/mvark/8c523eb47670c2fc8da4

0

Did you try adding sw.Close(); before yiur MailMessage building ?

acinace
  • 96
  • 4