/// <summary> /// Loads a list of recipients. /// </summary> /// <param name="element"></param> /// <param name="name"></param> private void LoadRecipients(XmlElement element, string name, MailMessageRecipientCollection recipients) { if (element == null) { throw new ArgumentNullException("element"); } if (name == null) { throw new ArgumentNullException("name"); } if (name.Length == 0) { throw new ArgumentOutOfRangeException("'name' is zero-length."); } if (recipients == null) { throw new ArgumentNullException("recipients"); } // clear... recipients.Clear(); // get... foreach (XmlElement recipientElement in element.SelectNodes(name + "/Recipient")) { recipients.Add(MailMessageRecipient.FromXmlElement(recipientElement)); } }
/// <summary> /// Populates the list from a string. /// </summary> /// <param name="value"></param> public void AddRange(string value) { // anything? if (value == null) { return; } value = value.Trim(); if (value.Length == 0) { return; } // split... string[] parts = value.Split(';'); foreach (string part in parts) { string usePart = part.Trim(); if (usePart.Length > 0) { MailMessageRecipient recipient = MailMessageRecipient.Parse(usePart); if (recipient == null) { throw new InvalidOperationException("recipient is null."); } this.Add(recipient); } } }
/// <summary> /// Loads a recipient from an XML element. /// </summary> /// <param name="element"></param> // mbr - 16-11-2007 - added. internal static MailMessageRecipient FromXmlElement(XmlElement element) { if (element == null) { throw new ArgumentNullException("element"); } // create... MailMessageRecipient recipient = new MailMessageRecipient(); // name... string displayName = XmlHelper.GetElementString(element, "DisplayName", OnNotFound.ReturnNull); if (displayName != null && displayName.Length == 0) { displayName = null; } recipient._displayName = displayName; // email... recipient._email = XmlHelper.GetElementString(element, "Email", OnNotFound.ReturnNull); // return... return(recipient); }
/// <summary> /// Removes a MailMessageRecipient item to the collection. /// </summary> /// <param name="item">The item to remove.</param> public void Remove(MailMessageRecipient item) { if (item == null) { throw new ArgumentNullException("item"); } List.Remove(item); }
/// <summary> /// Inserts a MailMessageRecipient instance into the collection. /// </summary> /// <param name="item">The item to add.</param> public void Insert(int index, MailMessageRecipient item) { if (item == null) { throw new ArgumentNullException("item"); } List.Insert(index, item); }
/// <summary> /// Loads data from an XML element. /// </summary> /// <param name="element"></param> // mbr - 16-11-2007 - added. private void LoadFromXmlElement(XmlElement element) { if (element == null) { throw new ArgumentNullException("element"); } // from... XmlElement fromElement = (XmlElement)element.SelectSingleNode("From"); if (fromElement == null) { throw new InvalidOperationException("fromElement is null."); } this.From = MailMessageRecipient.FromXmlElement(fromElement); // recipients... this.LoadRecipients(element, "To", this.To); this.LoadRecipients(element, "Cc", this.Cc); this.LoadRecipients(element, "Bcc", this.Bcc); var replyToElement = (XmlElement)element.SelectSingleNode("ReplyTo"); if (replyToElement != null) { this.ReplyTo = MailMessageRecipient.FromXmlElement(replyToElement); } // basics... this.Subject = XmlHelper.GetElementString(element, "Subject", OnNotFound.ReturnNull); this.Headers = XmlHelper.GetElementString(element, "Headers", OnNotFound.ReturnNull); this.MailFormat = (MailFormat)XmlHelper.GetElementEnumerationValue(element, "MailFormat", typeof(MailFormat), OnNotFound.ReturnNull); this.Date = XmlHelper.GetElementDateTime(element, "Date", OnNotFound.ReturnNull); this.Body = XmlHelper.GetElementString(element, "Body", OnNotFound.ReturnNull); // attachments... foreach (XmlElement attachmentElement in element.SelectNodes("Attachments/Attachment")) { MailAttachment attachment = MailAttachment.FromXmlElement(attachmentElement); this.Attachments.Add(attachment); } // embeds... foreach (XmlElement embedElement in element.SelectNodes("EmbeddedImages/EmbeddedImage")) { var name = embedElement.GetElementString("Name"); var asString = embedElement.GetElementString("Data"); var contentType = embedElement.GetElementString("ContentType"); var contentId = embedElement.GetElementString("ContentId"); var ext = Path.GetExtension(name); var temp = Runtime.Current.GetTempFilePath(ext); var bs = Convert.FromBase64String(asString); File.WriteAllBytes(temp, bs); this.EmbeddedImages.Add(new MailEmbeddedImage(name, temp, contentType, contentId, true)); } }
/// <summary> /// Discovers if the given item is in the collection. /// </summary> /// <param name="item">The item to find.</param> /// <returns>Returns true if the given item is in the collection.</returns> public bool Contains(MailMessageRecipient item) { if (IndexOf(item) == -1) { return(false); } else { return(true); } }
/// <summary> /// Configures and sends a message via SMTP. /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <param name="cc"></param> /// <param name="bcc"></param> /// <param name="subject"></param> /// <param name="body"></param> /// <param name="format"></param> public static void SendViaSmtp(MailMessageRecipient from, MailMessageRecipientCollection to, MailMessageRecipientCollection cc, MailMessageRecipientCollection bcc, string subject, string body, MailFormat format, string hostName, string username, string password) { SendViaSmtp(from, to, cc, bcc, subject, body, format, new SmtpConnectionSettings(hostName, username, password)); }
/// <summary> /// Configures and sends a message via SMTP. /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <param name="cc"></param> /// <param name="bcc"></param> /// <param name="subject"></param> /// <param name="body"></param> /// <param name="format"></param> public static void SendViaSmtp(MailMessageRecipient from, MailMessageRecipientCollection to, MailMessageRecipientCollection cc, MailMessageRecipientCollection bcc, string subject, string body, MailFormat format, SmtpConnectionSettings settings) { if (from == null) { throw new ArgumentNullException("from"); } // anyone? int total = 0; if (to != null) { total += to.Count; } if (cc != null) { total += cc.Count; } if (bcc != null) { total += bcc.Count; } // throw? if (total == 0) { throw new InvalidOperationException("You must specify at least one person to send the message to."); } // create... MailMessage message = new MailMessage(); message.From = from; message.To.Clear(); if (to != null) { message.To.AddRange(to); } message.Cc.Clear(); if (cc != null) { message.Cc.AddRange(cc); } message.Bcc.Clear(); if (bcc != null) { message.Bcc.AddRange(bcc); } message.Subject = subject; message.Body = body; message.MailFormat = format; // send... ISmtpProvider provider = GetSmtpProvider(); if (provider == null) { throw new InvalidOperationException("provider is null."); } // set... provider.Settings = settings; // run... provider.Send(message); }
/// <summary> /// Returns the index of the item in the collection. /// </summary> /// <param name="item">The item to find.</param> /// <returns>The index of the item, or -1 if it is not found.</returns> public int IndexOf(MailMessageRecipient item) { return(List.IndexOf(item)); }