2012-10-02 29 views
2

呃,我已經完成了我的功課,甚至在這個網站上看過幾個例子,都無濟於事。我的程序旨在發送填寫在表單上的數據,並將其發送到電子郵件。我的其他代碼顯示沒有錯誤,除了SmptMailMessage之外。這裏是我的代碼:C#中的錯誤:無法找到類型或命名空間'SmptMailMessage'

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Net.Mail; 

namespace FPSArrestReport 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void btn1_Click(object sender, EventArgs e) 
    { 

     SmtpClient SmptMailMessage = new SmtpClient(); 
     SmptMailMessage mail = new SmtpMailMessage("smtp.gmail.com", 25); // error on this line 
     \\on the SmptMailmessage 

      //set the to address to the primary email 
     mail.To.Add("[email protected]"); 

    //set the message type and subject and body 
     mail.IsHtmlMessage = true; 
     mail.Subject = ""; 
     mail.Body = "Hello world!"; 

    //send the email 
     mail.Send(); 
     } 
+3

當您一貫使用'Smtp'而不是'Smpt'時,您的許多問題都會消失。 – spender

+0

Noob here:你如何從cshtml頁面調用這個函數? –

+0

哈哈,@宣傳。我注意到我正在使用stmp xD –

回答

1

.NET中沒有StmpMailMessage類。你想要MailMessage。您還希望將服務器憑據傳遞給SmtpClient

這裏的這個問題解釋瞭如何使用Gmail做到這一點。

Sending email through Gmail SMTP server with C#

+0

如何將服務器證書傳遞給SmtpClient?對不起,我是C#的新手。 – user1713246

+0

@ user1713246查看鏈接的問題。 –

5

有一個在System.Net.Mail庫中沒有這樣的類型爲SmtpMailMessage(或SmptMailMessage爲此事)。看起來你正在試圖創建一個SmtpClient實例來發送消息。也許你的意思是做類似的事情;

SmtpClient client = new SmtpClient("smtp.gmail.com", 25); 

MailMessage mail = new MailMessage(); 
mail.To.Add("[email protected]"); 
client.Send(mail); 

您在這裏使用兩個對象 - 一個SmtpMailClient(做發送)和MAILMESSAGE它描述了消息。

+0

感謝您的提示,但是,當我做你上面發佈的內容時,出現IsHtmlTrue顯示的錯誤。有任何解決這個問題的方法嗎?感謝你目前的幫助。 – user1713246

+0

MailMessage上沒有這樣的屬性。也許你想.IsBodyHtml = true –

+0

謝謝大家..但現在,另一個錯誤:當我嘗試添加mail.from.Address,它給了我一個錯誤:不可使用的成員System.Net.MailAddress.Address不能使用像一種方法。對不起,問這麼多。 – user1713246

1

這應該是一個簡單的解決方案。另外,請嘗試注意您的拼寫正確。 :D

$ 
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Windows.Forms; 
    using System.Net.Mail; 
    using System.Net; 
    namespace WindowsFormsApplication1 
    { 
     public partial class Form1 : Form 
     { 
      MailAddress fromAddress = new MailAddress("[email protected]", "NameForIt"); 
      MailAddress toAddress = new MailAddress("DesinationEmailAddress", "NameForDestination"); 
      const string fromPassword = "password"; 
      const string subject = "Subject"; 
      const string body = "First line of text \n Second line of text."; 

      public Form1() 
      { 
       InitializeComponent(); 

      } 

      private void button1_Click(object sender, EventArgs e) 
      { 

       SmtpClient client = new SmtpClient() 
       { 
        Host = "smtp.gmail.com", 
        Port = 587, 
        EnableSsl = true, 
        DeliveryMethod = SmtpDeliveryMethod.Network, 
        UseDefaultCredentials = false, 
        Credentials = new NetworkCredential(fromAddress.Address, fromPassword) 
       }; 

       try 
       { 
        MailMessage message = new MailMessage(fromAddress, toAddress) 
        { 
         Subject = subject, 
         Body = body 
        }; 

        client.Send(message); 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show("There was an error!" + ex.Message); 
       } 


      } 

      } 
     } 
+1

RE:「另外,試着看你的拼寫正確。」......例如「你是」而不是「你的」? ;-) –

相關問題