/// <summary> /// greedy version /// </summary> /// <param name="site"></param> /// <param name="from"></param> /// <param name="to"></param> /// <param name="cc"></param> /// <param name="subject"></param> /// <param name="body"></param> /// <param name="attachments"></param> /// <param name="urgent"></param> internal static void SendMailWithAttachment(SPSite site, string from, string to, string cc, string subject, string body, AttachmentInfo[] attachments, bool urgent) { //check if mail server is specified on web application if (!string.IsNullOrEmpty(site.WebApplication.OutboundMailServiceInstance.Server.Address)) { string finalFrom = site.WebApplication.OutboundMailSenderAddress; if (!string.IsNullOrEmpty(from)) finalFrom = from; //create the mail message MailMessage mail = new MailMessage(); //set the addresses mail.From = new MailAddress(finalFrom); PopulateMailAddressCollection(mail.To, to); if (!string.IsNullOrEmpty(cc)) PopulateMailAddressCollection(mail.CC, cc); if (urgent) mail.Priority = MailPriority.High; //set the content mail.Subject = subject; mail.Body = body; mail.IsBodyHtml = true; foreach (AttachmentInfo ai in attachments) { //add an attachment from stream mail.Attachments.Add(new Attachment(ai.Stream, ai.FileName)); } //send the message SmtpClient smtp = new SmtpClient(site.WebApplication.OutboundMailServiceInstance.Server.Address); smtp.Send(mail); } else { throw new InvalidOperationException("Outgoing mail settings are not specified for this web application!"); } }
/// <summary> /// gets all of the attachments for a given list item /// </summary> /// <param name="listItems"></param> /// <returns></returns> public static AttachmentInfo[] GetListItemAttachments(SPListItem listItems) { List<AttachmentInfo> myAttachments = new List<AttachmentInfo>(); for (int i = 0; i < listItems.Attachments.Count; i++) { //attachments are actualy SPFiles stored in SPFolder that is part of our list SPFile myAttachmentFile = listItems.Web.GetFile(listItems.Attachments.UrlPrefix + listItems.Attachments[i]); AttachmentInfo myAI = new AttachmentInfo(myAttachmentFile.OpenBinaryStream(), myAttachmentFile.Name); myAttachments.Add(myAI); } return myAttachments.ToArray(); }