我想運行下面的代碼,而我怎麼能使用SMTP
NetworkCredential nc = new Net.NetworkCredential("USERNAME", "PASSWORD").
發送郵件通過Exchange服務器只使用的Exchange主機(服務器名)和端口
即時得到錯誤對於此代碼:信箱不可用。服務器響應爲:5.7.1客戶端沒有權限發送的該發件人
protected void SendEmail(object sender, EventArgs e)
{
SmtpClient smtpClient = new SmtpClient("ExchangeServerName",25);
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress("[email protected]", "From Me");
MailAddress toAddress = new MailAddress("[email protected]", "To You");
message.From = fromAddress;
message.To.Add(toAddress);
message.Subject = "Testing!";
message.Body = "This is the body of a sample message";
smtpClient.UseDefaultCredentials = true;
System.Net.NetworkCredential nc = CredentialCache.DefaultNetworkCredentials;
smtpClient.Credentials = (System.Net.ICredentialsByHost)nc.GetCredential("ExchangeServerName", 25, "Basic");
smtpClient.Send(message);
lblText.Text ="Email sent.";
}
catch (Exception ex)
{
lblText.Text = "Coudn't send the message!\n " + ex.Message;
}
}