private void btnCadastrar_Click(object sender, EventArgs e) { if (validarSMTP()) { Usuario usuario = new Usuario(); usuario.nome = tbxNome.Text; IObjectSet pesquisaSMTP = banco.QueryByExample(usuario); if (pesquisaSMTP.HasNext()) { usuario = (Usuario)pesquisaSMTP.Next(); try { ServidorSMTP smtp = new ServidorSMTP(); smtp.emailSMTP = txtEmailSMTP.Text; smtp.senhaSMTP = txtSenhaSMTP.Text; smtp.servidorSMTP = txtServidor.Text; smtp.portaSMTP = Convert.ToInt32(txtPorta.Text); smtp.SSL = cboxSSL.Checked; usuario.servidorSMTP = smtp; banco.Store(usuario); MessageBox.ShowMessageBoxOK("correct", "Servidor SMTP cadastrado com sucesso!", "Servidor cadastrado", DarkTheme); banco.Close(); banco = Db4oFactory.OpenFile(caminhoBanco); reiniciarSistema(); } catch (FormatException) { MessageBox.ShowMessageBoxOK("error", "A porta deve ser um número!", "Formato incorreto", DarkTheme); txtPorta.Focus(); return; } } } }
public Task SendAsync(IdentityMessage message) { ServidorSMTP servidor = ParametroRepository.GetServidorSMTP(); // Plug in your email service here to send an email. SmtpClient client = new SmtpClient(servidor.Endereco, Convert.ToInt32(servidor.Porta)); client.EnableSsl = false; MailMessage mailMessage = new MailMessage(servidor.RemetentePadrao, message.Destination, message.Subject, message.Body); mailMessage.IsBodyHtml = true; return(client.SendMailAsync(mailMessage)); }
private void btnExcluirSMTP_Click(object sender, EventArgs e) { if (validarSMTP()) { if (MessageBox.ShowMessageBoxYesNo("question", "Deseja realmente excluir seus dados do servidor SMTP atual?", "Excluir conta", DarkTheme).Equals("sim")) { Usuario user = new Usuario(); user.nome = tbxNome.Text; IObjectSet pesquisarExcluir = banco.QueryByExample(user); if (pesquisarExcluir.HasNext()) { user = (Usuario)pesquisarExcluir.Next(); ServidorSMTP newServer = new ServidorSMTP(); user.servidorSMTP = newServer; banco.Store(user); reiniciarSistema(); } banco.Close(); banco = Db4oFactory.OpenFile(caminhoBanco); } } }
public static string EnviaCorreo(int idEmpresa, List <string> destinatarios, string asunto, List <string> adjuntos, string cuerpo, List <string> responderA, List <string> ccos = null, string nombreRemitente = "") { string mensaje = ""; GeneralesDataModel contextoGeneral = new GeneralesDataModel(); ServidorSMTP servidorSMTP = null; try { servidorSMTP = contextoGeneral.ServidorSMTP.FirstOrDefault(s => s.IdEmpresa == idEmpresa); } catch (Exception ex) { mensaje = "Ocurrió un error al obtener los datos del servidor SMTP. " + ex.Message; } if (servidorSMTP != null) { SmtpClient smtp = new SmtpClient(); smtp.Host = servidorSMTP.NombreServidorSMTP; smtp.Port = servidorSMTP.PuertoSMTP.Value; smtp.EnableSsl = servidorSMTP.HabilitarSSL.Value; smtp.UseDefaultCredentials = servidorSMTP.UsarCredencialesPorDefecto.Value; smtp.Credentials = new NetworkCredential(servidorSMTP.Usuario, servidorSMTP.Clave); MailMessage msg = new MailMessage(); string from = "*****@*****.**";//servidorSMTP.Usuario if (responderA != null && responderA.Any()) { from = responderA.FirstOrDefault(); foreach (string responder in responderA) { msg.ReplyToList.Add(new MailAddress(responder)); } } msg.IsBodyHtml = true; msg.From = new MailAddress(from, nombreRemitente);//servidorSMTP.Usuario); msg.Subject = asunto; msg.Body = cuerpo; foreach (string destinatario in destinatarios) { msg.To.Add(new MailAddress(destinatario.Trim())); } if (ccos != null) { foreach (string cco in ccos) { msg.Bcc.Add(new MailAddress(cco.Trim())); } } if (adjuntos != null) { foreach (string adjunto in adjuntos) { Attachment attachment = new Attachment(adjunto); msg.Attachments.Add(attachment); } } try { smtp.Send(msg); msg.Dispose(); mensaje = "OK"; } catch (Exception ex) { mensaje = "Error al enviar correo electrónico: " + ex.Message; } } else { mensaje = "No existen datos de servidor SMTP para la empresa actual"; } return(mensaje); }