0

I wrote the following and was running perfectly. But after updates stops running, I have already setup a mail server I had not any problem.

using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SportsStore.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Security.Claims;
using System.Threading.Tasks;
namespace SportsStore.Helper
{
public class Email: IEmail
{
    private SmtpClient Client;
    private string MessageBody, Subject, UserEmail;
    private MailAddress MailFrom, MailTo;
    private MailMessage Message;        

    public Email(IServiceProvider service)
    {
        this.Client = new SmtpClient("localhost");
        this.Client.Port = 25;
        this.Client.UseDefaultCredentials = false;
        this.Client.Credentials = new NetworkCredential("postmaster", "xxxxxxx!");
        this.MailFrom = new MailAddress("xxxxx.xxxxx@yandex.com");
        this.UserEmail = service.GetRequiredService<IHttpContextAccessor>().HttpContext.User.Identity.Name;
        this.MailTo = new MailAddress(UserEmail);
    }

    public void SetMessageBody(string orderName)
    {           
       MessageBody = String.Format("The order with id {0} has been placed and is ready to ship", orderName);
    }

    public string GetMessageBody()
    {           
        return MessageBody;
    }

    public void SetSubject()
    {
        Subject = "Order Details";
    }

    public string GetSubject()
    {
        return Subject;
    }

    public bool SendMessageAsync()
    {
        this.Message = new MailMessage(this.MailFrom.ToString(), this.MailTo.ToString(), this.GetSubject(), this.GetMessageBody());

        if (this.Client.SendMailAsync(this.Message).IsCompletedSuccessfully)
        {
            return true;
        }
        return false;
    }
}

}

The Client.SendEmailAsync() now return false. Any suggestion?Please give a quick answer or I have to jump to nuget packages for a package to install.

1 Answers1

0

The method SendMailAsync is Async in your Send Mail in your application.

What are best practices for using SmtpClient, SendAsync and Dispose under .NET 4.0

L0uis
  • 703
  • 5
  • 8