我如何转换我的C#代码:System.Web.Mail“transform by”System.Net.Mail“

我使用“ System.Web.Mail ”使用C#通过SMTP发送电子邮件,代码如下。 它正在运作,但我认为它已经过时了

所以我想使用“ System.Net.Mail

如何更改或转换以下代码

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 ); //(...)

这是我的答案/解决方案 ::::::::::::::::::::::::::::::::感谢您的文件,

我尝试使用以下新代码:

它很有用,很棒……

据你说,这段代码是正确的吗?

这是答案/解答 :::::::::::::::::::::::::::::::::

 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()); } //...... }

看看这个: SmtpClient.Send

SmtpClient替代SmtpMail并且几乎完全相同。