# Implementing Send Mail Message

This tutorial elaborates on how to implement the `SendMailMessage` method required for [sending email messages](https://www.telerik.com/products/reporting/documentation/embedding-reports/display-reports-in-applications/web-application/send-mail-message) through report viewers that are connected to a Reporting REST Service.

### Controllers Setup

When the Reporting REST Service has been implemented using controllers, [override](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/override) the `SendMailMessage` method of the [ReportsController](https://www.telerik.com/products/reporting/documentation/api/telerik.reporting.services.webapi.reportscontrollerbase).

```C#
protected override HttpStatusCode SendMailMessage(MailMessage mailMessage)
{
    using (var smtpClient = new SmtpClient("smtp.companyname.com", 25))
    {
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpClient.EnableSsl = true;
        smtpClient.Send(mailMessage);
    }

    return HttpStatusCode.OK;
}
```

```VB.NET
Protected Overrides Function SendMailMessage(ByVal mailMessage As MailMessage) As HttpStatusCode
    Using smtpClient = New SmtpClient("smtp.companyname.com", 25)
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network
        smtpClient.EnableSsl = True
        smtpClient.Send(mailMessage)
    End Using

    Return HttpStatusCode.OK
End Function
```

### Minimal API Setup

When the Reporting REST Service has been implemented using [Minimal API](https://www.telerik.com/products/reporting/documentation/embedding-reports/host-the-report-engine-remotely/asp.net-core-web-api-implementation/host-reports-service-in-.net-6-with-minimal-api), first implement a method that sends the [MailMessage](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.mailmessage) object via the [SmtpClient](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient):

```C#
public static HttpStatusCode MyMailSender(MailMessage message)
{
    try
    {
        using (var smtpClient = new SmtpClient("tests.com", 26))
        {
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.Credentials = new NetworkCredential("user", "pass");
            smtpClient.EnableSsl = true;
            smtpClient.Send(message);
        }
        return HttpStatusCode.OK;
    }
    catch
    {
        return HttpStatusCode.InternalServerError;
    }
}
```

Then, this method can be passed alongside the [ReportServiceConfiguration](https://www.telerik.com/products/reporting/documentation/api/telerik.reporting.services.reportserviceconfiguration) of the Reporting REST Service when adding it in the starting point of the application, for example, the `Program.cs` file:

```C#
var mailContext = new MailContext(MyMailSender);
var reportServiceConfiguration = new ReportServiceConfiguration
{
    ReportingEngineConfiguration = builder.Configuration,
    HostAppId = "Reporting",
    Storage = new FileStorage(),
    ReportSourceResolver = new TypeReportSourceResolver()
                                .AddFallbackResolver(
                                    new UriReportSourceResolver(reportsPath))
};

builder.Services.AddRazorPages().AddTelerikReporting(reportServiceConfiguration, mailContext);
```

## See Also

* [Send Mail Message](https://www.telerik.com/products/reporting/documentation/embedding-reports/display-reports-in-applications/web-application/send-mail-message)
