سؤال

I am receiving emails through hmailserver and sending those emails as .eml file as an attachment of another report email.

I am having issues in reading and sending those emails as an attachment.

This is what I am doing.

public void addAttachment(string pathname, bool retry)
    {
        string attachmentname = "";
        do
        {
            try
            {
                attachmentname = Path.GetFileNameWithoutExtension(pathname);
                Stream file = new MemoryStream(File.ReadAllBytes(pathname));
                Log.WriteMessage("Size" + file.Length);
                dtstreamAttach.Add(attachmentname+".eml", file);
                retry = false;
            }
            catch (ArgumentException e)
            {
                string strCurrentTs = DateTime.Now.ToString(strDateFormat);
                attachmentname = attachmentname + "-" + strCurrentTs+".eml";
            }
        } while (retry);
    }

Then,

            MailMessage message = new MailMessage();
            . 
            .
            message.Attachments.Add(new Attachment(kvp.Value, kvp.Key)); // I have an attachment dictionary 
            string contenttype = GetMimeType(".eml");
            ContentType cnttype = new ContentType(contenttype);
            message.Attachments[0].ContentType = cnttype;

as you see i print the Stream size - which prints out as something like 4790Bytes (4KB) But when i receive the email, i only get an eml file with size 1KB and the eml file is empty.

I have checked the file paths and also made sure that the email is there until my report mail is sent out. I have also verified content type is message/rfc822.

Everything seems to check out. Not sure what the issue is.

هل كانت مفيدة؟

المحلول

I was able to resolve it. Looks like the MemoryStream have the correct stream and also the ContentStream of the message object has the correct sizes, but the Stream postion had moved to the end of the stream and hence when the message is actually being sent, it actualy has nothing.

So make sure to reposition the stream back to origin before adding it to AttachmentCollection, something like

Seek(0, SeekOrigin.Begin)

especially when using streams wrapped in dlls, something might have moved them.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top