Cómo puedo transformar mi código C #: System.Web.Mail “transform by” System.Net.Mail “

Estoy usando ” System.Web.Mail ” para enviar correos electrónicos a través de SMTP usando C # con mi siguiente código. Está funcionando pero creo que está obsoleto.

Así que quiero usar ” System.Net.Mail

¿Cómo puedo cambiar o transformar mi siguiente código?

using System; using System.Data; using System.Collections; using System.ComponentModel; using System.Configuration; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Web.Mail; using System.Net; //(...) MailMessage mail = new MailMessage(); mail.To = s.EmailFirst; mail.Cc = s.EmailSecond; mail.Bcc = ConfigurationManager.AppSettings["mailConfirmeMe"]; mail.Bcc = ConfigurationManager.AppSettings["mailConfirmeTheir"]; mail.From = ConfigurationManager.AppSettings["FromEmail"]; mail.Subject = "OBJET : CONFIRMATION"; mail.BodyFormat = MailFormat.Html; mail.Body = "

this is my test email body

"; mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "myUserName@domain.com"); //set my username here mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "##########"); //set my password here SmtpMail.SmtpServer = "000.000.0.0"; //set my serveur email SmtpMail.Send( mail ); //(...)

AQUÍ ESTA MI RESPUESTA / SOLUCIÓN ::::::::::::::::::::::::::::::::::: Hola, gracias por sus documentos,

Intento con el siguiente código siguiente:

Funciona, genial …

Según usted, ¿es este código correcto?

AQUÍ ESTA MI RESPUESTA / SOLUCIÓN :::::::::::::::::::::::::::::::::::::

 using System.Net.Mail; //..... protected void OkButton_Click(object sender, System.EventArgs e) { MailAddress from = new MailAddress(ConfigurationManager.AppSettings["FromEmail"]); MailAddress to = new MailAddress(s.EmailFirst); MailMessage message = new MailMessage(from, to); message.Subject = "OBJET : CONFIRMATION"; message.Body = @"

this is my test email body

"; message.BodyEncoding = System.Text.Encoding.UTF8; // Add a carbon copy recipient. MailAddress copy = new MailAddress(s.EmailSecond); message.CC.Add(copy); MailAddress bcc = new MailAddress(ConfigurationManager.AppSettings["mailConfirmeMe"]); message.Bcc.Add(bcc); MailAddress bcc2 = new MailAddress(ConfigurationManager.AppSettings["mailConfirmeTheir"]); message.Bcc.Add(bcc2); //set my serveur email /* string server = "000.000.0.0"; //set my serveur email SmtpClient client = new SmtpClient(server); SmtpClient.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication SmtpClient.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "myUserName@domain.com"); //set my username here SmtpClient.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "##########"); //set my password here */ SmtpClient client = new SmtpClient(); client.Host = "000.000.0.0"; //set my serveur email client.Credentials = new System.Net.NetworkCredential("myUserName@domain.com", "##########"); //set my username here and my password here //client.Credentials = CredentialCache.DefaultNetworkCredentials; Console.WriteLine("Sending an e-mail message to {0} by using the SMTP host {1}.", to.Address, message.CC.ToString(), message.Bcc.ToString()); try { client.Send(message); } catch (Exception ex) { Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}", ex.ToString()); } //...... }

Echa un vistazo a esto: SmtpClient.Send

SmtpClient sustituye a SmtpMail y funciona casi exactamente igual.