I'm trying to send an email from a Blazor WASM app but I'm getting this exception
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Message could not be sent. System.Net.Mail.SmtpException: Message could not be sent. ---> System.PlatformNotSupportedException: System.Net.Dns:GetHostByName is not supported on this platform. at System.Net.Sockets.TcpClient..ctor (System.String hostname, System.Int32 port) <0x44e7108 + 0x000aa> in :0 at System.Net.Mail.SmtpClient.SendInternal (System.Net.Mail.MailMessage message) <0x44e6a78 + 0x00016> in :0 at System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMessage message) <0x44e19c0 + 0x000c4> in :0 --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send (System.Net.Mail.MailMessage message) <0x44e19c0 + 0x000ee> in :0 at MusicSellingApp.Client.Pages.ArtistProfile.SendMail () [0x000a3] in C:\Users\ismailghedamsi\Source\Repos\MusicSellingPlatformVeille\MusicSellingApp\Client\Pages\ArtistProfile.razor:33 at MusicSellingApp.Client.Pages.ArtistProfile.OnInitializedAsync () [0x0000f] in C:\Users\ismailghedamsi\Source\Repos\MusicSellingPlatformVeille\MusicSellingApp\Client\Pages\ArtistProfile.razor:38 at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync () <0x3f6d608 + 0x0013a> in :0
This is my razor file
@page "/artistProfile"
@inject ILocalStorageService storageService
@using System.Net.Mail
<h2>@(artist == null)</h2>
<UserInfoComponent User="@artist" />
@code{
Artist artist;
public void SendMail()
{
try
{
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Sending mail";
mail.Body = "<h2>this is a mail body</h2>";
mail.IsBodyHtml = true;
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587))
{
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword");
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
}
catch (Exception)
{
throw;
}
}
protected override async Task OnInitializedAsync()
{
SendMail();
artist = await storageService.GetItemAsync<Artist>("loggedUser");
}
}
The same c# code is working on a console app
Even though Blazor (wasm) can run C# code direct on the browser, it's limited by the browser capabilities, send an e-mail is not something the browser can do. In this cases generally what happens is that a PlatformNotSupportedException is throw or you'll get a warning on visual studio CA1416: Validate platform compatibility. The alternative in this case would be call a server API that sends the e-mail.