Jassim Rahma

Jassim Rahma

  • NA
  • 48
  • 0

Send attachment using HttpClient

Dec 16 2017 12:46 PM
Hi,
 
I am using below code to send email using Mailgun and HttpClient.
 
I now have an image on my page which is imgProfile and I want to send this image as an attachment in the same code.
 
How can Id o that please?
  1. async Task<HttpStatusCode> SendMessage()  
  2. {  
  3.     try  
  4.     {  
  5.         loadingShareQRCode.IsBusy = true;  
  6.   
  7.         var client = new HttpClient();  
  8.         client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes("api" + ":" + "key-xxxxxxxxxxxxx")));  
  9.   
  10.         var form = new Dictionary<stringstring>();  
  11.         form["from"] = "Zeera <[email protected]>";  
  12.         // form["to"] = the_name + " <" + the_email + ">";  
  13.         // form["from"] = Convert.ToString(App.Current.Properties["ZeeraCustomerFirstName"]) + " " + Convert.ToString(App.Current.Properties["ZeeraCustomerLastName"]) + " <" + Convert.ToString(App.Current.Properties["ZeeraCustomerEmail"]) + ">";  
  14.         form["to"] = txtShareQRCodeName.Text + " <" + txtShareQRCodeEmail.Text + ">";  
  15.         form["subject"] = txtShareQRCodeSubject.Text;  
  16.   
  17.         body = "<html dir='ltr' lang='en'>";  
  18.         body += "<head>";  
  19.         body += "<meta charset='UTF-8'>";  
  20.         body += "<meta name='viewport' content='width=device-width, initial-scale=1.0'>";  
  21.         body += "</head>";  
  22.         body += "<body dir='ltr' lang='en'>";  
  23.         body += "Dear " + txtShareQRCodeName.Text + "<br /><br />";  
  24.         body += Convert.ToString(App.Current.Properties["ZeeraCustomerFirstName"]) + " has sent you the attached Zeera QR code with below details:";  
  25.         // body += txtContactZeeraMessage.Text;  
  26.         body += "</body>";  
  27.         body += "</html>";  
  28.   
  29.         form["html"] = Regex.Replace(body, @"\r\n?|\n""<br />");  
  30.   
  31.         var response = await client.PostAsync("https://api.mailgun.net/v2/" + "reach.softnames.com" + "/messages"new FormUrlEncodedContent(form));  
  32.   
  33.         return response.StatusCode;  
  34.     }  
  35.     catch  
  36.     {  
  37.         loadingShareQRCode.IsVisible = false;  
  38.         loadingShareQRCode.IsBusy = false;  
  39.         btnShareQRCodeSend.IsVisible = true;  
  40.     }  
  41.   
  42.     return HttpStatusCode.Unused;  
  43. }  
 
 
Thanks,
Jassim

Answers (1)