Exemplo n.º 1
0
        /// <summary>
        ///  Sends out an email from the application. Returns true if successful.
        /// </summary>
        public static bool SendGridEmail(string from, List<string> to, List<string> cc, List<string> bcc, string subj, string body, string smtpUser, string smtpUserPwd)
        {
            try
            {
                //******************** CONSTRUCT EMAIL ********************************************
                // Create the email object first, then add the properties.
                var myMessage = new SendGridMessage();

                // Add message properties.
                myMessage.From = new MailAddress(from);
                myMessage.AddTo(to);
                if (cc != null)
                {
                    foreach (string cc1 in cc)
                        myMessage.AddCc(cc1);
                }
                if (bcc != null)
                {
                    foreach (string bcc1 in bcc)
                        myMessage.AddBcc(bcc1);
                }

                myMessage.Subject = subj;
                //myMessage.Html = "<p>" + body + "</p>";
                myMessage.Text = body;
                //*********************************************************************************

                //********************* SEND EMAIL ************************************************
                var credentials = new NetworkCredential(smtpUser, smtpUserPwd);
                // Create an Web transport for sending email.
                var transportWeb = new Web(credentials);

                // Send the email.
                transportWeb.Deliver(myMessage);

                return true;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    db_Ref.InsertT_OE_SYS_LOG("EMAIL ERR", ex.InnerException.ToString());
                else if (ex.Message != null)
                    db_Ref.InsertT_OE_SYS_LOG("EMAIL ERR", ex.Message.ToString());
                else
                    db_Ref.InsertT_OE_SYS_LOG("EMAIL ERR", "Unknown error");

                return false;

            }
        }
Exemplo n.º 2
0
        public void TestFetchFormParamsHelper(Web webApi, bool credentials)
        {
            // Test Variables
            const string toAddress = "*****@*****.**";
            const string ccAddress = "*****@*****.**";
            const string bcc1Address = "*****@*****.**";
            const string bcc2Address = "*****@*****.**";
            MailAddress[] bccAddresses = {new MailAddress(bcc1Address), new MailAddress(bcc2Address)};
            const string fromAddress = "*****@*****.**";
            const string subject = "Test Subject";
            const string textBody = "Test Text Body";
            const string htmlBody = "<p>Test HTML Body</p>";
            const string headerKey = "headerkey";
            var testHeader = new Dictionary<string, string> { { headerKey, "headervalue" } };
            const string categoryName = "Example Category";

            var message = new SendGridMessage();
            message.AddTo(toAddress);
            message.AddCc(ccAddress);
            message.Bcc = bccAddresses;
            message.From = new MailAddress(fromAddress);
            message.Subject = subject;
            message.Text = textBody;
            message.Html = htmlBody;
            message.AddHeaders(testHeader);
            message.Header.SetCategory(categoryName);

            var result = webApi.FetchFormParams(message);
            if (credentials)
            {
                Assert.True(result.Any(r => r.Key == "api_user" && r.Value == TestUsername));
                Assert.True(result.Any(r => r.Key == "api_key" && r.Value == TestPassword));
            }
            Assert.True(result.Any(r => r.Key == "to[]" && r.Value == toAddress));
            Assert.True(result.Any(r => r.Key == "cc[]" && r.Value == ccAddress));
            Assert.True(result.Any(r => r.Key == "bcc[]" && r.Value == bcc1Address));
            Assert.True(result.Any(r => r.Key == "bcc[]" && r.Value == bcc2Address));
            Assert.True(result.Any(r => r.Key == "from" && r.Value == fromAddress));
            Assert.True(result.Any(r => r.Key == "subject" && r.Value == subject));
            Assert.True(result.Any(r => r.Key == "text" && r.Value == textBody));
            Assert.True(result.Any(r => r.Key == "html" && r.Value == htmlBody));
            Assert.True(
                result.Any(
                    r => r.Key == "headers" && r.Value == String.Format("{{\"{0}\":\"{1}\"}}", headerKey, testHeader[headerKey])));
            Assert.True(
                result.Any(r => r.Key == "x-smtpapi" && r.Value == String.Format("{{\"category\" : \"{0}\"}}", categoryName)));
        }
Exemplo n.º 3
0
        // Use NuGet to install SendGrid (Basic C# client lib) 
        private void configSendGridasync()
        {
            string filename = System.Web.HttpContext.Current.Server.MapPath(@"~/Content/Reports/template.htm");// "~/Content/Reports/template.htm";
            
            string mailboy = System.IO.File.ReadAllText(filename);
            filename = System.Web.HttpContext.Current.Server.MapPath(@"~/Content/Reports/emailTemplate.htm");// "~/Content/Reports/template.htm";

            string emailTemplate = System.IO.File.ReadAllText(filename);

            var myMessage = new SendGridMessage();
            myMessage.AddTo("*****@*****.**");
            myMessage.AddCc("*****@*****.**");
            myMessage.From = new System.Net.Mail.MailAddress(
                                "*****@*****.**", "Guimarães Mahota");
            myMessage.Subject ="teste sendgrid";
            myMessage.Text = mailboy;
            myMessage.Html = mailboy;

            myMessage.EnableTemplate(emailTemplate);

            var credentials = new NetworkCredential(
                       ConfigurationManager.AppSettings["mailAccount"],
                       ConfigurationManager.AppSettings["mailPassword"]
                       );

            // Create a Web transport for sending email.
            var transportWeb = new Web(credentials);

            // Send the email.
            if (transportWeb != null)
            {
                 transportWeb.DeliverAsync(myMessage);
            }
            else
            {
                Trace.TraceError("Failed to create Web transport.");
                
            }
        }
Exemplo n.º 4
0
        /**
         * This method will prepare the message by putting in information from the form.
         * and will send the message to a method SendAsync to check the credential of Sendgrid.
         * @param string to, string from, string fromName, string companyName, string phoneNum, string longMessage
         * @return void
         */
        private static void SendEmail(string to, string from, string fromName, string companyName, string phoneNum, string longMessage)
        {
            // Create the email object first, then add the properties.
            var myMessage = new SendGrid.SendGridMessage();

            myMessage.AddTo(to);
            myMessage.AddCc(from);
            myMessage.From    = new MailAddress(from, fromName);
            myMessage.Subject = "Email from ASP.NET Portfolio site";
            myMessage.Text    = " Message Detail \n" +
                                "From: " + fromName + "(" + from + ") \n" +
                                "Company: " + companyName + "\n" +
                                "Contact Number: " + phoneNum + "\n" +
                                "Message: " + longMessage;

            /* Example from sendgrid
             * var subs = new List<String> { "私は%type%ラーメンが大好き" };
             * myMessage.AddSubstitution("%tag%", subs);
             * myMessage.AddSection("%type%", "とんこつ");
             */

            SendAsync(myMessage);
        }
Exemplo n.º 5
0
        private static void Send(EmailData model)
        {
            string To = model.To;
            string From = model.FromEmail;
            string Name = model.Name;
            string Subject = model.Subject;
            string Message = model.Message;
            string CC = model.CC;
            string BCC = model.BCC; //for later implementation

            var message = new SendGridMessage();

            if (CC != null)
            {
                message.AddCc(CC);
            }

            message.AddTo(To);

            message.From = new MailAddress(From, Name); //ensure name is optional?
            message.Subject = Subject;
            message.Text = Message;

            var credentials = new NetworkCredential(_sendGridEmailUserName, _sendGridEmailPassword);

                var transportWeb = new SendGrid.Web(credentials);

            try
            {
                transportWeb.DeliverAsync(message);
            }
            catch (Exception)
            {
                throw;
            }
        }