2013-08-18 42 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