private void btnEnviar_Click(object sender, EventArgs e)
{
try
{
// Crear una instancia de Outlook
Outlook.Application outlookApp = new Outlook.Application();
// Crear un objeto de correo electrónico
Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
// Obtener la firma del correo electrónico
string signature = GetOutlookSignature();
// Establecer la firma en el correo electrónico
mailItem.HTMLBody = signature + "<br><br>";
// Establecer los campos del correo electrónico
mailItem.To = "[email protected]";
mailItem.Subject = "Asunto del correo electrónico";
mailItem.Body = "Cuerpo del correo electrónico";
if (lstAdjuntos.Items.Count != 0)
{
foreach (string filePath in lstAdjuntos.Items)
{
// Adjuntar el archivo al objeto MailItem
mailItem.Attachments.Add(@filePath.ToString());
}
}
// Enviar el correo electrónico
mailItem.Send();
// Liberar recursos
System.Runtime.InteropServices.Marshal.ReleaseComObject(mailItem);
System.Runtime.InteropServices.Marshal.ReleaseComObject(outlookApp);
}
catch (Exception)
{
throw;
}
}
The code that I share already sends the email but it is sent without the signature, which I already have configured in Outlook, well, the button code also uses the following method to obtain the signature.
public static string GetOutlookSignature()
{
// Obtener la ruta de la carpeta de firmas de Outlook
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string signatureFolder = Path.Combine(appDataPath, "Microsoft\Signatures");
// Obtener la firma predeterminada de Outlook
string signatureFile = Path.Combine(signatureFolder, "default.htm");
if (File.Exists(signatureFile))
{
// Leer el contenido del archivo de firma
string signature = File.ReadAllText(signatureFile, Encoding.UTF8);
// Reemplazar las rutas absolutas de las imágenes por rutas relativas
if (signature.Contains("<img"))
{
string baseFolder = signatureFolder;
Regex regex = new Regex("src=["']([^"']*)["']", RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(signature);
foreach (Match match in matches)
{
string absolutePath = match.Groups[1].Value;
if (absolutePath.StartsWith("file://"))
{
absolutePath = absolutePath.Substring(7);
}
if (absolutePath.StartsWith("/"))
{
absolutePath = absolutePath.Substring(1);
}
string relativePath = absolutePath.Replace(baseFolder, ".");
signature = signature.Replace(absolutePath, relativePath);
}
}
return signature;
}
else
{
// No se encontró la firma predeterminada
return string.Empty;
}
}
Library used:
using Outlook = Microsoft.Office.Interop.Outlook;
I am not an advanced programmer yet and I have helped myself from artificial intelligence to get that function that gets the signature but it didn’t really help me, thanks for the help in advance.
1
Answers
The following line completely wipes out the existing body (including the signature)
There is no reason to set the plain text body if you already set
HTMLBody
just a few lines above. Also note that you cannot concatenate two HTML strings and expect a valid HTML back – the two must be merged. Find the position of"<body"
, find the next">"
, and insert your HTML text after that.