Hi
I have below code . I want to send attachment also,
How to send attachment using c# sdk in sap b1 Hana
C#
using (var message = new MailMessage(fromAddress.ToString(), address)
{
Subject = subject,
Body = body,
IsBodyHtml = true,
Priority = MailPriority.High,
Attachments = "10.0.1.30\\SAP_Attachments\\"
})Thanks

Emily FosterRank #3,495 · 415 reputationPosted Mar 15, 2025, 4:17 AM EDT
When sending email attachments using C# with the SAP Business One (SAP B1) SDK in Hana, there are a few adjustments needed in your code snippet to correctly include attachments. The `MailMessage` class in C# doesn't have an `Attachments` property; instead, you need to utilize the `Attachments` collection. Here's an updated version of your code snippet to send an email with attachments:
using System.Net.Mail; // Create a new MailMessage instance MailMessage message = new MailMessage(fromAddress.ToString(), address) { Subject = subject, Body = body, IsBodyHtml = true, Priority = MailPriority.High }; // Add attachment paths to the Attachments collection message.Attachments.Add(new Attachment(@"10.0.1.30\SAP_Attachments\file.txt")); message.Attachments.Add(new Attachment(@"10.0.1.30\SAP_Attachments\image.jpg")); // Send the email using (SmtpClient smtpClient = new SmtpClient("yourSMTPserver")) { smtpClient.Send(message); }In this updated code snippet:
- The `MailMessage` object `message` is created with the necessary attributes like subject, body, etc.
- Attachments are added to the `Attachments` collection using the `Add` method, specifying the file paths.
- Ensure to replace `"yourSMTPserver"` with the correct SMTP server address for sending the email.
By following this structure, you can successfully send emails with attachments in C# using the SAP B1 SDK in Hana. This approach aligns with best practices for attaching files to emails programmatically. If you have specific file types or requirements, you can adjust the attachment paths accordingly within the `Add` method.