Send Email with .NET API call. Samples / Examples required.

  • 1
  • Question
  • Updated 7 months ago
Are there any examples / samples of how to send an email via the API in C# or VB.net? I have downloaded the available sample for uploading a file but cant find one for emails. 
Photo of Chris

Chris

  • 17 Posts
  • 2 Reply Likes

Posted 7 months ago

  • 1
Photo of Andy Berryman

Andy Berryman, Employee

  • 18 Posts
  • 5 Reply Likes
Are you referring to the ability to send access to an uploaded file via the ShareFile Shares functionality?

https://api.sharefile.com/rest/docs/resource.aspx?name=Shares

- Andy
Photo of Chris

Chris

  • 17 Posts
  • 2 Reply Likes
Hi,
No, I am referring to the ability to send encrypted emails via an API.

https://api.sharefile.com/rest/docs/resource.aspx?name=EncryptedEmails

I wondered if anyone had any examples.
Photo of Andy Berryman

Andy Berryman, Employee

  • 18 Posts
  • 5 Reply Likes
Ok ... Let me see if I can find one in something we have already.  If not ... We'll create one for you.

- Andy
Photo of Chris

Chris

  • 17 Posts
  • 2 Reply Likes
Brilliant thanks
Photo of Andy Berryman

Andy Berryman, Employee

  • 18 Posts
  • 5 Reply Likes
Sorta quickly threw this thing together ... It's shown as using the .NET SDK ... But should show you the flow that you need ...

// Assume you have your ShareFile Client initialized and ready to make calls
var sfClient = ...;

// First you need to have a local file that contains the desired Message Body for the Email
var myMsgBodyFile = @"C:\temp\EmailBody.txt";

// Next you need to upload those contents into ShareFile so that you can reference them for the Email
var parentFolderToUploadToUri = "fill me in";
var uploadedFileId = null;
using (var readStream = new FileInfo(myMsgBodyFile).OpenRead())
{
	var uploadSpec = new UploadSpecificationRequest
	{
		FileName = readStream.Name,
		FileSize = readStream.Length,
		Parent = parentFolderToUploadToUri,
		Method = UploadMethod.Standard
	};
	
	var fileUploader = sfClient.GetAsyncFileUploader(uploadSpec, readStream);
	fileUploader.Config.HttpTimeout = sfClient.Configuration.HttpTimeout;
	
	var uploadResponse = fileUploader.UploadAsync().Result;
	uploadedFileId = uploadResponse[0].Id;
}

// Next create your EncryptedEmail entity
var toRecipients = new List
{
	new Principal
	{
		Name = "fill me in",
		Email = "fill@me.in"
	}
};

var emailParams = new EncryptedEmailCreateParams
{
	Subject = "fill me in",
	ToRecipients = toRecipients,
	RequireLogin = true,
	ExpirationDays = 3
};

var encryptedEmail = sfClient.EncryptedEmails.Create(emailParams).Execute();

// Send the Email
var sendParams = new EncryptedEmailSendParams
{
	Message = new Item() { Id = uploadedFileId },
};
sfClient.EncryptedEmails.Send(encryptedEmail.url, sendParams).Execute();
Photo of Chris

Chris

  • 17 Posts
  • 2 Reply Likes
Brilliant, I will give it a try.

Thank you for the quick response.

Can the message body be uploaded as HTML or does it only work if it is plain text?
Photo of Andy Berryman

Andy Berryman, Employee

  • 18 Posts
  • 5 Reply Likes
Yes I believe that HTML will work.

- Andy
Photo of Chris

Chris

  • 17 Posts
  • 2 Reply Likes
Do you have a sample project with this working? I have tried using it with the supplied SDK but EncryptedEmails doesn't appear to be recognized by the samples.
Photo of Luke Skrzypek

Luke Skrzypek, Employee

  • 53 Posts
  • 7 Reply Likes
Which SDK and version are you using?
I just tried with the latest C# SDK (v4.0.200), and the ShareFile client object does expose the EncryptedEmails endpoint.

ShareFile.Api.Client.ShareFileClient shareFileClient = new ShareFileClient(...);
...
shareFileClient.EncryptedEmails.Send(...);
Photo of Chris

Chris

  • 17 Posts
  • 2 Reply Likes
I was using 3.033 i believe, I have tried it with v4.0.200 but it hangs on await oauthService.PasswordGrantAsync, so I was wondering if there were any updated samples with the email functionality.
(Edited)
Photo of Luke Skrzypek

Luke Skrzypek, Employee

  • 52 Posts
  • 7 Reply Likes
Responded to you on your other thread.