I'm using FluentFtp
and SmtpClient
to get a file and attach it directly to an email, but the content of the attached file is empty.
Getting file:
public Stream GetFileStream()
{
client = new FtpClient("123.123.123.123", "user", "pass");
client.AutoConnect();
Stream stream = new MemoryStream();
client.DownloadStream(stream,"remotepath");
return stream;
}
Sending mail function
public void SendMail(Stream file)
{
var smtpClient = new SmtpClient("smtp-mail.outlook.com")
{
Port = 587,
Credentials = new NetworkCredential("send@test.com", "password"),
EnableSsl = true,
};
var message = new MailMessage()
{
From = new MailAddress("send@test.com"),
Subject = "Test",
Body = "Test",
IsBodyHtml= true,
};
message.To.Add("receive@test.com");
var attachment = new Attachment(file, "test.txt");
message.Attachments.Add(attachment);
smtpClient.Send(message);
}
I have relied on Attach file existing on FTP server to mail in C# .NET to send attachments, but I am not getting it.