Scenario: From FrontEnd user will give subject,body,attach 1 or more file (can be any format(pdf,excel,jpeg))
Issue: I wrote below code and getting email perfectly, but am not able to open attached file in received mail ,
bool fileFound = false;
byte[] bytes = new byte[] { };
using (MailMessage mail = new MailMessage())
{
var from = new MailAddress("****.com", "notification Order");
mail.Subject = Subject;
string header = Body;
string footer =
mail.Body = header + footer;
if (Request.Form.Files.Count > 0)
{
var files = Request.Form.Files;
foreach (var data in files)
{
fileFound = true;
if (data.Length > 0)
{
//var fileName = ContentDispositionHeaderValue.Parse(data.ContentDisposition).FileName.Trim('"');
using (var reader = new StreamReader(data.OpenReadStream()))
{
string contentAsString = reader.ReadToEnd();
bytes = new byte[contentAsString.Length * sizeof(char)];
System.Buffer.BlockCopy(contentAsString.ToCharArray(), 0, bytes, 0, bytes.Length);
MemoryStream stream2 = new MemoryStream(bytes);
string filename = Path.GetFileName(data.FileName);
mail.Attachments.Add(new Attachment(stream2, filename));
}
}
}
}
mail.IsBodyHtml = true;
mail.From = from;
Please help me out (MVC 6)