2010-09-27 140 views
-2

這在ASP.NET發送錯誤是電子郵件發送的代碼,但它在try塊給出了錯誤:電子郵件使用C#

protected void Page_Load(object sender, EventArgs e) 
{ 
    EmailUtility email = new EmailUtility(); 
    email.Email = new MailMessage(); 
    string body = email.GetEmailTemplate(Server.MapPath("~/EmailTemplates"), "test.htm"); 
    EmailMessageToken token = new EmailMessageToken(); 
    token.TokenName = "$Name$"; 
    token.TokenValue = "Ricky"; 
    EmailMessageTokens tokens = new EmailMessageTokens(); 
    tokens.Add(token); 
    //av.LinkedResources.Add(lr); 
    email.Email.Body = email.ReplaceTokens(body, tokens); 
    email.Email.To.Add(new MailAddress("[email protected]")); 
    email.Email.IsBodyHtml = true; 
    email.Email.From = new MailAddress("[email protected]"); 
    email.Email.Subject = "Hello from bootcamp"; 
    email.SMTP.Host = ConfigurationManager.AppSettings["SMTPServer"]; 
    try 
    { 
     email.SMTP.Send(email.Email); 
     Response.Write("Email sent !"); 
    } 
    catch (Exception ex) 
    { 
     Response.Write(ex.StackTrace); 
    } 
} 

的錯誤是:

at System.Net.Mail.IisPickupDirectory.GetPickupDirectory() 
at System.Net.Mail.SmtpClient.Send(MailMessage message) 
at _Default.Page_Load(Object sender, EventArgs e) in c:\Users\Sahil\Desktop\Csharp Email Code(2)\Test website\EmailTest.aspx.cs:line 38 
+3

發佈錯誤的全部內容(即'ex.ToString()')將提高無限回答這個問題的能力。 – Jamiec 2010-09-27 08:00:28

+0

什麼是EmailUtility?看起來不是一個框架類。 – 2010-09-27 08:01:12

+0

看起來您正在使用第三方庫。在我們提供幫助之前,我們至少需要知道您使用的是什麼。 – Carvellis 2010-09-27 08:01:41

回答

0

我要去閱讀你的想法,並假設你看到System.Net.Mail.SmtpException: Cannot get IIS pickup directory.谷歌搜索,我看到你的網站配置中需要的SMTP配置的參考。您的郵件服務器可能需要憑據,因此您可能需要如下所示:

<system.net> 
    <mailSettings> 
     <smtp from="fromaddress"> 
     <network defaultCredentials="true" 
       host="smtpservername" port="smtpserverport" 
       userName="username" password="password" /> 
     </smtp> 
    </mailSettings> 
</system.net> 

...在您的web.config中。實質上,配置您的Web應用程序能夠與SMTP服務器交談。

查看該主題了解更多referencehere's relevant MSDN documentation