public SmtpClient(Account account, OutgoingEmail email) { Account = account; NewEmail = email; }
private void SaveFavorites(OutgoingEmail email) { List<string> allOutgoingAddresses = new List<string>(); foreach (string to in email.To) { allOutgoingAddresses.Add(to); } if (email.Cc != null) { foreach (string cc in email.Cc) { allOutgoingAddresses.Add(cc); } } if (email.Bcc != null) { foreach (string bcc in email.Bcc) { allOutgoingAddresses.Add(bcc); } } foreach (string receiver in allOutgoingAddresses) { if (DatabaseManager.InsertContact(FromAccount.AccountName, receiver)) { Trace.WriteLine(receiver + " added to Contacts."); } } }
private bool StoreEntities(OutgoingEmail email, List<string> attachmentList) { foreach (string iter in attachmentList) { FileStream stream = File.OpenRead(iter); if (!stream.CanRead) { return false; } string mimeType = MimeTypes.GetMimeType(iter); ContentType fileType = ContentType.Parse(mimeType); MimePart attachment; if (fileType.IsMimeType("text", "*")) { attachment = new TextPart(fileType.MediaSubtype); foreach (var param in fileType.Parameters) attachment.ContentType.Parameters.Add(param); } else { attachment = new MimePart(fileType); } attachment.FileName = Path.GetFileName(iter); attachment.IsAttachment = true; MemoryBlockStream memoryBlockStream = new MemoryBlockStream(); BestEncodingFilter encodingFilter = new BestEncodingFilter(); byte[] fileBuffer = new byte[4096]; int index, length, bytesRead; while ((bytesRead = stream.Read(fileBuffer, 0, fileBuffer.Length)) > 0) { encodingFilter.Filter(fileBuffer, 0, bytesRead, out index, out length); memoryBlockStream.Write(fileBuffer, 0, bytesRead); } encodingFilter.Flush(fileBuffer, 0, 0, out index, out length); memoryBlockStream.Position = 0; attachment.ContentTransferEncoding = encodingFilter.GetBestEncoding(EncodingConstraint.SevenBit); attachment.ContentObject = new ContentObject(memoryBlockStream); if (attachment != null) email.AttachmentList.Add(attachment); } return true; }
public void SendEmail() { OutgoingEmail email = new OutgoingEmail(); email.To = ExtractRecipients(toAccounts); if (!String.IsNullOrEmpty(CcAccounts)) email.Cc = ExtractRecipients(CcAccounts); if (!String.IsNullOrEmpty(BccAccounts)) email.Bcc = ExtractRecipients(BccAccounts); email.Subject = Subject; email.Message = MessageBody; if (isHtml) email.HtmlPart = HtmlBody; if (Attachments != null) { List<string> attachmentFilePaths = new List<string>(); foreach (AttachmentViewModel attachmentVm in Attachments) { attachmentFilePaths.Add(attachmentVm.FilePath); } StoreEntities(email, attachmentFilePaths); } SmtpClient NewConnection = new SmtpClient(FromAccount, email); if (!NewConnection.Connect()) { Trace.WriteLine(NewConnection.Error); MessageBoxResult result = MessageBox.Show(NewConnection.Error); return; } if (!NewConnection.SendMail(isHtml)) { Trace.WriteLine(NewConnection.Error); MessageBoxResult result = MessageBox.Show(NewConnection.Error); return; } SaveFavorites(email); FinishInteraction(); Attachments.Clear(); NewConnection.Disconnect(); }