Beispiel #1
0
        /// <summary>
        /// Attachment should be with ABSOLUTE PATH (with AppPath())
        /// </summary
        public void SendEmail(NovellGroupWiseMail mail)
        {
            WebReference.Mail             mailToSend = new WebReference.Mail();
            WebReference.MessagePart[]    part;
            WebReference.NameAndEmail     item;
            WebReference.Recipient[]      recips;
            WebReference.sendItemRequest  req;
            WebReference.sendItemResponse resp;
            mailToSend.subject = mail.Subject;

            var att = new WebReference.AttachmentItemInfo();

            List <AttachmentItemInfo> atts = new List <AttachmentItemInfo>();



            //сохранить html text в файл text.htm
            if (mail.HtmlText != null)
            {
                String path = HttpContext.Current.Server.MapPath("~/TemporaryFiles/") + Guid.NewGuid() + ".htm";
                var    fs   = new FileStream(path, FileMode.Create);
                using (var bw = new BinaryWriter(fs))
                {
                    bw.Write(mail.HtmlText);
                }
                fs.Close();

                var fss = new FileStream(path, FileMode.OpenOrCreate);
                using (var binaryReader = new BinaryReader(fss))
                {
                    att.data = binaryReader.ReadBytes((int)fss.Length);
                }
                fss.Close();
                att.name = "text.htm";
                atts.Add(att);
            }


            if (mail.Attachments != null)
            {
                foreach (var attachment in mail.Attachments)
                {
                    var fs = new FileStream(attachment, FileMode.OpenOrCreate);
                    using (var binaryReader = new BinaryReader(fs))
                    {
                        att.data = binaryReader.ReadBytes((int)fs.Length);
                    }
                    att.name = System.IO.Path.GetFileName(attachment);
                    atts.Add(att);
                    fs.Close();
                }
            }

            mailToSend.attachments   = atts.ToArray();
            mailToSend.hasAttachment = true;

            //if (0 != mail.Text.Length)
            //{
            //    part = new WebReference.MessagePart[1];
            //    part[0] = new WebReference.MessagePart();
            //    part[0].Value = System.Text.UTF8Encoding.UTF8.GetBytes(mail.Text);
            //    mailToSend.message = part;
            //}

            recips = new WebReference.Recipient[mail.Recipients.Count];
            for (int i = 0; i < mail.Recipients.Count; i++)
            {
                recips[i]             = new WebReference.Recipient();
                recips[i].displayName = mail.Recipients[i].Name;
                recips[i].email       = mail.Recipients[i].Email;
                recips[i].uuid        = mail.Recipients[i].Id;
            }

            mailToSend.distribution            = new WebReference.Distribution();
            mailToSend.distribution.recipients = recips;

            req      = new WebReference.sendItemRequest();
            req.item = mailToSend;

            resp = ws.sendItemRequest(req);
        }