Copyright © Giltaş A.Ş.
using System.Net.Mail;
using System.Threading.Tasks;
public class EmailController
{
private readonly string _smtpHost = "giltas.com.tr";
private readonly int _smtpPort = 587;
private readonly string _smtpUser = "test@giltas.com.tr";
private readonly string _smtpPass = "ti42v@52W";
public async Task SendEmailAsync(string to, string subject, string body)
{
try
{
using (var smtpClient = new SmtpClient(_smtpHost, _smtpPort))
{
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(_smtpUser, _smtpPass);
var mailMessage = new MailMessage
{
From = new MailAddress(_smtpUser),
Subject = subject,
Body = body,
IsBodyHtml = false
};
mailMessage.To.Add(to);
await smtpClient.SendMailAsync(mailMessage);
Console.WriteLine("Email sent successfully.");
}
}
catch (Exception ex)
{
Console.WriteLine("Failed to send email: " + ex.Message);
}
}
}