// 2. In C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
public class EmailSender
{
private static readonly HttpClient client = new HttpClient();
public async Task SendEmailAsync(string licno, string email, string subject, string content, List filePaths)
{
try
{
using (var formData = new MultipartFormDataContent())
{
// Add form data
formData.Add(new StringContent(licno), "licno");
formData.Add(new StringContent(email), "email");
formData.Add(new StringContent(subject), "Subject");
formData.Add(new StringContent(content), "Content");
// Add files
foreach (var filePath in filePaths)
{
var fileStream = File.OpenRead(filePath);
var fileContent = new StreamContent(fileStream);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "Files",
FileName = Path.GetFileName(filePath)
};
formData.Add(fileContent);
}
// Make the HTTP POST request
var response = await client.PostAsync("https://Custommail.dynode.in/api/mail/sendmail", formData);
// Check the response
if (response.IsSuccessStatusCode)
{
return true;
}
else
{
var responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Error: {responseContent}");
return false;
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
return false;
}
}
}
//Usage
//You can use this function from anywhere in your application to send an email. Here’s an example of how to call it:
public class Program
{
public static async Task Main(string[] args)
{
var emailSender = new EmailSender();
string licno = "your_license_number";
string email = "recipient@example.com";
string subject = "Your Subject";
string content = "Email body content";
List filePaths = new List
{
@"C:\path\to\your\file1.txt",
@"C:\path\to\your\file2.txt"
};
bool isSuccess = await emailSender.SendEmailAsync(licno, email, subject, content, filePaths);
if (isSuccess)
{
Console.WriteLine("Email sent successfully.");
}
else
{
Console.WriteLine("Failed to send email.");
}
}
}
//JavaScript (Fetch API)
//Here's a function using the Fetch API:
async function sendEmail(licno, email, subject, content, files) {
try {
let formData = new FormData();
formData.append('licno', licno);
formData.append('email', email);
formData.append('Subject', subject);
formData.append('Content', content);
for (let i = 0; i < files.length; i++) {
formData.append('Files', files[i], files[i].name);
}
let response = await fetch('https://Custommail.dynode.in/api/mail/sendmail', {
method: 'POST',
body: formData
});
if (response.ok) {
let responseData = await response.json();
console.log('Email sent successfully:', responseData);
return true;
} else {
let errorData = await response.text();
console.error('Failed to send email:', errorData);
return false;
}
} catch (error) {
console.error('Error:', error);
return false;
}
}
//jQuery
//Here's a function using jQuery:
function sendEmail(licno, email, subject, content, files) {
var formData = new FormData();
formData.append('licno', licno);
formData.append('email', email);
formData.append('Subject', subject);
formData.append('Content', content);
for (var i = 0; i < files.length; i++) {
formData.append('Files', files[i], files[i].name);
}
$.ajax({
url: 'https://Custommail.dynode.in/api/mail/sendmail',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function (response) {
console.log('Email sent successfully:', response);
return true;
},
error: function (jqXHR, textStatus, errorThrown) {
console.error('Failed to send email:', textStatus, errorThrown);
return false;
}
});
}
JavaScript (Fetch API) Usage
Send Email
//jQuery Usage
Send Email