Sending Email is leaving the attachment path in the body of the email

Asked

Viewed 108 times

0

I’d like some help! I made a class to send emails in my application, where reports will be generated to be sent in PDF and XML format, but the email is being sent with the path of the files in the body of the email. and in my code there is no action where it is done.

Does anyone have any idea what it might be?

using System;
using CSharpUtil.Validation;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Windows.Forms;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.IO;

namespace CSharpUtil.Services
{
    public class Email
    {
        protected MailMessage Mail = new MailMessage();

        public bool Send(string fromName, string from, string to, string cc, string co, string subject, string body,
                         string attachments, string user, string password, string smtpHost, int port, int autentication, int enableSsl)
        {

            if (!AssertConsern.AssertArgumentIsNullorEmpty(from.Trim(),
                    "Por favor informe o e-mail do remetente!", "Envio de Email")) return false;
            if (!AssertConsern.AssertArgumentIsNullorEmpty(password,
                    "Por favor informe a senha do e-mail do remetente!", "Envio de Email")) return false;
            if (!AssertConsern.AssertArgumentIsNullorEmpty(smtpHost,
                    "Por favor informe DNS do provedor SMTP/IMAP do e-mail do remetente!", "Envio de Email")) return false;
            if (!AssertConsern.AssertArgumentIsNullorEmpty(port,
                    "Por favor informe a porta do provedor SMTP/IMAP do e-mail do remetente!", "Envio de Email")) return false;
            if (!AssertConsern.AssertArgumentIsNullorEmpty(new object[] { to, cc, co },
                    "Por favor informe o e-mail do destinatário!", "Envio de Email")) return false;

            try
            {

                Mail.From = new MailAddress(from.Trim(), fromName.Trim());
                EmailAdd(to.Trim());
                EmailAdd(cc.Trim());
                EmailAdd(co.Trim());
                Mail.Subject = subject.Trim();
                Mail.SubjectEncoding = System.Text.Encoding.UTF8;
                Mail.Body = body.TrimEnd();
                Mail.BodyEncoding = System.Text.Encoding.UTF8;
                Mail.IsBodyHtml = true;
                Mail.Priority = MailPriority.High;
                attachments = attachments.Trim();

                if (!string.IsNullOrEmpty(attachments))
                {
                    char[] characters = new[] { ';', ','};

                    foreach (var attachment in attachments.Split(characters))
                   {
                        if (!File.Exists(attachment.Trim())) continue;

                        FileStream fileStream = new FileStream(attachment.Trim(), FileMode.Open, FileAccess.Read);
                        Attachment data = new Attachment(fileStream, Path.GetFileName(attachment));
                        Mail.Attachments.Add(data);

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Falha na tentativa de criação do e-mail! " +
                               "Verifique as configurações e tente novamente!\n\n" + ex.Message, "Envio de E-mail");
                return false;
            }


            try
            {
                var smtp = new SmtpClient(smtpHost, port);
                smtp.EnableSsl = (enableSsl == 1 ? true : false);
                if (autentication > 0)
                    smtp.Credentials = new NetworkCredential(user.Trim(), password.Trim());

                smtp.Send(Mail);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Falha no envio do e-mail! " +
                                "Verifique as configurações e tente novamente!\n\n" + ex.Message,"Envio de Email");
                return false;
            }
        }

        private void EmailAdd(string email)
        {
            if (!string.IsNullOrEmpty(email))
            {
                foreach (var sTo in email.Split(';')
                    .Where(sTo => sTo != null && email.Trim() != ""))
                    Mail.To.Add(sTo);
            }
        }
    }
}

inserir a descrição da imagem aqui

  • As the variable body is being filled in? What if you change, just for testing, instead of passing the variable body real spend a constant, as "eu sou um corpo de teste" to see if the behavior remains the same?

  • @Jeffersonquesado thanks for the tip! I discovered the problem. and really the problem was in the data entry. What happens is that this email class was written in C# to be consumed by another legacy language through a DLL COM generated by Nuget "Unmanaged Exports". To summarize the novel the problem was in a language BUG that was consuming the DLL and until now did not understand why it was concatenating the value of the two variables... but solved otherwise bypassing this problem. In short, it was legacy language stick!

  • In case you are curious to know what language I am speaking, I am using softvelocity Clarion 5.5.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.