示例#1
0
        public void CreateEmail(string subject, string body, EmailBodyFormat bodyFormat,
                                IResourceList recipients, string[] attachments, bool addSignature)
        {
            EmailRecipient[] emailRecipients = null;
            if (recipients != null)
            {
                emailRecipients = new EmailRecipient [recipients.Count];
                for (int i = 0; i < recipients.Count; i++)
                {
                    IResource res = recipients [i];
                    if (res.Type != "Contact" && res.Type != "EmailAccount")
                    {
                        throw new ArgumentException("Invalid recipient resource type");
                    }
                    emailRecipients [i].Name = res.DisplayName;
                    if (res.Type == "Contact")
                    {
                        IContact contact = Core.ContactManager.GetContact(res);
                        emailRecipients [i].EmailAddress = contact.DefaultEmailAddress;
                    }
                    else
                    {
                        emailRecipients [i].EmailAddress = BuildAddressWithType(res.GetStringProp("EmailAddress"));
                    }
                }
            }

            CreateEmail(subject, body, bodyFormat, emailRecipients, attachments, addSignature);
        }
示例#2
0
        public void CreateEmail(string subject, string body, EmailBodyFormat bodyFormat, IResourceList recipients, string[] attachments,
                                bool useTemplatesInBody)
        {
            if (recipients != null && recipients.Count > 0)
            {
                foreach (IResource resource in recipients)
                {
                    if (resource.Type != "Contact" && resource.Type != "ContactName" && resource.Type != STR.EmailAccount)
                    {
                        throw new ApplicationException("CreateEmail -- wrong invokation with a recipient of inappropriate type [" + resource.Type + "]");
                    }
                }
            }
            CreateEmailDelegate emailDelegate = OutlookFacadeHelper.CreateNewMessage;

            try
            {
                _outlookProcessor.RunJob("Creation new mail", emailDelegate,
                                         subject, body, bodyFormat, recipients, attachments, useTemplatesInBody);
            }
            catch (OutlookThreadTimeoutException ex)
            {
                _tracer.TraceException(ex);
                _outlookProcessor.QueueJob(JobPriority.AboveNormal, "Creation new mail", emailDelegate,
                                           subject, body, bodyFormat, recipients, attachments, useTemplatesInBody);
            }
        }
示例#3
0
        public override bool CreateNewMessage(string subject, string body, EmailBodyFormat bodyFormat, EmailRecipient[] recipients,
                                              string[] attachments, bool useTemplatesInBody)
        {
            ArrayList recipientsArray = null;

            if (recipients != null && recipients.Length > 0)
            {
                recipientsArray = GetRecipientsArray(recipients);
            }
            return(CreateNewMessage(subject, body, bodyFormat, recipientsArray, attachments, useTemplatesInBody, null));
        }
示例#4
0
        public void CreateEmail(string subject, string body, EmailBodyFormat bodyFormat, EmailRecipient[] recipients, string[] attachments, bool addSignature)
        {
            CreateEmailWithRecipDelegate emailDelegate = OutlookFacadeHelper.CreateNewMessage;

            try
            {
                _outlookProcessor.RunUniqueJob("Creation new mail", emailDelegate, subject, body, bodyFormat, recipients, attachments, addSignature);
            }
            catch (OutlookThreadTimeoutException ex)
            {
                _tracer.TraceException(ex);
                _outlookProcessor.QueueJob(JobPriority.AboveNormal, "Creation new mail", emailDelegate, subject, body, bodyFormat, recipients, attachments, addSignature);
            }
        }
示例#5
0
        public override bool CreateNewMessage(string subject, string body, EmailBodyFormat bodyFormat, IResourceList recipients,
                                              string[] attachments, bool useTemplatesInBody)
        {
            ArrayList recipList  = GetRecipientsArray(recipients);
            ArrayList attachList = new ArrayList();

            if (attachments != null)
            {
                foreach (string path in attachments)
                {
                    attachList.Add(new AttachInfo(path, Path.GetFileName(path)));
                }
            }
            return(CreateNewMessage(subject, body, bodyFormat, recipList, attachList, useTemplatesInBody));
        }
        public Task SendAsync(string subject, string body, EmailBodyFormat bodyFormat, IEnumerable<EmailRecipient> recipients)
        {
            recipients.ThrowIfNull("recipients");

            recipients = recipients.ToArray();

            if (!recipients.Any())
            {
                throw new ArgumentException("Must provide at least one to recipient.", "recipients");
            }

            var client = new SmtpClient(_configuration.SmtpHost, _configuration.SmtpPort)
            {
                Timeout = (int)_configuration.Timeout.TotalMilliseconds,
                EnableSsl = _configuration.UseSsl,
                Credentials = _configuration.Credentials
            };
            var message = new MailMessage
            {
                From = new MailAddress(_configuration.FromAddress, _configuration.FromAddressDisplayName)
            };

            foreach (EmailRecipient recipient in recipients)
            {
                switch (recipient.Type)
                {
                    case EmailRecipientType.To:
                        message.To.Add(new MailAddress(recipient.EmailAddress));
                        break;
                    case EmailRecipientType.CC:
                        message.CC.Add(new MailAddress(recipient.EmailAddress));
                        break;
                    case EmailRecipientType.Bcc:
                        message.Bcc.Add(new MailAddress(recipient.EmailAddress));
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }

            message.Subject = subject ?? "";
            message.Body = body ?? "";
            message.IsBodyHtml = bodyFormat == EmailBodyFormat.Html;

            return client.SendMailAsync(message);
        }
示例#7
0
        private bool CreateNewMessage(string subject, string body, EmailBodyFormat bodyFormat, ArrayList recipients,
                                      ArrayList attachments, bool useTemplatesInBody)
        {
            try
            {
                MailBodyFormat mailBodyFormat = MailBodyFormat.HTML;
                if (bodyFormat == EmailBodyFormat.PlainText)
                {
                    if (useTemplatesInBody && Settings.UseSignature)
                    {
                        body += "\r\n";
                        body += Settings.Signature;
                    }

                    mailBodyFormat = MailBodyFormat.PlainText;
                }
                else if (bodyFormat == EmailBodyFormat.Html)
                {
                    body = _rxHtmlComment.Replace(body, "");
                    body = "<HTML><BODY>" + body;
                    if (useTemplatesInBody && Settings.UseSignature)
                    {
                        body += "\r\n";
                        body += Settings.Signature;
                    }
                    body += "</BODY></HTML>";
                    Trace.WriteLine("HTML body for Outlook: \r\n" + body);
                }

                IEMsgStore msgStore = OutlookSession.GetDefaultMsgStore();
                if (msgStore != null)
                {
                    return(msgStore.CreateNewMessage(subject, body, mailBodyFormat, recipients,
                                                     attachments, OutlookSession.GetOutlookDefaultEncodingOut()));
                }
                else
                {
                    throw new ApplicationException("There are no default message store for outlook");
                }
            }
            catch (Exception exception)
            {
                ReportProblem(exception);
            }
            return(false);
        }
示例#8
0
        //用指定的信箱发信,所以不指定From
        public void SendMail(string mailTo, string subject, string body, EmailBodyFormat Format,params Attachment[] attachments)
        {

            SmtpClient mail = new SmtpClient();
            //实例
            mail.Host =ConfigurationManager.AppSettings["smtp"];
            //发信主机
            //mail.Credentials.GetCredential("smtp.163.com", 25, "Network"); //发信认证主机及端口
            mail.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["mailFrom"], 
                ConfigurationManager.AppSettings["mailFromPWD"]);

            //发件人
            MailMessage msg = new MailMessage(ConfigurationManager.AppSettings["mailFrom"], mailTo, subject, body);
            
            foreach (var item in attachments)
	        {
                if(item!=null)
                    msg.Attachments.Add(item);		 
	        }
            
            if(ConfigurationManager.AppSettings["mailToCC"]!="")
                msg.CC.Add(ConfigurationManager.AppSettings["mailToCC"]);

            if (ConfigurationManager.AppSettings["mailToBcc"] != "")
                msg.Bcc.Add(ConfigurationManager.AppSettings["mailToBcc"]);

            if (Format == EmailBodyFormat.HTML)
                msg.IsBodyHtml = true;
            else
                msg.IsBodyHtml = false;

            try
            {
                mail.Send(msg);

            }
            catch (Exception)
            {

                throw;
            }
        }
示例#9
0
        public override bool CreateNewMessage(string subject, string body, EmailBodyFormat bodyFormat, IResourceList recipients,
                                              string[] attachments, bool useTemplatesInBody)
        {
            ArrayList recipientsArray = null;
            ArrayList categories      = new ArrayList(  );

            if (recipients != null && recipients.Count > 0)
            {
                recipientsArray = GetRecipientsArray(recipients);
                foreach (IResource recipient in recipients)
                {
                    if (recipient.Type == "Contact")
                    {
                        IResourceList resCategories = Core.CategoryManager.GetResourceCategories(recipient);
                        ExportCategories.LoadCategoriesArrayList(resCategories, categories);
                    }
                }
            }
            return(CreateNewMessage(subject, body, bodyFormat, recipientsArray, attachments, useTemplatesInBody, categories));
        }
示例#10
0
        //用指定的信箱发信,收件人用[email protected],真正收件人放在BCC里面
        public void SendMailBCC(string mailTo, string mailBCC,string subject, string body, EmailBodyFormat Format, params Attachment[] attachments)
        {
            SmtpClient mail = new SmtpClient();
            //实例
            mail.Host = ConfigurationManager.AppSettings["smtp"];
            //发信主机
            mail.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["mailFrom"],
                ConfigurationManager.AppSettings["mailFromPWD"]);

            //发件人
            MailMessage msg = new MailMessage(ConfigurationManager.AppSettings["mailFrom"], mailTo, subject, body);

            foreach (var item in attachments)
            {
                if (item != null)
                    msg.Attachments.Add(item);
            }

            if (ConfigurationManager.AppSettings["mailToCC"] != "")
                msg.CC.Add(ConfigurationManager.AppSettings["mailToCC"]);

            msg.Bcc.Add(mailBCC);
            if (ConfigurationManager.AppSettings["mailToBcc"] != "")
                msg.Bcc.Add(ConfigurationManager.AppSettings["mailToBcc"]);

            if (Format == EmailBodyFormat.HTML)
                msg.IsBodyHtml = true;
            else
                msg.IsBodyHtml = false;

            try
            {
                mail.Send(msg);

            }
            catch (Exception)
            {

                throw;
            }
        }
示例#11
0
        public void CreateEmail(string subject, string body, EmailBodyFormat bodyFormat,
                                EmailRecipient[] recipients, string[] attachments, bool addSignature)
        {
            MapiMessage msg = new MapiMessage();

            msg.subject = subject;

            if (bodyFormat == EmailBodyFormat.Html)
            {
                body = body.Replace("<p>", "\r\n\r\n");
                body = body.Replace("<P>", "\r\n\r\n");
                body = body.Replace("<br>", "\r\n");
                body = body.Replace("<BR>", "\r\n");
                body = HtmlTools.StripHTML(body);
                body = HtmlTools.SafeHtmlDecode(body);
            }

            if (addSignature && Core.SettingStore.ReadBool("MailFormat", "UseSignature", false))
            {
                body += "\r\n";
                body += Core.SettingStore.ReadString("MailFormat", "Signature");
            }

            msg.noteText = body;

            if (recipients != null && recipients.Length > 0)
            {
                ArrayList recipArray = new ArrayList();
                foreach (EmailRecipient recipient in recipients)
                {
                    MapiRecipDesc recip = new MapiRecipDesc();
                    recip.recipClass = MapiTO;
                    recip.name       = recipient.Name;
                    recip.address    = BuildAddressWithType(recipient.EmailAddress);
                    recipArray.Add(recip);
                }

                msg.recips     = AllocRecips(recipArray);
                msg.recipCount = recipArray.Count;
            }

            if (attachments != null && attachments.Length > 0)
            {
                msg.files     = AllocAttachs(attachments);
                msg.fileCount = attachments.Length;
            }

            try
            {
                int rc = MAPISendMail(IntPtr.Zero, Core.MainWindow.Handle, msg, MAPI_DIALOG, 0);
                if (rc != 0 && rc != MAPI_E_USER_ABORT)
                {
                    ReportError(rc);
                }
            }
            catch (Exception)
            {
                MessageBox.Show(Core.MainWindow, "Failed to send e-mail (unknown error)", "Send E-mail", MessageBoxButtons.OK);
            }


            if (msg.recips != IntPtr.Zero)
            {
                int runptr = (int)msg.recips;
                for (int i = 0; i < msg.recipCount; i++)
                {
                    Marshal.DestroyStructure((IntPtr)runptr, typeof(MapiRecipDesc));
                    runptr += Marshal.SizeOf(typeof(MapiRecipDesc));
                }
                Marshal.FreeHGlobal(msg.recips);
            }

            if (msg.files != IntPtr.Zero)
            {
                Type ftype = typeof(MapiFileDesc);
                int  fsize = Marshal.SizeOf(ftype);

                int runptr = (int)msg.files;
                for (int i = 0; i < msg.fileCount; i++)
                {
                    Marshal.DestroyStructure((IntPtr)runptr, ftype);
                    runptr += fsize;
                }
                Marshal.FreeHGlobal(msg.files);
            }
        }
示例#12
0
 public abstract bool CreateNewMessage(string subject, string body, EmailBodyFormat bodyFormat, EmailRecipient[] recipients,
                                       string[] attachments, bool useTemplatesInBody);
 public Task SendAsync(string subject, string body, EmailBodyFormat bodyFormat, params string[] toEmailAddresses)
 {
     return SendAsync(subject, body, bodyFormat, (IEnumerable<string>)toEmailAddresses);
 }
        public Task SendAsync(string subject, string body, EmailBodyFormat bodyFormat, IEnumerable<string> toEmailAddresses)
        {
            toEmailAddresses.ThrowIfNull("toEmailAddresses");

            return SendAsync(subject, body, bodyFormat, toEmailAddresses.Select(arg => new EmailRecipient(arg, EmailRecipientType.To)));
        }
 public Task SendAsync(string subject, string body, EmailBodyFormat bodyFormat, params EmailRecipient[] recipients)
 {
     return SendAsync(subject, body, bodyFormat, (IEnumerable<EmailRecipient>)recipients);
 }
示例#16
0
        private bool CreateNewMessage(string subject, string body, EmailBodyFormat bodyFormat, ArrayList recipients,
                                      string[] attachments, bool useTemplatesInBody, ArrayList categories)
        {
            Settings.LoadSettings();
            OutlookGUIInit.StartAndInitializeOutlook();
            _com_Outlook_Application outlook = null;
            _com_OutlookMailItem     newMail = null;

            try
            {
                outlook         = new _com_Outlook_Application();
                newMail         = outlook.CreateNew();
                newMail.Subject = subject;

                bool validBody = !String.IsNullOrEmpty(body);

                if (useTemplatesInBody && Settings.UseSignature)
                {
                    body += "\r\n";
                    body += Settings.Signature;
                }

                if (validBody && EmailBodyFormat.Html == bodyFormat)
                {
                    try
                    {
                        newMail.BodyFormat = OlBodyFormat.olFormatHTML;
                    }
                    catch (Exception) {}
                    newMail.HTMLBody = body;
                }
                else
                if (validBody)
                {
                    try
                    {
                        newMail.BodyFormat = OlBodyFormat.olFormatPlain;
                    }
                    catch (Exception) {}
                    newMail.Body = body;
                }
                else
                if (!String.IsNullOrEmpty(body))
                {
                    newMail.Body = body;
                }

                if (recipients != null && recipients.Count > 0)
                {
                    OutlookSession.EMAPISession.AddRecipients(newMail.MAPIOBJECT, recipients);
                    if (Settings.SetCategoryFromContactWhenEmailSent && categories != null)
                    {
                        newMail.AddCategories(categories);
                    }
                }
                if (attachments != null && attachments.Length > 0)
                {
                    newMail.AddAttachments(attachments);
                }
                newMail.Display(false);
                return(true);
            }
            catch (Exception exception)
            {
                ReportProblem(exception);
            }
            finally
            {
                COM_Object.ReleaseIfNotNull(newMail);
                COM_Object.ReleaseIfNotNull(outlook);
            }
            return(false);
        }
示例#17
0
 static public void CreateNewMessage(string subject, string body, EmailBodyFormat bodyFormat, EmailRecipient[] recipients,
                                     string[] attachments, bool useTemplatesInBody)
 {
     FormHelper.CreateNewMessage(subject, body, bodyFormat, recipients, attachments, useTemplatesInBody);
 }