2

I have this routine that sends email to a list as follows and I have tested it. The first email that comes to me is fine but the second one has less bytes that the actual file rendering it corrupt for opening. How can I make it so that each email attachment is sent properly without sending any 0 byte attachments? I am sending the attachments through a ASP.NET C# webform directly to recipients but most of the PDF attachments come through as improperly decoded with that error message. Here is the main email code of the page:

if (emailIsValid(EmailToSend))
{
    string TheSubject = SubjectTxt.Text;
    string TheBody = "Dear " + FirstName + ",<br/><br/>" + MessageTxt.Text;
    TheBody = TheBody + " EMail Body "
    string BodyTxt = TheBody.Replace(Environment.NewLine, "<br />");

    MailMessage mailObj = new MailMessage(
        "noreply@company.com", EmailToSend, TheSubject, BodyTxt);
    SmtpClient SMTPServer = new SmtpClient("unknown.company.com");

    string RPT = FromTxt.Text;
    mailObj.ReplyToList.Add(RPT);
    mailObj.BodyEncoding = System.Text.Encoding.UTF8;
    mailObj.IsBodyHtml = true;


    string filePath = txtAttachment.PostedFile.FileName;
    string filename = Path.GetFileName(filePath);
    string ext = Path.GetExtension(filename);
    string contenttype = String.Empty;

    //Set the contenttype based on File Extension
    switch (ext)
    {
        case ".doc":
            contenttype = "application/vnd.ms-word";
            break;
        case ".docx":
            contenttype = "application/vnd.ms-word";
            break;
        case ".xls":
            contenttype = "application/vnd.ms-excel";
            break;
        case ".xlsx":
            contenttype = "application/vnd.ms-excel";
            break;
        case ".ppt":
            contenttype = "application/vnd.ms-powerpoint";
            break;
        case ".pptx":
            contenttype = "application/vnd.ms-powerpoint";
            break;
        case ".jpg":
            contenttype = "image/jpg";
            break;
        case ".png":
            contenttype = "image/png";
            break;
        case ".gif":
            contenttype = "image/gif";
            break;
        case ".pdf":
            contenttype = "application/pdf";
            break;
        case ".csv":
            contenttype = "text/csv";
            break;
        case ".txt":
            contenttype = "text/csv";
            break;
        default:
            contenttype = "Unknown Content Type";
            break;


    }

    if (txtAttachment.PostedFile != null && contenttype != "Unknown Content Type")
    {
        try
        {
            string strFileName =
            System.IO.Path.GetFileName(txtAttachment.PostedFile.FileName);
            Attachment attachFile =
            new Attachment(txtAttachment.PostedFile.InputStream, strFileName, contenttype);                                           
            mailObj.Attachments.Add(attachFile);
        }
        catch
        {

        }
    }

    try
    {
        SMTPServer.Send(mailObj);

        SqlConnection con2 = new SqlConnection(CS);

        con2.Open();

        DateTime now = DateTime.Now;
    }
catch
    {
    }
mason
  • 31,774
  • 10
  • 77
  • 121
Jason
  • 652
  • 8
  • 23
  • You have an empty catch block in your code. You should almost never do that. It's an anti-pattern. If you're not going to do anything about the failed email, at least log that it happened. – mason Dec 23 '15 at 15:45
  • Okay thanks but that is not what is causing my problem with the emails – Jason Dec 23 '15 at 15:46
  • Instead of setting `contenttype` to "Unknown Content Type" you should use "application/octet-stream" as described in the question [Is there a “default” MIME type?](http://stackoverflow.com/questions/12539058/is-there-a-default-mime-type) Also you should follow C# standard naming conventions. `contenttype` -> `contentType`, `strFileName` -> `fileName`, `mailObj` -> `message` or `mailMessage` etc. – mason Dec 23 '15 at 16:11

1 Answers1

8

Before

Attachment attachFile =
        new Attachment(txtAttachment.PostedFile.InputStream, strFileName, contenttype);

try resetting the stream cursor, like this:

txtAttachment.PostedFile.InputStream.Position = 0;
Attachment attachFile =
        new Attachment(txtAttachment.PostedFile.InputStream, strFileName, contenttype);
Anderson Pimentel
  • 5,086
  • 2
  • 32
  • 54