/// <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(string from, string toAsString, string ccAsString, string bccAsString, string subject, string body, MailFormat format, string hostName, string username, string password) { // add... MailMessageRecipientCollection to = new MailMessageRecipientCollection(); if (toAsString != null && toAsString.Length > 0) { to.AddRange(toAsString); } MailMessageRecipientCollection cc = new MailMessageRecipientCollection(); if (ccAsString != null && ccAsString.Length > 0) { cc.AddRange(ccAsString); } MailMessageRecipientCollection bcc = new MailMessageRecipientCollection(); if (bccAsString != null && bccAsString.Length > 0) { bcc.AddRange(bccAsString); } // defer... SendViaSmtp(new MailMessageRecipient(from), to, cc, bcc, subject, body, format, 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(string from, string toAsString, string ccAsString, string bccAsString, string subject, string body, MailFormat format, SmtpConnectionSettings settings) { if (settings == null) { throw new ArgumentNullException("settings"); } // add... MailMessageRecipientCollection to = new MailMessageRecipientCollection(); if (toAsString != null && toAsString.Length > 0) { to.AddRange(toAsString); } MailMessageRecipientCollection cc = new MailMessageRecipientCollection(); if (ccAsString != null && ccAsString.Length > 0) { cc.AddRange(ccAsString); } MailMessageRecipientCollection bcc = new MailMessageRecipientCollection(); if (bccAsString != null && bccAsString.Length > 0) { bcc.AddRange(bccAsString); } // defer... SendViaSmtp(new MailMessageRecipient(from), to, cc, bcc, subject, body, format, settings); }
/// <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> /// Adds the recipients to XML. /// </summary> /// <param name="doc"></param> /// <param name="name"></param> /// <param name="recipients"></param> // mbr - 16-11-2007 - added. private void AddRecipientsToXml(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."); } // create... XmlElement subElement = element.OwnerDocument.CreateElement(name); element.AppendChild(subElement); // walk... foreach (MailMessageRecipient recipient in recipients) { XmlElement recipientElement = element.OwnerDocument.CreateElement("Recipient"); subElement.AppendChild(recipientElement); // populate.... recipient.PopulateXmlElement(recipientElement); } }
/// <summary> /// Gets a list of all recipients. /// </summary> /// <returns></returns> public MailMessageRecipientCollection GetAllRecipients() { MailMessageRecipientCollection results = new MailMessageRecipientCollection(); results.AddRange(this.To); results.AddRange(this.Cc); results.AddRange(this.Bcc); // return... return(results); }
/// <summary> /// Adds a set of MailMessageRecipient instances to the collection. /// </summary> /// <param name="item">The item to add.</param> public void AddRange(MailMessageRecipientCollection items) { if (items == null) { throw new ArgumentNullException("items"); } for (int index = 0; index < items.Count; index++) { Add(items[index]); } }
private void CopyAddresses(Nm.MailAddressCollection addresses, MailMessageRecipientCollection recipients) { if (addresses == null) { throw new ArgumentNullException("addresses"); } if (recipients == null) { throw new ArgumentNullException("recipients"); } // copy... addresses.Clear(); foreach (MailMessageRecipient recipient in recipients) { addresses.Add(recipient.ToMailAddress()); } }
/// <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); }