2013-08-18 41 views
0

我做了一個小程序捕捉使用此代碼屏幕:附加圖片郵寄

Bitmap b = new Bitmap(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height); 
Graphics g = Graphics.FromImage(b); 
g.CopyFromScreen(Point.Empty,Point.Empty,Screen.PrimaryScreen.WorkingArea.Size); 

現在我想的截圖附加到郵件。我已經編寫了用於發送郵件的必要代碼,我只需要附加圖像。這裏是我用於郵件的代碼:

MailMessage message = new MailMessage(); 
     message.From = new MailAddress("[email protected]"); 
     message.Subject = "Subject"; 
     message.Body = "Body"; 
     message.To.Add("[email protected]"); 
SmtpClient client = new SmtpClient(); 
     client.Credentials = new NetworkCredential("[email protected]", "password"); 
     client.Host = "smtp.gmail.com"; 
     client.Port = 587; 
     client.EnableSsl = true; 
     client.Send(message); 

你能幫我嗎?謝謝。

+0

請看看這個問題,http://stackoverflow.com/q/2825950/1443529。你可以在構造函數中使用'Attachment'來接受'Attachment(Stream,ContentType)' – cpz

+1

對於內聯附件,你可以看看@JeremyThompson的參考文獻。 – cpz

回答

0

檢查下面給出的答案。您需要附加圖像作爲附件。希望能幫助到你。

MailMessage message = new MailMessage(); 
    message.From = new MailAddress("[email protected]"); 
    message.Subject = "Subject"; 
    message.Body = "Body"; 
    message.To.Add("[email protected]"); 
    string filepath = "C:\image.jpg";  // Image File Path 
    mail.Attachments.Add(new Attachment(filepath)); 

    SmtpClient client = new SmtpClient(); 
    client.Credentials = new NetworkCredential("[email protected]", "password"); 
    client.Host = "smtp.gmail.com"; 
    client.Port = 587; 
    client.EnableSsl = true; 
    client.Send(message); 
+0

我知道如何使用其路徑附加圖像,但這是不同的。我想附加併發送圖像,而不保存在計算機上。我想直接從變量「b」附加它。無論如何,我找到了解決方案,我會在這裏發佈。 – Joker

+0

這是我發現的靈魂:http://stackoverflow.com/questions/7529968/sending-screenshot-via-c-sharp。這正是我所問的。 – Joker