N
Novu•12mo ago
MFM Tech

Documentation sending email with attachment in C#/.NET with Custom SMPT

Hi, i working on .net for create API that send an email with the attachment, but its not working. im looking for documentation or guidance to work with it. Thank you
6 Replies
empe
empe•12mo ago
Hey @MFM Tech, thanks for bring this up! To send an email with an attachment using Novu in a C#/.NET environment, you would typically use the Novu API. While the exact code for C#/.NET is not provided in the available documentation, the process is generally similar across different programming languages. Based on the provided Node.js example from the Novu documentation, here is what you need to ensure: 1. Initialize the Novu client with your API key. 2. Use the trigger method with the appropriate workflow identifier. 3. Include the attachments array in the payload, with each attachment specified by its metadata (like file, name, and mime type). For C#/.NET, you would need to adapt the process into a corresponding .NET HTTP client call to the Novu API. You can find it here: https://docs.novu.co/channels-and-providers/email/overview#sending-email-attachments I've used ChatGPT to try and adapt to .NET but please check this up and see if it works, and please let me know what would be the correct code snippet so we could include this in our documentation:
C#
using Novu.DTO;
using Novu.Models;
using Novu;

var novuConfiguration = new NovuClientConfiguration
{
Url = "https://api.novu.co/v1/events/trigger",
ApiKey = "12345",
};

var novu = new NovuClient(novuConfiguration);

public class OnboardEventPayload
{
public string Username { get; set; }
public string WelcomeMessage { get; set; }
}

var onboardingMessage = new OnboardEventPayload
{
Username = "jdoe",
WelcomeMessage = "Welcome to novu-dotnet"
};

var payload = new EventTriggerDataDto()
{
EventName = "onboarding",
To = new ToDto { SubscriberId = "subscriberId" },
Payload = onboardingMessage
};

// Assuming you want to send a file in the payload
var fileBytes = File.ReadAllBytes(Path.Combine(Directory.GetCurrentDirectory(), "data", "novu.jpeg"));

using (var stream = new MemoryStream(fileBytes))
{
var fileContent = new StreamContent(stream);
fileContent.Headers.Add("Content-Type", "image/jpeg");

payload.Attachments = new AttachmentDto[]
{
new AttachmentDto
{
File = fileContent,
Name = "novu.jpeg",
Mime = "image/jpeg"
}
};

try
{
var trigger = await novu.Event.Trigger(payload);
Console.WriteLine("Trigger successful!");
}
catch (Exception ex)
{
Console.WriteLine($"Error triggering the event: {ex.Message}");
}
}

var trigger = await novu.Event.Trigger(payload);
C#
using Novu.DTO;
using Novu.Models;
using Novu;

var novuConfiguration = new NovuClientConfiguration
{
Url = "https://api.novu.co/v1/events/trigger",
ApiKey = "12345",
};

var novu = new NovuClient(novuConfiguration);

public class OnboardEventPayload
{
public string Username { get; set; }
public string WelcomeMessage { get; set; }
}

var onboardingMessage = new OnboardEventPayload
{
Username = "jdoe",
WelcomeMessage = "Welcome to novu-dotnet"
};

var payload = new EventTriggerDataDto()
{
EventName = "onboarding",
To = new ToDto { SubscriberId = "subscriberId" },
Payload = onboardingMessage
};

// Assuming you want to send a file in the payload
var fileBytes = File.ReadAllBytes(Path.Combine(Directory.GetCurrentDirectory(), "data", "novu.jpeg"));

using (var stream = new MemoryStream(fileBytes))
{
var fileContent = new StreamContent(stream);
fileContent.Headers.Add("Content-Type", "image/jpeg");

payload.Attachments = new AttachmentDto[]
{
new AttachmentDto
{
File = fileContent,
Name = "novu.jpeg",
Mime = "image/jpeg"
}
};

try
{
var trigger = await novu.Event.Trigger(payload);
Console.WriteLine("Trigger successful!");
}
catch (Exception ex)
{
Console.WriteLine($"Error triggering the event: {ex.Message}");
}
}

var trigger = await novu.Event.Trigger(payload);
MFM Tech
MFM TechOP•12mo ago
Thank you very much for your help. I successfully send email with attachment using novu provider, but when i changed using the smtp to gmail / office 365, its not send the attachment. do you have any information about it ? @Emil
empe
empe•12mo ago
I'm looking into it now Well, unfortunately, it might take me some time to get to the bottom of it. It appears that the issue you're encountering could be related to the configuration or handling of the attachment data within the SMTP setup. To troubleshoot this, please ensure that the attachment is correctly formatted according to the requirements of the SMTP service you're using. For Office 365, it's crucial to set up your SMTP credentials correctly in the Novu dashboard, and it should generally be capable of handling attachments if the integration is configured correctly. What provider are you using for Custom SMTP?
MFM Tech
MFM TechOP•12mo ago
Im testing to send email with attachment using my gmail as SMTP and the default provider which nodemailer i guess
empe
empe•12mo ago
Have you been following this documentation page: https://docs.novu.co/channels-and-providers/email/custom-smtp
Novu
Custom SMTP - Novu
Learn how to use the Custom SMTP provider to send email notifications using Novu
empe
empe•12mo ago
Could you please share more about the error or whatever you can detect? Also consider visiting the Activity Feed section and learn about the triggers that were set in motion and share screenshots here 🙂
Want results from more Discord servers?
Add your server