2013-12-19 235 views
2

我想通過和C#應用程序(控制檯)發送電子郵件。其實我希望它發送一封電子郵件到任何類型的電子郵件,但暫時如果我能把它發送到一個Gmail帳戶,我會很高興。發送Gmail郵件

我只是想暫時把它發送到Gmail賬戶?

整體規劃設計:

namespace EmailAddress 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Program test = new Program(); 
      test.email_send(); 
     } 

     public void email_send() 
     { 
      MailMessage mail = new MailMessage(); 
      SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); 
      mail.From = new MailAddress("[email protected]"); 
      mail.To.Add("[email protected]"); 
      mail.Subject = "Test Mail - 1"; 
      mail.Body = "Your attachment is accessible on your computer!"; 

      System.Net.Mail.Attachment attachment; 
      attachment = new System.Net.Mail.Attachment("g:/example1.txt"); 
      mail.Attachments.Add(attachment); 

      SmtpServer.Port = 587; 
      SmtpServer.Credentials = new System.Net.NetworkCredential("your [email protected]", "your password"); 
      SmtpServer.EnableSsl = true; 

      SmtpServer.Send(mail); 
     } 
    } 
} 

新代碼:不掛,但不會將消息發送到收件箱

static void Main(string[] args) 
     { 
      Program test = new Program(); 
      //test.email_send(); 
      test.CreateTestMessage4(); 
     } 

     public void email_send() 
     { 
      MailMessage mail = new MailMessage(); 
      SmtpClient smtp = new SmtpClient("smtp.gmail.com"); 
      mail.From = new MailAddress("[email protected]"); 
      mail.To.Add("[email protected]"); 
      mail.Subject = "Test Mail - 1"; 
      mail.Body = "Your attachment is accessible on your computer!"; 

      System.Net.Mail.Attachment attachment; 

      attachment = new System.Net.Mail.Attachment("g:\\example1.txt"); 
      mail.Attachments.Add(attachment);//list of attachements 

      smtp.Port = 587;//google standard -- most of the time wouldn't not set 
      smtp.EnableSsl = true; 
      smtp.UseDefaultCredentials = true; 
      smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
      smtp.Credentials = new System.Net.NetworkCredential("*","*"); 

      smtp.Send(mail); 

      Console.WriteLine("-- Sending Email --"); 
      Console.ReadLine(); 
     } 

有人可以試試這個代碼,看看它是否工作。由於某種原因,這是行不通的,所以我想有人對此有新的看法。我只是在這個類的一個實例的主要方法中調用它。

public void email_send() 
     { 
      MailMessage mail = new MailMessage(); 
      SmtpClient smtp = new SmtpClient("smtp.gmail.com"); 
      mail.From = new MailAddress("your email address"); 
      mail.To.Add("your email address"); 
      mail.Subject = "Test Mail - 1"; 
      mail.Body = "Your attachment is accessible on your computer!"; 

      System.Net.Mail.Attachment attachment; 

      attachment = new System.Net.Mail.Attachment("g:\\example1.txt"); 
      mail.Attachments.Add(attachment);//list of attachements 

      //smtp.Port = 587;//google standard -- most of the time wouldn't not set 
      //smtp.EnableSsl = true; 
      //smtp.UseDefaultCredentials = true; 
      //smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
      //smtp.Credentials = new System.Net.NetworkCredential("your address", 
                   "your password"); 

      smtp.Port = 587; 
      smtp.Host = "smtp.gmail.com"; 
      smtp.UseDefaultCredentials = false; 
      smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
      smtp.EnableSsl = true; 
      smtp.Credentials = new System.Net.NetworkCredential("*", "*"); smtp.Send(mail); 

      Console.WriteLine("-- Sending Email --"); 
      Console.ReadLine(); 
     } 
+2

發送_TO的account_和發送_via的account_是完全不同的。你的問題似乎在混合這兩件事。你能澄清你遇到的問題嗎? –

+1

@Doug Hauf請保留每個帖子一個問題。 – kaptan

+0

關於你的新代碼。我從來沒有看到你調用'email_send()'方法。嘗試在'Program test = new Program();'後立即調用'test.email_send();' – mason

回答

2

你的代碼是接近:

MailMessage mail = new MailMessage(); 
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com")) { 
    mail.From = new MailAddress("[email protected]"); 
    mail.To.Add("[email protected]"); 
    mail.Subject = "Test Mail - 1"; 
    mail.Body = "Your attachment is accessible on your computer!"; 

    System.Net.Mail.Attachment attachment; 

    attachment = new System.Net.Mail.Attachment("g:\\example1.txt"); 
    mail.Attachments.Add(attachment); 

    smtp.Port = 587; 
    smtp.EnableSsl = true; 
    smtp.UseDefaultCredentials = false; 
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
    smtp.Credentials = new System.Net.NetworkCredential("your [email protected]", "your password"); 

    smtp.Send(mail); 
} 

現在,關於你的一些其他問題。

端口587僅供Google使用。通常,您連接的郵件服務器會告訴您使用SMTP郵件的端口; Google表示有587.

對於通過其他服務器發送,您通常不會設置端口號。

側筆記:

  1. SmtpClient實現IDisposable接口。所以它應該包含在一個使用條款中,以確保在完成時妥善處理它。除非你正在使用.SendAsync()方法。
  2. 由於兩個原因,我將變量名從SmtpServer更改爲smtp。首先,命名約定。一個方法內的變量被推薦爲駱駝案例(for Example)。其次,由於對象是SmtpClient,SmtpServer是一個誤稱。這些是非常不同的事情,錯誤的事情是不好的做法。
+0

這是我剛剛收到的錯誤,是不是必須有用GMail設置的密碼。SMTP服務器需要安全連接或客戶端未通過身份驗證。服務器響應是:5.5.1需要身份驗證。在 –

+0

瞭解更多@DougHauf:好的。這就是'smtp.Credentials = ...'行的用途。用''[email protected]「'和''你的密碼''用你的實際Gmail密碼替換''你的[email protected]'''。它會使用你的憑證。 – NotMe

+0

@DougHauf永遠不要把你的密碼放到這樣的公共網站上。現在,如果我願意,我可以登錄到您的Gmail帳戶。我可以登錄到其他網站,因爲您可能使用了相同的密碼。您需要立即更改您使用相同密碼的任何網站的gmail密碼和密碼。 – mason

2

你可以試試這個,它的工作對我來說:

SmtpClient SmtpServer = new SmtpClient(); 
SmtpServer.Port = 587; 
SmtpServer.Host = smtp.gmail.com; 
SmtpServer.UseDefaultCredentials = false; 
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network; 
SmtpServer.EnableSsl = true; 
SmtpServer.Credentials = new System.Net.NetworkCredential("your [email protected]", "your password");