将图像附加到通过Web API 2.0发送的电子邮件

时间:2018-01-24 19:38:13

标签: c# asp.net asp.net-web-api nativescript email-attachments

我的移动应用程序带有带图像选项的数据输入表单。现在,当我点击提交时,所有数据都应该绑定在正文中,而Image应该附加到我通过web api发送的电子邮件中。我可以发送没有图像的电子邮件。如何将移动设备的图像附加到发送电子邮件的web api上?

图片捕获代码。

exports.onCapture = function(args)
  { 
var dialogs = require("ui/dialogs");
dialogs.action({
  message: "Select Option to Attach Image",
  cancelButtonText: "Cancel",
  actions: ["Camera", "Gallary"]
}).then(function (result) {
  console.log("Dialog result: " + result); 
  if(result == "Camera"){
      //Do action1
      console.log("Camera selected");
      if(imagecontainer.imageSource!=null)
      {
          imagecontainer.imageSource=null;
      }

camera.takePicture({
width: 200, height: 200, keepAspectRatio: false, saveToGallery: true
})   
.then(function (imageAsset) {
    console.log("Result is an image asset instance"); 

    filepath = fs.path.join(fs.knownFolders.documents().path, "img_" + (new Date().getTime() / 1000) + ".jpg");

    imageAsset.getImageAsync(image => {

    imagecontainer.imageSource = imageSource.fromNativeSource(image);

   (imagecontainer.imageSource).saveToFile(filepath, uiEnums.ImageFormat.jpeg);
    console .log("image source:"+image);
    });
}).catch(function (err) {
    console.log("Error -> " + err.message);
});
  }else if(result == "Gallary"){
      if(imagecontainer.imageSource!=null)
      {
          imagecontainer.imageSource=null;
      }
      //Do action2
      var items;
      var imagepicker = require("nativescript-imagepicker");
      var context = imagepicker.create({
          mode: "single"
      });
      context.authorize()
          .then(function() {
              return context.present();
          })
          .then(function(selection) {
            filepath = fs.path.join(fs.knownFolders.documents().path, "img_" + (new Date().getTime() / 1000) + ".jpg");
              selection.forEach(function(selected) {
                selected.getImage({ maxWidth: 200, maxHeight: 200, aspectRatio: 'fill' })

                .then((imageSourceS) => {
                    imagecontainer.imageSource = imageSourceS;

                    imageSourceS.saveToFile(filepath, uiEnums.ImageFormat.jpeg);
                });

                console.log("pic selected");
              });
          }).catch(function (e) {
          console.log(e);
      });
  }
  console.log("file path;;;;;;:"+filepath);
 });

};

发送电子邮件的WebAPI

   public async Task<string> SendEMail(string recipient,string subject, string message)
    {
        string isMessageSent = "";
        try
        {

            //Intialise Parameters  
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("10.10.200.98");
            client.Port = 25;
            client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(Properties.Settings.Default.EmailHost, Properties.Settings.Default.EmailHostPassword);
            client.EnableSsl = false;
            client.Credentials = credentials;

            var mail = new System.Net.Mail.MailMessage(Properties.Settings.Default.EmailHost, recipient);
            mail.Subject = subject;
            mail.Body = message;
            mail.IsBodyHtml = true;




            //System.Net.Mail.Attachment attachment;  
            //attachment = new Attachment(@"C:\Users\XXX\XXX\XXX.jpg");---->this should be image from filepath of above   .js file
            //mail.Attachments.Add(attachment);  
            client.Send(mail);

            isMessageSent = "Sent";
        }
        catch (Exception ex)
        {
            isMessageSent = ex.ToString();
        }
        return isMessageSent;

    }

0 个答案:

没有答案