Hi, I'm trying to create an attachment to send via email on the fly and I was attempting to use the memorystream object. However, whenever I send the email, I receive it, but the attachment is a 0 byte file.
The thing that is confusing me is that I have stepped through the code and I have verified that the memorystream the attachment has been created off of has > 0 bytes of data up to and after executing the Send() method. However, I can't figure out why those bytes are never getting transmitted.
Can someone please help me to understand what is going on
To demonstrate what is happening, please see my code below.
StringBuilder msg;
System.Net.Mail.Attachment attachment;
System.Net.Mime.ContentType attachType;
MemoryStream attach;
StreamWriter writ;
using (attach = new MemoryStream())
{
//construct an email to send to the offending team member
msg.Append("my message");
//create the email attachment
attachText = "some data for an attachment\r\n";
writ = new StreamWriter(attach);
writ.Write(attachText);
writ.Flush();
attachType = new System.Net.Mime.ContentType();
attachType.MediaType = System.Net.Mime.MediaTypeNames.Text.Plain;
attachType.Name = "ARTask.ARtask";
attachments.Add(new System.Net.Mail.Attachment(attach, attachType));
//send the email
Utilities.SendEmail("from@email.com", "to@email.com", msg.ToString(), "the subject", null, attachments);
}
From the Utilities Class
public static bool SendEmail(string from, string to, string msg,
string subject, string bcc,
List attachments)
{
SmtpClient srv = new SmtpClient(Properties.Settings.Default.SMTPServer);
MailMessage myMail;
try
{
using (myMail = new MailMessage())
{
myMail.From = new MailAddress(from);
myMail.To.Add(to);
myMail.Body = msg;
myMail.Subject = subject;
myMail.IsBodyHtml = true;
//was there someone to bcc
if (!String.IsNullOrEmpty(bcc)) //Yes
myMail.Bcc.Add(bcc);
//are there attachments
if (attachments != null && attachments.Count > 0) //Yes
{
foreach (System.Net.Mail.Attachment attach in attachments)
myMail.Attachments.Add(attach);
}
srv.Send(myMail);
}
return true;
}
catch (System.Exception ex)
{
return false;
}
}
I also took a diagnostics trace and the following is what I found.
System.Net.Sockets Verbose: 0 : [3156] Exiting Socket#20304321::Send() -> 286#286 DateTime=2006-05-05T14:46:02.3083500Z System.Net Information: 0 : [3156] HeaderCollection#3129430::Get(content-type) DateTime=2006-05-05T14:46:02.3083500Z System.Net Information: 0 : [3156] HeaderCollection#3129430::Get(content-transfer-encoding) DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] Socket#20304321::Send() DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] Data from Socket#20304321::Send DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] 00000000 : 0D 0A 2D 2D 2D 2D 62 6F-75 6E 64 61 72 79 5F 30 : ..----boundary_0 DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] 00000010 : 5F 32 61 64 63 34 30 64-38 2D 66 34 30 61 2D 34 : _2adc40d8-f40a-4 DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] 00000020 : 65 36 30 2D 62 62 62 38-2D 32 38 32 64 34 37 35 : e60-bbb8-282d475 DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] 00000030 : 33 63 32 64 65 0D 0A 63-6F 6E 74 65 6E 74 2D 74 : 3c2de..content-t DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] 00000040 : 79 70 65 3A 20 74 65 78-74 2F 70 6C 61 69 6E 3B : ype: text/plain; DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] 00000050 : 20 6E 61 6D 65 3D 41 52-54 61 73 6B 2E 41 52 74 : name=ARTask.ARt DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] 00000060 : 61 73 6B 0D 0A 63 6F 6E-74 65 6E 74 2D 74 72 61 : ask..content-tra DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] 00000070 : 6E 73 66 65 72 2D 65 6E-63 6F 64 69 6E 67 3A 20 : nsfer-encoding: DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] 00000080 : 62 61 73 65 36 34 0D 0A-0D 0A : base64.... DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] Exiting Socket#20304321::Send() -> 138#138 DateTime=2006-05-05T14:46:02.3083500Z System.Net Information: 0 : [3156] HeaderCollection#3129430::Get(content-transfer-encoding) DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] Socket#20304321::Send() DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] Data from Socket#20304321::Send DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] 00000000 : 0D 0A 2D 2D 2D 2D 62 6F-75 6E 64 61 72 79 5F 30 : ..----boundary_0 DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] 00000010 : 5F 32 61 64 63 34 30 64-38 2D 66 34 30 61 2D 34 : _2adc40d8-f40a-4 DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] 00000020 : 65 36 30 2D 62 62 62 38-2D 32 38 32 64 34 37 35 : e60-bbb8-282d475 DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] 00000030 : 33 63 32 64 65 2D 2D 0D-0A : 3c2de--.. DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] Exiting Socket#20304321::Send() -> 57#57 DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] Socket#20304321::Send() DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] Data from Socket#20304321::Send DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] 00000000 : 0D 0A : .. DateTime=2006-05-05T14:46:02.3083500Z System.Net.Sockets Verbose: 0 : [3156] Exiting Socket#20304321::Send() -> 2#2 DateTime=2006-05-05T14:46:02.3083500Z
Again, I haven't been able to make sense of why there is no data coming through the attachment. If someone can help me to understand what is going wrong, I'd greatly appreciate it.
Thanks,
Jon

Getting 0 byte email attachment when constructing attachment using memorystream
jonpet
Thanks. That was exactly what I forgot to do. It works great now and there are no files to clean up after sending the attachment.
froodien