我正在使用“ 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 = "<p>this is my test email body</p>";
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 = @"<p>this is my test email body</p>";
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());
}
//......
}