messaging the provider in Asp.net

Behnam akbary 0 Reputation points
2025-02-25T10:58:26.57+00:00

i have a task to write a code that can message X number of provider for example via SMS or Email.

and it has to be by using "TryAddTransient".

Any solutions for that?

maybe it seems simple but i just started to learn Asp.

ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
387 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. XuDong Peng-MSFT 11,336 Reputation points Microsoft Vendor
    2025-02-26T06:49:00.3933333+00:00

    Hi @,

    and it has to be by using "TryAddTransient". Any solutions for that?

    It looks like you want to create and manage your service for sending emails or SMS. In this case, you can refer to the following simple example (send email):

    IEmailService.cs 
    
    public interface IEmailService
    {
        Task SendEmailAsync(string to, string subject, string body);
    }
    public class EmailService : IEmailService
    {
        private readonly SmtpClient _smtpClient;
        private readonly string _fromEmail;
        public EmailService(string smtpHost, int smtpPort, string fromEmail, string fromPassword)
        {
            _smtpClient = new SmtpClient(smtpHost, smtpPort)
            {
                Credentials = new NetworkCredential(fromEmail, fromPassword),
                EnableSsl = true
            };
            _fromEmail = fromEmail;
        }
        public async Task SendEmailAsync(string to, string subject, string body)
        {
            var mailMessage = new MailMessage(_fromEmail, to)
            {
                Subject = subject,
                Body = body,
                IsBodyHtml = true
            };
            await _smtpClient.SendMailAsync(mailMessage);
            Console.WriteLine($"Email sent to {to}: {subject}");
        }
    }
    

    Test with gmail below:

    static async Task Main(string[] args)
    {
        // Configuring Dependency Injection
        var services = new ServiceCollection();
    
        // Register for email service
        services.TryAddTransient<IEmailService>(provider =>
        {
            // Configuring SMTP Server Information
            string smtpHost = "smtp.gmail.com"; // SMTP server address
            int smtpPort = 587; // SMTP port
            string fromEmail = "******@gmail.com"; // sender email
            string fromPassword = "your app password";         
    		return new EmailService(smtpHost, smtpPort, fromEmail,fromPassword);
        });
    
        var serviceProvider = services.BuildServiceProvider();
    
        // Parsing mail service
        var emailService = serviceProvider.GetRequiredService<IEmailService>();
    
        // send email
        string to = "recipient@example.com"; // Recipient's email
        string subject = "Test Email";
        string body = "<h1>Hello, this is a test email!</h1>";
    
        try
        {
            await emailService.SendEmailAsync(to, subject, body);
            Console.WriteLine("Email sent successfully!");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Failed to send email: {ex.Message}");
        }
    }
    
    

    Best regards,

    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.