Wednesday, July 8, 2020

Send Email Async most used

using NOBOLO.Library.Validations;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web.Configuration;
using System.Web.Hosting;

namespace NOBOLO.Library.Utilities
{
    public static class EmailHelper
    {
        public static string SendEmail(string To, string Subject, string BodyText)
        {

            if (FValidations.IsValidEmail(To))
            {
                MailMessage mailMessage = new MailMessage(ConfigurationManager.AppSettings["ADMIN_EMAIL"].ToString(), To, Subject, BodyText);
                MailAddress Reply = new MailAddress(ConfigurationManager.AppSettings["ADMIN_EMAIL"].ToString(), "relay", System.Text.Encoding.UTF8);
#pragma warning disable CS0618 // 'MailMessage.ReplyTo' is obsolete: 'ReplyTo is obsoleted for this type.  Please use ReplyToList instead which can accept multiple addresses. http://go.microsoft.com/fwlink/?linkid=14202'
                mailMessage.ReplyTo = Reply;
#pragma warning restore CS0618 // 'MailMessage.ReplyTo' is obsolete: 'ReplyTo is obsoleted for this type.  Please use ReplyToList instead which can accept multiple addresses. http://go.microsoft.com/fwlink/?linkid=14202'


                SmtpClient smtp = new SmtpClient();
                try
                {

                    mailMessage.IsBodyHtml = true;


                    smtp.Send(mailMessage);

                    return "Success";
                }
                catch (Exception ex)
                {
                    LogHelper.CreateLog(ex, ErrorType.Exception);
                    return "Error";
                }
                finally
                {
                    mailMessage.Dispose();
                    smtp = null;
                }
            }
            else
            {
                return "Invalid EmailAddress";
            }

        }

        public static string SendEmailAttachment(string To, string Subject, string BodyText, string FileURL)
        {

            if (FValidations.IsValidEmail(To))
            {
                MailMessage mailMessage = new MailMessage(ConfigurationManager.AppSettings["ADMIN_EMAIL"].ToString(), To, Subject, BodyText);
                MailAddress Reply = new MailAddress(ConfigurationManager.AppSettings["ADMIN_EMAIL"].ToString(), "relay", System.Text.Encoding.UTF8);
#pragma warning disable CS0618 // 'MailMessage.ReplyTo' is obsolete: 'ReplyTo is obsoleted for this type.  Please use ReplyToList instead which can accept multiple addresses. http://go.microsoft.com/fwlink/?linkid=14202'
                mailMessage.ReplyTo = Reply;
#pragma warning restore CS0618 // 'MailMessage.ReplyTo' is obsolete: 'ReplyTo is obsoleted for this type.  Please use ReplyToList instead which can accept multiple addresses. http://go.microsoft.com/fwlink/?linkid=14202'


                SmtpClient smtp = new SmtpClient();
                try
                {

                    mailMessage.IsBodyHtml = true;

                    System.Net.Mail.Attachment attachment;
                    attachment = new System.Net.Mail.Attachment(FileURL);
                    mailMessage.Attachments.Add(attachment);

                    smtp.Send(mailMessage);

                    return "Success";
                }
                catch (Exception ex)
                {
                    LogHelper.CreateLog(ex, ErrorType.Exception);
                    return "Error";
                }
                finally
                {
                    mailMessage.Dispose();
                    smtp = null;
                }
            }
            else
            {
                return "Invalid EmailAddress";
            }

        }

        public static async Task<string> SendEmailAsync(string To, string Subject, string BodyText)
        {
            return await Task.Factory.StartNew(() =>
            {
                if (FValidations.IsValidEmail(To))
                {
                    MailMessage mailMessage = new MailMessage(ConfigurationManager.AppSettings["ADMIN_EMAIL"].ToString(), To, Subject, BodyText);
                    MailAddress Reply = new MailAddress(ConfigurationManager.AppSettings["ADMIN_EMAIL"].ToString(), "relay", System.Text.Encoding.UTF8);
#pragma warning disable CS0618 // 'MailMessage.ReplyTo' is obsolete: 'ReplyTo is obsoleted for this type.  Please use ReplyToList instead which can accept multiple addresses. http://go.microsoft.com/fwlink/?linkid=14202'
                    mailMessage.ReplyTo = Reply;
#pragma warning restore CS0618 // 'MailMessage.ReplyTo' is obsolete: 'ReplyTo is obsoleted for this type.  Please use ReplyToList instead which can accept multiple addresses. http://go.microsoft.com/fwlink/?linkid=14202'


                    SmtpClient smtp = new SmtpClient();
                    try
                    {

                        mailMessage.IsBodyHtml = true;

                        smtp.Send(mailMessage);

                        return "Success";
                    }
                    catch (Exception ex)
                    {
                        LogHelper.CreateLog(ex, ErrorType.Exception);
                        return "Error";
                    }
                    finally
                    {
                        mailMessage.Dispose();
                        smtp = null;
                    }
                }
                else
                {
                    return "Invalid EmailAddress";
                }
            });

        }


    }

    public static class StandardEmail
    {
        public static void ForgetPassword(string _ToUserName, string _URl, string _ToEmail)
        {
            string html = "<p>Hi " + _ToUserName + ",</p>";
            html += "<p><br><br>Thanks for using " + WebConfigurationManager.AppSettings["AppName"] + "!</p>";
            html += "<p><br>click on this link to change your password: " + _URl + "</p>";
            //html += "<p><br>If you would like to change your password, please go to the profile area of the app.</p>";
            html += "<p><br>Thanks,</p>";
            html += "<p><br><strong>The " + WebConfigurationManager.AppSettings["AppName"] + " Team</strong></p>";

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
            EmailHelper.SendEmailAsync(_ToEmail, WebConfigurationManager.AppSettings["AppName"] + " : Forgot Password", html);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

        }

        public static void PaymentComplete(string _ToUserName, string Dollars, string _ToEmail)
        {
            string html = "<p>Hi " + _ToUserName + ",</p>";
            html += "<p><br><br>Thanks for using " + WebConfigurationManager.AppSettings["AppName"] + "!</p>";
            html += "<p><br>your payment has been successfully completed of $"+Dollars+"</p>";
            html += "<p><br>Soon you will get your photos in your account </p>";
            //html += "<p><br>If you would like to change your password, please go to the profile area of the app.</p>";
            html += "<p><br>Thanks,</p>";
            html += "<p><br><strong>The " + WebConfigurationManager.AppSettings["AppName"] + " Team</strong></p>";

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
            EmailHelper.SendEmailAsync(_ToEmail, WebConfigurationManager.AppSettings["AppName"] + " : Forgot Password", html);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

        }

        public static void ChangePassword(string _ToUserName, string _Password, string _ToEmail,long _UserID)
        {
            string URL = "http://staging3.sdsol.com/nobolo/Admin/Login/ChangePassword?UserID=" + _UserID;
            string html = "<p>Hi " + _ToUserName + ",</p>";
            html += "<p><br><br>Thanks for using " + WebConfigurationManager.AppSettings["AppName"] + "!</p>";
            html += "<p><br>Please go to this link to change your password: " + URL + "</p>";
            html += "<p><br>Thanks,</p>";
            html += "<p><br><strong>The " + WebConfigurationManager.AppSettings["AppName"] + " Team</strong></p>";

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
            EmailHelper.SendEmailAsync(_ToEmail, WebConfigurationManager.AppSettings["AppName"] + " : Change Password", html);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

        }

        public static void ContactUs(string _ToEmail, string _subject, string _body, string _SendInfor)
        {
            string html = "<p>Hi " + WebConfigurationManager.AppSettings["AppName"] + " Team,</p>";
            html += "<p><br><br>Feedback!</p>";
            html += "<p><br>" + _body + "</p>";
            html += "<p><br>Thanks,</p>";
            html += "<p><br><strong>Sender: " + _SendInfor + "</strong></p>";

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
            EmailHelper.SendEmailAsync(_ToEmail, "Contact Us :" + _subject, html);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

        }

        public static void PasswordUpdate(string _ToUserName, string _Password, string _ToEmail)
        {

            string html = "<p>Hi " + _ToUserName + ",</p>";
            html += "<p><br>Your password has been changed successfully.</p>";
            html += "<p><br>Thanks,</p>";
            html += "<p><br><strong>The " + WebConfigurationManager.AppSettings["AppName"] + " Team</strong></p>";

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
            EmailHelper.SendEmailAsync(_ToEmail, WebConfigurationManager.AppSettings["AppName"] + " : Password Changed", html);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

        }

        public static void ForgetPassword_Admin(string _ToUserName, string _Password, string _ToEmail)
        {
            string html = "<p>Hi " + _ToUserName + "</p>";
            html += "<p>Here is the password : " + _Password + "</p>";
            html += "<p><br><br>Thanks for using " + WebConfigurationManager.AppSettings["AppName"] + "<br><strong>" + WebConfigurationManager.AppSettings["AppName"] + " team</strong></p>";


#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
            EmailHelper.SendEmailAsync(_ToEmail, WebConfigurationManager.AppSettings["AppName"] + " : Forgot Password", html);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

        }

        public static void StoreOrderConfirmation(string _ToUserName, string _OrderNo, string _ToEmail, string _MoreHtml = "")
        {
            string html = "<p>Hi " + _ToUserName + ",</p>";
            html += "<p><br><br>Thanks for using " + WebConfigurationManager.AppSettings["AppName"] + "!</p>";
            html += "<p><br>Your Order No is: " + _OrderNo + "</p>";
            html += "<p><br>" + _MoreHtml + "</p>";
            html += "<p><br>Thanks,</p>";
            html += "<p><br><strong>The " + WebConfigurationManager.AppSettings["AppName"] + " Team</strong></p>";

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
            EmailHelper.SendEmailAsync(_ToEmail, WebConfigurationManager.AppSettings["AppName"] + " : Order Confirmation #" + _OrderNo, html);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

        }

        public static void InviteFrindEmail(string _ToUserName, string _FromUserName, string _ToEmail, string _MoreHtml = "")
        {
            string html = "<p>Hi " + _ToUserName + ",</p>";
            //html += "<p><br><br>Thanks for using " + WebConfigurationManager.AppSettings["AppName"] + "!</p>";
            //html += "<p><br>User : " + _FromUserName + " has send you the invitatino</p>";
            html += "<p><br>" + _MoreHtml + "</p>";
            html += "<p><br>Thanks,</p>";
            html += "<p><br><strong>The " + WebConfigurationManager.AppSettings["AppName"] + " Team</strong></p>";

            EmailHelper.SendEmailAsync(_ToEmail, WebConfigurationManager.AppSettings["AppName"] + " : Invitation to join Humminz", html);

        }


    }

}

No comments:

Post a Comment

Two Factor Authentication using .Net Core

Install Package dotnet add package GoogleAuthenticator --version 3.1.1 Model Changes public bool IsAuthenticatorReset { get; set; } public s...