在UWP中发送捕获的照片作为附件

时间:2018-05-26 01:26:17

标签: c# uwp smtp

所以我现在一直在喋喋不休,我似乎无法弄清楚如何从网络摄像头拍摄照片并将其附加到电子邮件中。两者都成功发送电子邮件并附上图像。但是,当我尝试打开图像时,它将无法打开。

首先我尝试使用LightBuzz.SMTP

    private async Task<SmtpResult> SendAlertEmail(List<CapturedPhoto> images)
    {
        using (SmtpClient client = new SmtpClient("smtp.gmail.com", 465, true, "mygmail@gmail.com", "myPassword"))
        {

            EmailMessage emailMessage = new EmailMessage();

            emailMessage.Sender.Address = "IoTAlertApp@donotreply.com";
            emailMessage.Sender.Name = "IoT App";
            emailMessage.To.Add(new EmailRecipient("myemail@gmail.com"));
            emailMessage.Subject = "ALERT | MOTION DETECTED";
            emailMessage.Body = "This is an email sent from a UWP app!";

            foreach (CapturedPhoto image in images)
            {
                int imageCount = 1;
                string fileName = "image_" + imageCount + "_" + DateTime.Now.Ticks + ".jpg";
                RandomAccessStreamReference reference = RandomAccessStreamReference.CreateFromStream(image.Frame.CloneStream());
                emailMessage.Attachments.Add(new EmailAttachment(fileName, reference));
                break;
            }
            images.Clear();
            SmtpResult result = await client.SendMailAsync(emailMessage);

            return result;
        }
    }

然后我尝试使用EASendMailRT并得到相同的结果

    private async Task Send_Email(byte[] image)
    {
        String Result = "";
        try
        {
            SmtpMail oMail = new SmtpMail("TryIt");
            SmtpClient oSmtp = new SmtpClient();


            oMail.From = new MailAddress("IoTSquatter@DoNotReply.com");


            oMail.To.Add(new MailAddress("myemail@gmail.com"));


            oMail.Subject = "ALERT | MOTION DETECTED";


            SmtpServer oServer = new SmtpServer("smtp.gmail.com");


            oServer.User = "myemail@gmail.com";
            oServer.Password = "password";

            oServer.Port = 465;
            oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

            Attachment oAttachment = oMail.AddAttachment("testImage.jpg",image);

            string contentID = "test001@host";
            oAttachment.ContentID = contentID;
            oMail.HtmlBody = "<html><body>this is an <img src=\"cid:"
                    + contentID + "\"> embedded picture.</body></html>";

            await oSmtp.SendMailAsync(oServer, oMail);
            Result = "Email was sent successfully!";
        }
        catch (Exception ep)
        {
            Result = String.Format("Failed to send email with the following error: {0}", ep.Message);
        }

    }

我不确定我做错了什么。我认为这与文件格式有关。

1 个答案:

答案 0 :(得分:1)

答案最终成为我创建流的方式。以下是传递给电子邮件的正确流。

 var stream = new InMemoryRandomAccessStream();
 await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream);

然后我像这样添加了

string fileName = "image_"+imageCount+".png";
RandomAccessStreamReference reference = RandomAccessStreamReference.CreateFromStream(stream);
emailMessage.Attachments.Add(new EmailAttachment(fileName, reference));

以下是LightBuzz的结果

enter image description here