I would like some help! I made a class to send e-mails in my application, where reports will be generated to be sent in PDF and XML format, but the e-mail is being sent with the path of the files in the body of the e-mail. and in my code there is no action where it is done.
Does anyone have any idea what it can 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);
}
}
}
}