First step is to initialize Configuration
object to handle API authentication. The library supports the API Key Header authentication method.
To see your base URL, log in to the Infobip API Resource hub with your Infobip account.
var configuration = new Configuration()
{
BasePath = "<put your base URL here prefixed by https://>",
ApiKey = "<put your API key here>"
};
Now we can initialize Email API client.
var emailApi = new EmailApi(configuration);
We're now ready for sending our first email. Note that response contains BulkId
property which may be useful for checking the status sent emails.
Fields from
, to
and subject
are required, also the message must contain at least one of these: text
, html
or templateId
.
IMPORTANT NOTE: Keep in mind following restrictions while using trial account
- you can only send messages to verified email addresses
- you can only use your emails address with Infobip test domain in following form
[email protected]
string mailTo = "[email protected]";
string mailFrom = "<set your user name>@selfserviceib.com";
string mailText = "This is my first email.";
string mailSubject = "Subject of the mail";
var response = emailApi.SendEmail(
from: mailFrom,
to: new List<string> { mailTo },
subject: mailSubject,
text: mailText
);
string bulkId = response.BulkId;
Example below shows how to send email with attachment.
try
{
string attachmentFilePath = "/temp/report.csv";
using FileStream attachmentFile = new FileStream(attachmentFilePath, FileMode.Open, FileAccess.Read);
EmailSendResponse sendResponse = emailApi.SendEmail(
from: "[email protected]",
to: new List<string>
{
"[email protected]"
},
subject: "Mail subject text",
text: "Test message with file",
attachment: new List<FileParameter>
{
attachmentFile
}
);
attachmentFile.Dispose();
}
catch (Exception ex)
{
// HANDLE EXCEPTION
}
You can also send delayed emails very easily. All you need to define is the desired date of the email delivery as sendAt
parameter of the SendEmail
method.
try
{
string attachmentFilePath = "/temp/report.csv";
DateTimeOffset sendAtDate = new DateTimeOffset(DateTime.UtcNow.AddMinutes(30), TimeSpan.FromHours(0));
using FileStream attachmentFile = new FileStream(attachmentFilePath, FileMode.Open, FileAccess.Read);
EmailSendResponse sendResponse = emailApi.SendEmail(
from: "[email protected]",
to: new List<string>
{
"[email protected]"
},
subject: "Mail subject text",
text: "Test message with file",
attachment: new List<FileParameter>
{
attachmentFile
}
);
attachmentFile.Dispose();
}
catch (Exception ex)
{
// HANDLE EXCEPTION
}
For each message that you send out, we can send you a delivery report in real-time.
All you need to do is specify your endpoint when sending email in notifyUrl
field.
Additionally, you can use our Delivery reports API to fetch reports.
You can filter reports by multiple parameters (see the API's documentation for full list), for example, by bulkId
, bulkId
and limit
like in the snippet below:
try
{
string messageId = "<set messageId>";
string bulkId = "<set bulk id>";
int limit = 10;
EmailReportsResult emailReportsResult = emailApi.GetEmailDeliveryReports(
messageId: messageId,
bulkId: bulkId,
limit: limit
);
}
catch (Exception ex)
{
// HANDLE EXCEPTION
}