コード例 #1
0
        public static int SendEmailRequest(string url, string from, string to, string cc, string bcc,
                                           int activityId, int tenantId, int userId, string consumerKey, string producerKey,
                                           bool storeContent, EmailType type)
        {
            from = EmailFunctions.Normalize(from);
            to   = EmailFunctions.Normalize(to);
            cc   = EmailFunctions.Normalize(cc);
            bcc  = EmailFunctions.Normalize(bcc);

            if (to.IsEmpty() && cc.IsEmpty() && bcc.IsEmpty())
            {
                throw new InvalidOperationException("Error creating email. There must be at least one recipient.");
            }

            try {
                int emailId = 0;

                url = url
                      + (url.Contains("?") ? "&" : "?")
                      + "_From=" + HttpUtility.UrlEncode(from)
                      + "&_To=" + HttpUtility.UrlEncode(to)
                      + "&_CC=" + HttpUtility.UrlEncode(cc)
                      + "&_BCC=" + HttpUtility.UrlEncode(bcc)
                      + "&_ActivityId=" + activityId
                      + "&_TenantId=" + tenantId
                      + "&_UserId=" + userId
                      + "&_ProducerKey=" + producerKey
                      + "&_IsEmail=true"
                      + "&_StoreContent=" + ((storeContent || (type == EmailType.TestDownloadEmail)) ? "true" : "false")
                      + ((type == EmailType.TestSendEmail || type == EmailType.TestDownloadEmail) ? "&_IsTestEmail=true" : "");

                string baseUrl;
                EmailHelper.QueryParameter[] parameters;
                EmailHelper.SplitUrl(url, out baseUrl, out parameters);

                string content;
                string encoding;
                var    authHeader = SecurityTokenManager.GenerateAuthorizationHeader(RuntimeSettingsProvider.Instance, consumerKey, producerKey, userId, tenantId);
                var    headers    = new Dictionary <HttpRequestHeader, string>();
                headers.Add(HttpRequestHeader.Authorization, authHeader);
                EmailHelper.HttpPost(baseUrl, parameters, headers, "", null, out content, out encoding);

                var exceptionMatch = exceptionExp.Match(content);
                if (exceptionMatch != Match.Empty)
                {
                    string message    = HttpUtility.HtmlDecode(exceptionMatch.Groups[1].Value);
                    string stacktrace = HttpUtility.HtmlDecode(exceptionMatch.Groups[2].Value);
                    throw new EmailException("Error creating Email. " + message, stacktrace);
                }
                else
                {
                    var emailMatch = emailIdExp.Match(content);
                    if (emailMatch != Match.Empty)
                    {
                        emailId = Convert.ToInt32(emailMatch.Groups[1].Value);
                    }
                    else
                    {
                        throw new EmailException("Error creating Email. No EmailId was returned.");
                    }
                }

                if (type != EmailType.TestDownloadEmail)
                {
                    if (RuntimePlatformSettings.Sandbox.SandboxOperating.GetValue())
                    {
                        using (Transaction statusTransaction = DatabaseAccess.ForCurrentDatabase.GetCommitableTransaction()) {
                            DBRuntimePlatform.Instance.CreateEmailStatus(statusTransaction, emailId);
                            SendEmailNotifier.Current.NotifiyNewEmail();
                            statusTransaction.Commit();
                        }
                    }
                    else
                    {
                        using (Transaction trans = DatabaseAccess.ForRuntimeDatabase.GetRequestTransaction()) {
                            DBRuntimePlatform.Instance.CreateEmailStatus(trans, emailId);
                        }
                    }
                }
                return(emailId);
            } catch (EmailException) {
                throw;
            } catch (Exception e) {
                throw new EmailException("Error creating Email.", e);
            }
        }