2009-11-20 47 views
1

我無法發送電子郵件到雅虎服務器,因爲我的代碼在C#2008中將「異常發送失敗」作爲「失敗發送郵件」。 請爲yahoo服務器和gmail服務器提供SMTP HostName,PortName。通過.NET代碼發送電子郵件

而且還提供一個良好的工作C#代碼,我可以直接發送電子郵件到任何郵件服務器。

請提供完整的工作代碼......以便我將複製到Visual Studio環境並執行相同的操作。 因爲我從早上得到異常....無法解決問題。 請在這方面幫助我。

+0

你可以發佈你的代碼嗎(沒有真正的用戶名和密碼) – 2009-11-20 11:23:25

+0

我只使用下面的代碼只有....但得到異常 – Hariram 2009-11-20 12:09:11

回答

14

對於Gmail:

var client = new SmtpClient("smtp.gmail.com", 587); 
client.EnableSsl = true; 
client.Credentials = new NetworkCredential("[email protected]", "secret"); 

var mail = new MailMessage(); 
mail.From = new MailAddress("[email protected]"); 
mail.To.Add("[email protected]"); 
mail.Subject = "Test mail"; 
mail.Body = "test body"; 
client.Send(mail); 

對於雅虎:

var client = new SmtpClient("smtp.mail.yahoo.com", 587); 
client.Credentials = new NetworkCredential("[email protected]", "secret"); 

var mail = new MailMessage(); 
mail.From = new MailAddress("[email protected]"); 
mail.To.Add("[email protected]"); 
mail.Subject = "Test mail"; 
mail.Body = "test body"; 
client.Send(mail);    
+0

對不起.......上述代碼根本不適用於 – Hariram 2009-11-20 11:54:25

+0

請在這方面幫助我。 需要非常極大的幫助。 因爲我複製了上面相同的代碼,並相應地在上面的代碼中提供了我的憑據,並在控制檯以及窗口中執行....但是作爲'Failure Sending mail'發生異常。 請幫我........... – Hariram 2009-11-20 11:56:03

+1

您是否有可能阻擋可能阻塞端口587的防火牆? – 2009-11-20 12:02:35

0

記住,有些ISP(包括我)強迫客戶使用自己的SMTP服務器(作爲中繼)。垃圾郵件保護是原因。

因此,您應該避免從客戶端應用程序向Internet發送電子郵件,除非您允許用戶指定其SMTP主機名,或者您的應用程序依賴於用戶的電子郵件軟件(MAPI,...)。

0

想象一下,如果您發佈完整的異常消息以及堆棧跟蹤,那麼我們能夠幫助您更容易。

此外,再往前走一步,啓用System.Net.Mail的日誌記錄,以便我們可以在網絡級別看到任何可能的故障。

如果你不知道如何啓用日誌記錄SNM,這裏是一個鏈接:

http://systemnetmail.com/faq/4.10.aspx

謝謝!

戴夫

0

有兩種方法,使我們可以發送郵件,

1)第一種是使用JavaScript鏈接「電子郵件地址」。這將不會自動發送郵件,但它只是打開郵件window.Refer到下面的代碼

<a class="label" onclick='javascript:buildEmail(this)'>Send Mail</a> 

查找下面的JS方法

 function buildEmail(el) { 
var emailId = [email protected]; 
    var subject="Hi"; 
var body="Hello"; 
    el.href = "mailto:" + emailId + "?Subject=" + escape(subject) + 
              "&Body=" + escape(body); 
} 

2)第二種方法是使用System.Net.Mail,它會自動將郵件以安全的方式發送給收件人。

string subject="Hello"; 
     string body="Data"; 
     using (MailMessage objMail = new MailMessage ("[email protected]", "[email protected]"))//From and To address respectively 
       { 
        objMail.IsBodyHtml = false;// Message format is plain text 
        objMail.Priority = MailPriority.High;// Mail Priority = High 
        objMail.Body = "Hello"; 
        ArrayList CCarr = new ArrayList();//Assume we add recipients here 

        // populate additional recipients if specified 
        if ((CCarr != null) && (CCarr .Count > 0)) 
        { 
         foreach (string recipient in CCarr) 
         { 
          if (recipient != "Please update the email address") 
          { 
           objMail.CC.Add (new MailAddress (recipient)); 
          } 
         } 
        } 

        // Set the subject of the message - and make sure it is CIS Compliant 
         if (!subject.StartsWith ("SigabaSecure:")) 
         { 
          subject = "SigabaSecure: " + subject; 
         } 
        objMail.Subject = subject; 

        // setup credentials for the smpthost 
        string username = "Username"; 
        string passwd =  "xxxxxx"; 
        string smtpHost = "mail.bankofamerica.com"; 

        SmtpClient ss = new SmtpClient(); 
        ss.EnableSsl= true; 
        ss.Host = smtpHost; 
        ss.Credentials = new NetworkCredential (username, passwd); 
        ss.Send (objMail); 
} 

Sigaba安全電子郵件通過使用桌面插件和基於Web的驗證和解密的保證從客戶端到客戶端的電子郵件。