Send Messages with Files
This guide explains how to send file attachments securely.
Two Methods for Attaching Files
- By file path (
FilesPath) - Files on disk - By binary content (
FileArgs) - Files in memory
Method 1: Files by Path
Quick Example
var args = new MutliRecipientArgs(
endpoint, serial, apiUser, apiPassword,
recipients, message, subject,
null, null, SendMethodEnum.onlyEmail,
false, true, true, "en-CA", 5, 1440
);
// Add files by their path
args.FilesPath = new List<string>
{
@"C:\Documents\report.pdf",
@"C:\Documents\invoice.xlsx"
};
var response = MessageHelper.MultiRecipientMessage(args);
Complete Example: Sending Contract Documents
using System;
using System.Collections.Generic;
using System.IO;
using SecureExchangesSDK.Helpers;
using SecureExchangesSDK.Models.Args;
using SecureExchangesSDK.Models.Entity;
using SecureExchangesSDK.SecureExchanges;
public class ContractDeliveryService
{
private readonly Guid _serial;
private readonly Guid _apiUser;
private readonly Guid _apiPassword;
public ContractDeliveryService(Guid serial, Guid apiUser, Guid apiPassword)
{
_serial = serial;
_apiUser = apiUser;
_apiPassword = apiPassword;
}
/// <summary>
/// Sends a contract with its annexes
/// </summary>
public SendResult SendContract(
string recipientEmail,
string recipientName,
string contractPath,
List<string> annexPaths = null)
{
// Validate files
if (!File.Exists(contractPath))
return new SendResult { Success = false, Error = "Contract not found" };
// Prepare file list
var filePaths = new List<string> { contractPath };
if (annexPaths != null)
{
foreach (var path in annexPaths)
{
if (File.Exists(path))
filePaths.Add(path);
}
}
// Calculate total size
long totalSize = 0;
foreach (var path in filePaths)
{
totalSize += new FileInfo(path).Length;
}
// Check limit (2.5 GB)
if (totalSize > 2.5 * 1024 * 1024 * 1024)
return new SendResult { Success = false, Error = "Total size exceeds 2.5 GB" };
// Create HTML message
string htmlBody = $@"
<h2>Hello {recipientName},</h2>
<p>Please find attached your contract and related documents.</p>
<p><strong>Attached documents:</strong></p>
<ul>
<li>{Path.GetFileName(contractPath)} (Main contract)</li>
{string.Join("", annexPaths?.Select(p => $"<li>{Path.GetFileName(p)}</li>") ?? Array.Empty<string>())}
</ul>
<p>Please return the signed contract at your earliest convenience.</p>";
// Configure arguments
var recipients = new List<RecipientInfo>
{
new RecipientInfo { Email = recipientEmail }
};
Uri endpoint = new Uri("https://www.secure-exchanges.com/_api/0217/0217.asmx");
var args = new MutliRecipientArgs(
endpoint,
_serial,
_apiUser,
_apiPassword,
recipients,
htmlBody,
"Your Contract - Documents to Sign",
password: null,
filesList: null,
SendMethodEnum.onlyEmail,
sendByMyOwnSMTPServer: false,
showSubject: true,
getNotify: true,
culture: "en-CA",
maxOpeningTime: 10,
expirationMinutes: 10080 // 7 days
);
// Attach files by path
args.FilesPath = filePaths;
// Send
var response = MessageHelper.MultiRecipientMessage(args);
if (response.Status == 200)
{
return new SendResult
{
Success = true,
MessageUrl = response.RecipientsAnswer[0].Answer.URL,
FileCount = filePaths.Count,
TotalSize = totalSize
};
}
return new SendResult
{
Success = false,
Error = response.Data
};
}
}
public class SendResult
{
public bool Success { get; set; }
public string MessageUrl { get; set; }
public string Error { get; set; }
public int FileCount { get; set; }
public long TotalSize { get; set; }
}
Method 2: Files in Memory (FileArgs)
Use this method when:
- Files are generated dynamically
- You're working with data streams
- Files are not on disk
Quick Example
// Create FileArgs from in-memory data
var pdfBytes = File.ReadAllBytes(@"C:\temp\document.pdf");
var fileArgs = new FileArgs(pdfBytes, "document.pdf", "application/pdf");
var args = new MutliRecipientArgs(
endpoint, serial, apiUser, apiPassword,
recipients, message, subject,
password: null,
filesList: new List<FileArgs> { fileArgs },
SendMethodEnum.onlyEmail,
false, true, true, "en-CA", 5, 1440
);
Complete Example: PDF Report Generation
using System;
using System.Collections.Generic;
using System.IO;
using SecureExchangesSDK.Helpers;
using SecureExchangesSDK.Models.Args;
using SecureExchangesSDK.Models.Entity;
using SecureExchangesSDK.SecureExchanges;
public class ReportDeliveryService
{
private readonly Guid _serial;
private readonly Guid _apiUser;
private readonly Guid _apiPassword;
public ReportDeliveryService(Guid serial, Guid apiUser, Guid apiPassword)
{
_serial = serial;
_apiUser = apiUser;
_apiPassword = apiPassword;
}
/// <summary>
/// Generates and sends a monthly report
/// </summary>
public SendResult SendMonthlyReport(
string recipientEmail,
string companyName,
byte[] reportPdfBytes,
byte[] dataExcelBytes = null)
{
// Prepare in-memory files
var files = new List<FileArgs>
{
new FileArgs(
reportPdfBytes,
$"Report_{companyName}_{DateTime.Now:yyyy-MM}.pdf",
"application/pdf"
)
};
// Add Excel file if present
if (dataExcelBytes != null && dataExcelBytes.Length > 0)
{
files.Add(new FileArgs(
dataExcelBytes,
$"Data_{companyName}_{DateTime.Now:yyyy-MM}.xlsx",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
));
}
// HTML message
string htmlBody = $@"
<h2>Monthly Report - {companyName}</h2>
<p>Please find attached your monthly report for {DateTime.Now:MMMM yyyy}.</p>
<p>This report contains:</p>
<ul>
<li>Activity summary</li>
<li>Performance indicators</li>
{(dataExcelBytes != null ? "<li>Detailed data (Excel)</li>" : "")}
</ul>
<p>Please contact us if you have any questions.</p>";
// Configuration
var recipients = new List<RecipientInfo>
{
new RecipientInfo { Email = recipientEmail }
};
Uri endpoint = new Uri("https://www.secure-exchanges.com/_api/0217/0217.asmx");
var args = new MutliRecipientArgs(
endpoint,
_serial,
_apiUser,
_apiPassword,
recipients,
htmlBody,
$"Monthly Report {DateTime.Now:MMMM yyyy} - {companyName}",
password: null,
filesList: files, // In-memory files
SendMethodEnum.onlyEmail,
sendByMyOwnSMTPServer: false,
showSubject: true,
getNotify: true,
culture: "en-CA",
maxOpeningTime: 5,
expirationMinutes: 20160 // 14 days
);
var response = MessageHelper.MultiRecipientMessage(args);
return new SendResult
{
Success = response.Status == 200,
MessageUrl = response.Status == 200 ? response.RecipientsAnswer[0].Answer.URL : null,
Error = response.Status != 200 ? response.Data : null
};
}
}
Common MIME Types
| Extension | MIME Type |
|---|---|
.pdf |
application/pdf |
.doc |
application/msword |
.docx |
application/vnd.openxmlformats-officedocument.wordprocessingml.document |
.xls |
application/vnd.ms-excel |
.xlsx |
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
.png |
image/png |
.jpg |
image/jpeg |
.zip |
application/zip |
.txt |
text/plain |
Limits and Recommendations
| Limit | Value |
|---|---|
| Maximum total size | 2.5 GB |
| Number of files | Unlimited (within size limit) |
| File types | All types accepted |
Tip
For large files, use FilesPath rather than FileArgs to avoid loading the entire file into memory.