using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Mail;
using System.Threading.Tasks;
using System.IO;
public class CameraService
{
private string[] cameraIps = { "192.168.1.1", "192.168.1.2" }; / Add all camera IPs here
private const string Endpoint = "/ISAPI/Streaming/channels/101/picture";
private const int Port = 80;
private const string Username = "yourUsername";
private const string Password = "yourPassword";
private const string EmailTo = "example@email.com";
public async Task CaptureAndSendImages()
{
var images = await CaptureCameraImages();
SendEmail(images);
}
private async Task<byte[][]> CaptureCameraImages()
{
byte[][] images = new byte[cameraIps.Length][];
for (int i = 0; i < cameraIps.Length; i++)
{
var url = $"http://{cameraIps[i]}:{Port}{Endpoint}";
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new Uri(url), "Digest", new NetworkCredential(Username, Password));
using var httpClientHandler = new HttpClientHandler
{
Credentials = credentialCache
};
using var httpClient = new HttpClient(httpClientHandler);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await httpClient.GetAsync(url);
if (response.IsSuccessStatusCode)
{
images[i] = await response.Content.ReadAsByteArrayAsync();
}
else
{
/ Handle failure to get an image, maybe log an error or throw an exception
}
}
return images;
}
private void SendEmail(byte[][] images)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("youremail@example.com");
mail.To.Add(EmailTo);
mail.Subject = "Camera Images";
mail.Body = "Attached are the camera images.";
for (int i = 0; i < images.Length; i++)
{
var stream = new MemoryStream(images[i]);
mail.Attachments.Add(new Attachment(stream, $"camera_{i}.jpg"));
}
SmtpClient smtpClient = new SmtpClient("your.smtp.server");
smtpClient.Credentials = new NetworkCredential("username", "password");
smtpClient.Send(mail);
}
}