public async Task SendMailAsync(string subject, string body, CancellationToken cancel = default) { foreach (var dest in this.Settings.MailSettings.MailToList.Distinct(StrCmpi).OrderBy(x => x, StrCmpi)) { await SmtpUtil.SendAsync(this.Settings.SmtpSettings, this.Settings.MailSettings.MailFrom, dest, subject, body, true, cancel); } }
public void Deve_Enviar_Email_Por_Smtp() { try { var email = new SmtpUtil(_emailRemetente, _emailDestinatarios, "Você recebeu uma mensagem enviada pelo teste <b>Deve_Enviar_Email_Por_Smtp</b>.", _smtp) { NomeRemetente = "Utilzão Teste", Assunto = "Mensagem enviada pelo teste Deve_Enviar_Email_Por_Smtp", MensagemEmHtml = true }; email.Enviar(); Assert.IsTrue(true); } catch (Exception ex) { Assert.Fail(ex.GetBaseException().Message); } }
public void Deve_Enviar_Email_Por_Smtp_Com_Anexo() { try { using (MemoryStream memoryStream = new MemoryStream()) { byte[] contentAsBytes = Encoding.UTF8.GetBytes("Olá, sou um anexo!"); memoryStream.Write(contentAsBytes, 0, contentAsBytes.Length); memoryStream.Seek(0, SeekOrigin.Begin); var contentType = new ContentType { MediaType = MediaTypeNames.Text.Plain, Name = "AnexoEmail.txt" }; var anexo = new Attachment(memoryStream, contentType); var email = new SmtpUtil(_emailRemetente, _emailDestinatarios, "Você recebeu uma mensagem enviada pelo teste <b>Deve_Enviar_Email_Por_Smtp_Com_Anexo</b>.", _smtp) { Anexos = new List <Attachment> { anexo }, NomeRemetente = "Utilzão Teste", Assunto = "Mensagem enviada pelo teste Deve_Enviar_Email_Por_Smtp_Com_Anexo", MensagemEmHtml = true }; email.Enviar(); Assert.IsTrue(true); } } catch (Exception ex) { Assert.Fail(ex.GetBaseException().Message); } }
public override async Task FlushAsync(bool halfFlush = false, CancellationToken cancel = default) { List <string> linesToSend; List <string> lines2ToSend; try { if (MyLocalIp == null) { MyLocalIp = await GetMyPrivateIpNativeUtil.GetMyPrivateIpAsync(IPVersion.IPv4); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } if (MyIpInfo == null) { try { await using GetMyIpClient c = new GetMyIpClient(); MyIpInfo = await c.GetMyIpInfoAsync(IPVersion.IPv4, cancel); } catch { } } string globalInfo = "(Unknown)"; if (MyIpInfo != null) { if (MyIpInfo.GlobalFqdn._IsSamei(MyIpInfo.GlobalIpAddress.ToString())) { globalInfo = MyIpInfo.GlobalFqdn; } else { globalInfo = $"{MyIpInfo.GlobalFqdn} - {MyIpInfo.GlobalIpAddress}"; } } lock (this.Lock) { if (this.Lines.Count == 0 && this.Lines2.Count == 0) { return; } linesToSend = this.Lines; lines2ToSend = this.Lines2; this.Lines = new List <string>(); this.Lines2 = new List <string>(); } string cmdName = CoresLib.Report_CommandName; if (cmdName._IsEmpty()) { cmdName = "Unknown"; } string resultStr = CoresLib.Report_SimpleResult._OneLine(); if (resultStr._IsEmpty()) { resultStr = "Ok"; } StringWriter w = new StringWriter(); w.WriteLine($"Reported: {DtOffsetNow._ToDtStr()}"); w.WriteLine($"Program: {CoresLib.AppName} Built: {Env.BuildTimeStamp._ToDtStr()}"); w.WriteLine($"Hostname: {Env.DnsFqdnHostName}"); w.WriteLine($"Global: {globalInfo}, Local: {MyLocalIp}"); w.WriteLine($"Command: {cmdName}, Result: {(CoresLib.Report_HasError ? "*Error* - " : "OK - ")}{resultStr}"); if (lines2ToSend.Count >= 1) { w.WriteLine("====================="); w.WriteLine(); lines2ToSend.ForEach(x => w.WriteLine(x)); w.WriteLine(); } if (linesToSend.Count >= 1) { w.WriteLine("--------------------"); w.WriteLine(); linesToSend.ForEach(x => w.WriteLine(x)); w.WriteLine(); } w.WriteLine("--------------------"); EnvInfoSnapshot snapshot = new EnvInfoSnapshot(); snapshot.CommandLine = ""; w.WriteLine($"Program Details: {snapshot._GetObjectDump()}"); w.WriteLine(); string subject = $"Report - {cmdName}{(CoresLib.Report_HasError ? " *Error*" : "")} - {linesToSend.Count} lines - {Env.DnsHostName} - {MyLocalIp} ({globalInfo}): {resultStr._NormalizeSoftEther(true)._TruncStrEx(60)}"; var hostAndPort = this.Settings.SmtpServer._ParseHostnaneAndPort(Consts.Ports.Smtp); SmtpConfig cfg = new SmtpConfig(hostAndPort.Item1, hostAndPort.Item2, this.Settings.SmtpUseSsl, this.Settings.SmtpUsername, this.Settings.SmtpPassword); Console.WriteLine("Report Subject: " + subject); try { Console.WriteLine($"SMTP Log Sending to '{this.Settings.MailTo}' ..."); await TaskUtil.RetryAsync(async() => { await SmtpUtil.SendAsync(cfg, this.Settings.MailFrom, this.Settings.MailTo, subject, w.ToString(), false, cancel); return(0); }, 1000, 3, cancel : cancel, randomInterval : true); Console.WriteLine("SMTP Log Sent."); } catch (Exception ex) { Console.WriteLine("SMTP Log Send Error: " + ex.ToString()); } }