2012-07-10 111 views
3

我想運行下面的代碼,而我怎麼能使用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; 
    } 
} 

回答

4

我已經完成了。有關我的代碼的更多詳細信息,請使用this link

下面的代碼是工作的罰款與

服務器:在Windows Server 2003,Windows Server 2008中,在Windows Server 2008 R2

IIS 6.0,7.0

的.Net框架Wotk:2.0,3.5, 4.0

string sMessage; 
SmtpClient smtpClient = new SmtpClient(); 
MailMessage message = new MailMessage(); 
try 
{ 

//you can provide invalid from address. but to address Should be valil 
MailAddress fromAddress = new MailAddress("[email protected]", "BALA"); 

smtpClient.Host = "Exchange Server Name"; 
smtpClient.Port = 25; 
//smtpClient.Port = 587; 


smtpClient.UseDefaultCredentials = true; 

message.From = fromAddress; 
message.To.Add([email protected]); //Recipent email 
message.Subject = _subject; 
message.Body = _details; 
message.IsBodyHtml = true; 

//smtpClient.EnableSsl = true; 

smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; 

smtpClient.Send(message); 

sMessage = "Email sent."; 
} 
catch (Exception ex) 
{ 
sMessage = "Coudn't send the message!\n " + ex.Message; 
} 


lblMailStatus.Text = sMessage; 
1

您正試圖發送使用Exchange郵件消息。爲此,發件人(或發送進程)必須對其登錄的帳戶具有權限,以代表您指定爲發件人的用戶發送。這與通過Exchange的SMTP郵件傳輸代理(MTA)不同,以便讓Exchange接收和路由電子郵件。所以你知道你應該使用SMTP來做到這一點,但你只是試圖使用錯誤的API來實現這一點。您希望看看CDOSYS通過SMTP MTA發送它,而不必進行用戶驗證。在System.Web.Mail.MailMessage上搜索更具體的例子 - 這裏有很多。如果Exchange服務器似乎不接受/傳遞以這種方式傳遞給它的SMTP消息,則可能只需要打開它的一些配置。在這種情況下,Exchange服務器可能配置了對通過其SMTP MTA接收的郵件進行路由的嚴格安全性,並且只需要將發送這些郵件的計算機的IP地址配置爲允許郵件轉發。

0

嘗試的NetworkCredential NC =新Net.NetworkCredential( 「USERNAME」, 「密碼」, 「域」)