Reputation: 505
I'm trying to attach a image to my email. I'm doing it this way.
SmtpClient smtpClient = new SmtpClient();
MailMessage mailDSS = new MailMessage();
//Setting From , To and CC
mailDSS.From = new MailAddress(billModel.Email, "from: " + billModel.Email);
mailDSS.To.Add(new MailAddress("[email protected]"));
mailDSS.Subject = "This is my subject";
mailDSS.Attachments.Add(new Attachment(billModel.UploadedBill.Path + billModel.UploadedBill.Filename));
mailDSS.Attachments.Add(aAttachment);
mailDSS.Body = "This is the content :" + billModel.Name;
smtpClient.Send(mailDSS);
And it works but when I try to change it to a memory stream it doesn't work. this is what I'm trying to make work:
mailDSS.From = new MailAddress(billModel.Email, "from: " + billModel.Email);
mailDSS.To.Add(new MailAddress("[email protected]"));
mailDSS.Subject = "This is my subject";
billModel.UploadedBill.FileDataStream.Seek(0,SeekOrigin.Begin);
Attachment aAttachment = new Attachment(billModel.UploadedBill.FileDataStream, billModel.UploadedBill.Filename);
aAttachment.ContentDisposition.FileName = billModel.UploadedBill.Filename;
mailDSS.Attachments.Add(aAttachment);
mailDSS.Body = "This is the content :" + billModel.Name;
smtpClient.Send(mailDSS);
what am I doing wrong ? the only difference between the two is one is a reference to a file on the disk where the other is a reference to a file from a stream =?
BillModel is just a domain model class made serializable.
public class BillModel
{
public string Name { get; set; }
public string CivilRegistrationNo { get; set; }
public string Email { get; set; }
public int Phone { get; set; }
public int RegNo { get; set; }
public int AccountNo { get; set; }
public string PaymentGuarentee { get; set; }
[XmlIgnore]
public MemoryFileStream UploadedBill { get; set; }
}
Edit: There are no error messages the email is sent fine the attachment however is only 227bytes on arrival where the attachments should had been 20 kilobytes. So I do not know how I can describe my problem anymore clear. My debugging amounts to trying different approaches some didn't work some is a small variation of the above-mentioned. All have the same issue with corrupting the file.
If there is something specific you need me to show in order to help me I will be happy to provide but atm I do not see what else I can show? It simply doesn't work as I intended.
Upvotes: 1
Views: 72
Reputation: 505
I found my problem.
the problem was not in what I showed. but where I was running this from.
in my testmethod I did this
using (System.IO.Stream fileData = System.IO.File.OpenRead(TestFilenamePath))
{
string filenamePath = aBillModel.UploadedBill.Path + filename;
using (System.IO.FileStream newFileData = System.IO.File.Create(filenamePath))
{
fileData.Position = 0;
fileData.CopyTo(newFileData);
}
fileData.CopyTo(aBillModel.UploadedBill.FileDataStream);
//Perform Action to be tested
bool emailSendt = EmailSender.Send(aBillModel);
//Test Result of Action
Assert.That(emailSendt, Is.True);
}
And apparently I couldn't both write the data to a location on the disk and at the same time send it with an email. So I did this instead.
using (System.IO.Stream fileData = System.IO.File.OpenRead(TestFilenamePath))
{
string filenamePath = aBillModel.UploadedBill.Path + filename;
using (System.IO.FileStream newFileData = System.IO.File.Create(filenamePath))
{
fileData.Position = 0;
fileData.CopyTo(newFileData);
}
}
using (System.IO.Stream fileData = System.IO.File.OpenRead(TestFilenamePath))
{
fileData.CopyTo(aBillModel.UploadedBill.FileDataStream);
//Perform Action to be tested
bool emailSendt = EmailSender.Send(aBillModel);
//Test Result of Action
Assert.That(emailSendt, Is.True);
}
and it solved my problem of receiving a corrupt file of only a few bytes in size instead of getting the file of 20Kb in size.
Upvotes: 1