Skip to content

Commit

Permalink
Merge pull request #443 from kzi-nastava/refactor/EmailService
Browse files Browse the repository at this point in the history
[refactor] EmailService
  • Loading branch information
natasakasikovic authored May 27, 2024
2 parents a1ebd93 + a486d88 commit 6213356
Showing 1 changed file with 36 additions and 27 deletions.
63 changes: 36 additions & 27 deletions LangLang/BusinessLogic/UseCases/EmailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,47 @@ public static void SendEmail(string toEmail, string subject, string body, PdfDoc
{
try
{
MailAddress fromAddress = new("[email protected]", "LangLang");
MailAddress toAddress = new(toEmail);

MailMessage mail = new();
mail.From = fromAddress;
mail.To.Add(toAddress);
mail.Subject = subject;
mail.Body = body;

if (pdf != null)
{
var attachment = ConvertFromPdf(pdf);
mail.Attachments.Add(attachment);
}

SmtpClient smtpClient = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
Credentials = new NetworkCredential("[email protected]", "lckfqhhpgyespyso")
};

MailMessage mail = CreateMailMessage(toEmail, subject, body, pdf);
SmtpClient smtpClient = ConfigureSmtpClient();
smtpClient.Send(mail);

}
catch (Exception ex)
{
throw new SmtpException(ex.Message);
throw new SmtpException("Failed to send email: " + ex.Message, ex);
}
}

private static MailMessage CreateMailMessage(string toEmail, string subject, string body, PdfDocument? pdf)
{
MailAddress fromAddress = new("[email protected]", "LangLang");
MailAddress toAddress = new(toEmail);

MailMessage mail = new();
mail.From = fromAddress;
mail.To.Add(toAddress);
mail.Subject = subject;
mail.Body = body;

if (pdf != null)
{
var attachment = ConvertFromPdf(pdf);
mail.Attachments.Add(attachment);
}
}


return mail;
}

private static SmtpClient ConfigureSmtpClient()
{
return new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
Credentials = new NetworkCredential("[email protected]", "lckfqhhpgyespyso")
};
}

private static Attachment ConvertFromPdf(PdfDocument document)
{
MemoryStream ms = new MemoryStream();
Expand Down

0 comments on commit 6213356

Please sign in to comment.