public static SmtpServer ImportFromDTO(SmtpServerDTO dtoObject) { SmtpServer server = new SmtpServer(dtoObject.nome, dtoObject.endereco, dtoObject.porta); server.requiresTLS = dtoObject.requiresTLS; server.username = dtoObject.usuario; server.password = dtoObject.senha; return server; }
public static SmtpServer ImportFromDTO(SmtpServerDTO dtoObject) { SmtpServer server = new SmtpServer(dtoObject.nome, dtoObject.endereco, dtoObject.porta); server.requiresTLS = dtoObject.requiresTLS; server.username = dtoObject.usuario; server.password = dtoObject.senha; return(server); }
public MailSender(SmtpServer smtpServer, IListener exceptionHandler) { if (smtpServer == null) { if (exceptionHandler != null) exceptionHandler.NotifyObject(new Exception("Parâmetro nulo - smtpServer")); return; } smtpClient = new SmtpClient(smtpServer.address, smtpServer.port); if (smtpServer.requiresTLS) smtpClient.EnableSsl = true; Boolean useCredentials = true; if (String.IsNullOrEmpty(smtpServer.username)) useCredentials = false; if (String.IsNullOrEmpty(smtpServer.password)) useCredentials = false; if (useCredentials) { smtpClient.Credentials = new NetworkCredential(smtpServer.username, smtpServer.password); } // Atribui o handler SendCompleted, utilizado para envio assíncrono smtpClient.SendCompleted += new SendCompletedEventHandler(SmtpClientSendCompleted); // Atribui o responsável pelo tratamento de exceções, elas não são tratadas nesta classe this.exceptionHandler = exceptionHandler; }
public MailSender(SmtpServer smtpServer, IListener exceptionHandler) { if (smtpServer == null) { if (exceptionHandler != null) { exceptionHandler.NotifyObject(new Exception("Parâmetro nulo - smtpServer")); } return; } smtpClient = new SmtpClient(smtpServer.address, smtpServer.port); if (smtpServer.requiresTLS) { smtpClient.EnableSsl = true; } Boolean useCredentials = true; if (String.IsNullOrEmpty(smtpServer.username)) { useCredentials = false; } if (String.IsNullOrEmpty(smtpServer.password)) { useCredentials = false; } if (useCredentials) { smtpClient.Credentials = new NetworkCredential(smtpServer.username, smtpServer.password); } // Atribui o handler SendCompleted, utilizado para envio assíncrono smtpClient.SendCompleted += new SendCompletedEventHandler(SmtpClientSendCompleted); // Atribui o responsável pelo tratamento de exceções, elas não são tratadas nesta classe this.exceptionHandler = exceptionHandler; }
private static void SendMailing(DataConnector connector, MailingDTO mailing, SmtpServerDTO server) { MailMessage mailMessage = null; if (mailing.codigoContrato == 0) { mailMessage = MountBusinessPartnerBilling(connector, mailing.businessPartnerCode, mailing.enviarDemonstrativo); } else { mailMessage = MountContractBilling(connector, mailing.codigoContrato, mailing.codigoSubContrato, mailing.enviarDemonstrativo); } List <String> reportFiles = null; if (mailing.enviarDemonstrativo && mailMessage.Attachments.Count > 0) { reportFiles = new List <String>(); foreach (Attachment mailAttachment in mailMessage.Attachments) { reportFiles.Add(mailAttachment.Name); } } // TODO: Consertar o envio através do .NET, utilizar algum componente que funcione, o SmtpClient // da Microsoft não suporta SSL implicito SmtpServer smtpServer = SmtpServer.ImportFromDTO(server); MailSender mailSender = new MailSender(smtpServer, new TraceHandler()); mailSender.SetContents(mailMessage.Body, reportFiles); Boolean success = mailSender.SendMail("Faturamento de contrato", server.usuario, mailing.destinatarios); if (!success) { // caso não tenha tido sucesso ao enviar através do SmtpClient do .NET tenta enviar pelo PHP String mailerUrl = "http://" + primaryServer + "/Contratos/AjaxCalls/SendEmail.php"; String mailerParams = "subject=Faturamento%20de%20contrato&mailBody=" + mailMessage.Body + "&recipients=" + mailing.destinatarios; String aditionalParams = "&fileCount=0"; if (reportFiles != null) { aditionalParams = "&fileCount=" + reportFiles.Count; int fileIndex = 0; foreach (String filename in reportFiles) { aditionalParams += "&filename" + fileIndex + "=" + reportFiles[fileIndex]; aditionalParams += "&path" + fileIndex + "=" + Environment.CurrentDirectory + @"\" + reportFiles[fileIndex]; fileIndex++; } } RequestHandler mailerRequest = new RequestHandler(mailerUrl, new TraceHandler()); mailerRequest.StartRequest(mailerParams + aditionalParams, null); String mailerResponse = (String)mailerRequest.ParseResponse(typeof(System.String)); if (mailerResponse == "Email enviado com sucesso!") { success = true; } if (!success) { EventLog.WriteEntry("Billing Mailer", mailerResponse); } } if (success) // e-mail enviado com sucesso, grava no banco de dados a data { MailingDAO mailingDAO = new MailingDAO(connector.MySqlConnection); mailing.ultimoEnvio = DateTime.Now; mailingDAO.SetMailing(mailing); } }