将图像附加到邮件

时间:2013-08-18 00:08:12

标签: c# image email screenshot

我制作了一个使用此代码捕获屏幕的小应用程序:

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("mail_address@gmail.com");
        message.Subject = "Subject";
        message.Body = "Body";
        message.To.Add("mail_address@gmail.com");
SmtpClient client = new SmtpClient();
        client.Credentials = new NetworkCredential("mail_address@gmail.com", "password");
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.EnableSsl = true;
        client.Send(message);

你能帮我解决这个问题吗?谢谢。

1 个答案:

答案 0 :(得分:0)

检查下面给出的答案。您需要附加图像作为附件。希望它有所帮助。

    MailMessage message = new MailMessage();
    message.From = new MailAddress("mail_address@gmail.com");
    message.Subject = "Subject";
    message.Body = "Body";
    message.To.Add("mail_address@gmail.com");
    string filepath = "C:\image.jpg";     // Image File Path 
    mail.Attachments.Add(new Attachment(filepath)); 

    SmtpClient client = new SmtpClient();
    client.Credentials = new NetworkCredential("mail_address@gmail.com", "password");
    client.Host = "smtp.gmail.com";
    client.Port = 587;
    client.EnableSsl = true;
    client.Send(message);