I have a razor component in my Blazor project where when user click a button it will trigger email
The OnClick function Email will send email, through the below code
async Task Email(string originalpiv, string docno, string cuscode, string cusname, string docdate)
{
CModule.SendMail("[email protected]", "[email protected]", "", "HI", "HI BRO");
}
The CModule.SendMail is method where the email from,to,subj,body will be passed, below code
public static bool SendMail(string from, string to, string cc, string subject, string body)
{
bool rst = false;
MailMessage Msg = new MailMessage();
try
{
SmtpClient smtp = new SmtpClient("mail.gmail.com", 587);
string[] toeml = to.Split(new Char[] { ';', ',' });
foreach (string tmp in toeml)
{
if (tmp.Trim() != "") Msg.To.Add(tmp.Trim());
}
string[] cceml = cc.Split(new Char[] { ';', ',' });
foreach (string tmp in cceml)
{
if (tmp.Trim() != "") Msg.CC.Add(tmp.Trim());
}
Msg.From = new MailAddress(from.Trim());
Msg.Subject = subject;
Msg.Body = body;
Msg.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "880215");
smtp.Send(Msg);
rst = true;
}
catch (Exception ex)
{
}
finally
{
Msg.Dispose();
}
return rst;
}
These are the code that i have tried, but the email is not sending out, not sure of why, any idea?
Did you add your email class as a service to be used as dependency injection in startup.cs
?
This worked for me:
namespace ReservationBookingSystem.Model
{
public class MailSettings
{
public string Username { get; set; }
public string Password { get; set; }
public int Port { get; set; }
public string FromEmail { get; set; }
public string Host { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ReservationBookingSystem.Model;
namespace ReservationBookingSystem.Services
{
public interface IMailService
{
Task SendEmailAsync(string ToEmail, string Subject, string HTMLBody);
}
}
using ReservationBookingSystem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Net.Mail;
namespace ReservationBookingSystem.Services
{
public class MailService : IMailService
{
private readonly MailSettings _mailConfig;
public MailService(MailSettings mailConfig)
{
_mailConfig = mailConfig;
}
public async Task SendEmailAsync(string ToEmail, string Subject, string HTMLBody)
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress(_mailConfig.FromEmail);
message.To.Add(new MailAddress(ToEmail));
message.Subject = Subject;
message.IsBodyHtml = true;
message.Body = HTMLBody;
smtp.Port = _mailConfig.Port;
smtp.Host = _mailConfig.Host;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(_mailConfig.Username, _mailConfig.Password);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
await smtp.SendMailAsync(message);
}
}
}
services.AddSingleton(Configuration.GetSection("MailSettings").Get<MailSettings>());
services.AddScoped<IMailService, MailService>();
@inject ReservationBookingSystem.Services.IMailService MailService
. Inside the code call await MailService.SendEmailAsync("[email protected]", "test", "test");
This works in blazor server. So basically for you, replace MailSettings
with CModule
and you are good to go
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With