private void getAttachmentToMailItem(ref Mail mailItem, string folderPath) { // folderPath = @"C:\Dev\Projects\MinMeddelanden\SydnarkeMinaMeddelande\UploadsFolder"; string[] files = Directory.GetFiles(folderPath); mailItem.Attachments = new List<BO.Attachment>(); //BO BusinessObject project AttachmentHelper attachmentHelper = new AttachmentHelper(); if (files.Length >= 0) // controll for filenNameList is not null { foreach (var filepath in files) { FileInfo fileAttachment = new FileInfo(filepath); BO.Attachment attac = attachmentHelper.GetAttachment(fileAttachment); // create Attachment object mailItem.Attachments.Add(attac); if (attachmentHelper.ValidateAttachment(mailItem.Attachments)) // check if the attachment is valid { LogManager.Log(new Log { Message = string.Format("Attached file: {0} succesfully", fileAttachment.Name), Level = Level.Info }); } } } }
/// <summary> /// Create a new Attachment /// </summary> private BO.Attachment getAttachment(FileInfo info) { if (info.Exists) { var attachment = new BO.Attachment() { Filename = info.Name, ContentType = getAttachmentExtension(info.Name), // getContentType(info), Body = getFileAsBase64ByteArray(info), Size = (int)info.Length }; return(attachment); } throw new ArgumentException(string.Format("Hittar inte filen: {0}. Kontrollera att den inte är borttagen.", info.Name)); }