creating multiple in memory files and attaching them to an email - c#

I need to be able to send textual data stored in a database in multiple row format as txt file attachments in an email. Think Notes added to something by different people at different times.
As of right now the email sends and there are some text files generated like test0.txt and test1.txt but they're empty. I'm flushing the streamwriter so to me it seems like the file should have text in it. I can't close the stream writer before I send the email because then I get an error saying cannot read from closed stream. I'm storing both the memorystream and streamwriter in containers so they shouldn't get disposed of or cleaned up until the very end. I wonder if the email object gets lost or for some reason can't access the streams stored in the containers?
I've looked at some similar questions on SO but they don't seem to work.
This person is using byte[] so only memory stream no streamwriter
This person disposes of their streamwriter before sending email which errors out for me
Instead of trying to do this in memory should I just be writing temp files and then including those as attachments? It seems slow to write to disk just so that I can then read from disk to attach the attachment.
var companyEmail = new MailAddress("address#company.com", "Person Name");
email.To.Add(companyEmail);
email.From = new System.Net.Mail.MailAddress("donotreply#company.com", "doesn't matter");
email.Subject = "subject";
email.Body = "body";
email.IsBodyHtml = true;
var nonAttCounter = 0;
var nonAttStreamHolder = new List<MemoryStream>();
var nonAttWriterHolder = new List<StreamWriter>();
//churn through the attachments and see if any of them are checked in the form
foreach (DataRow datarow in claim.attachments.Rows)
{
string cbFormName = "ctl00$MainBody$att" + datarow["attNum"].ToString();//name of checkbox controls on page and in form.
var includedInForm = rForm[cbFormName];
//see if the attachment was selected as one to include.ie the attNum is in the posted form.
if (includedInForm != null)
{
string origData = datarow["origData"].ToString();
string[] fIDs = origData.Split(',');
foreach (var item in fIDs)
{
//not all attachments are real attachments with files...cause why would attachments be attachments.
int fid;
bool isInt = int.TryParse(item, out fid);
if (isInt)
{
var tempDS = new datastore(ref fid);
var tempData = tempDS.blobData;
email.Attachments.Add(new Attachment(tempData, tempDS.fileNameWithExt));
}
else
{
//grab all the textual data from the database for this "attachment" and write it to a memory stream and upload to the email
nonAttStreamHolder.Add(new MemoryStream());
nonAttWriterHolder.Add(new StreamWriter(nonAttStreamHolder[nonAttCounter]));
nonAttWriterHolder[nonAttCounter].WriteLine("This is a test.");
nonAttWriterHolder[nonAttCounter].WriteLine("Why this no work?!");
nonAttWriterHolder[nonAttCounter].Flush();
//nonAttWriterHolder[nonAttCounter].Close();
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain);
System.Net.Mail.Attachment tempFile = new System.Net.Mail.Attachment(nonAttStreamHolder[nonAttCounter], ct);
tempFile.ContentDisposition.FileName = "test" + nonAttCounter + ".txt";
email.Attachments.Add(tempFile);
nonAttCounter++;
}
}
}
}
Global_Utilities.SharedFunctions.emailQuickSend(email);
foreach (var writer in nonAttWriterHolder)
{ writer.Close(); }
foreach (var stream in nonAttStreamHolder)
{ stream.Close(); }

You can simply take your string(s) and encode them as byte[], then use the techniques outlined in attach multiple files to an email programticaly without writing to disk. Here's some simple code to get you started:
var myString = "This is a test.";
var myBytes = System.Text.Encoding.UTF8.GetBytes(myString);
Now myBytes is an array of bytes. Note that you'll likely want to specify the encoding of the attachment, via the TransferEncoding property.

Related

Send encrypted email with attachments

This seems like a tough one to find a good answer to. I want to create a mail message, add attachments to it, encrypt it using a X509Certificate2 certificate, and then send it. Sounds simple enough, right?! I use asp.net mvc and C#.
This is what I have so far.
string sMTPClient = ConfigurationManager.AppSettings.Get("SMTPClient");
using (var smtpClient = new SmtpClient(sMTPClient))
{
var attachments = MethodToCreateMailAttachments(......);
X509Certificate2 certificate = MethodToGetCertificateBySerialNumber("xxxxxxx");
using (var finalMailmessage = new MailMessage())
{
var encryptedMailMessage = new MailMessage();
var encryptCert = new X509Certificate2(certificate);
encryptedMailMessage.Subject = mailsubject;
encryptedMailMessage.Body = mailBody;
if (attachments.Any())
{
foreach (var item in attachments)
encryptedMailMessage.Attachments.Add(item);
}
byte[] encryptedBodyBytes = Encoding.ASCII.GetBytes(encryptedMailMessage.ToString());
EnvelopedCms Envelope = new EnvelopedCms(new ContentInfo(encryptedBodyBytes));
CmsRecipient Recipient = new CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, encryptCert);
Envelope.Encrypt(Recipient);
byte[] EncryptedBytes = Envelope.Encode();
//Attach the encrypted message as an alternate view.
MemoryStream ms = new MemoryStream(EncryptedBytes);
AlternateView av = new AlternateView(ms, "application/pkcs7-mime; smime-type=signed-data;name=smime.p7m");
finalMailmessage.AlternateViews.Add(av);
finalMailmessage.From = new MailAddress(mailFrom);
foreach (var address in mailTo.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
{
finalMailmessage.To.Add(address);
}
var smtp = new SmtpClient(sMTPClient);
smtp.Send(finalMailmessage);
finalMailmessage.Dispose();
ErrorLogging.log.Debug("Mailmessage sent");
return "";
}
}
What this does is create two MailMessages, one for the things that need to be encrypted, attachments, body and subject. Then I create the message that will be sent. To this I add the first message as an alternate view. This works so far as to encrypt and send the email, and on the recieving end, I get an email with a padlock icon in Outlook.
I can then open the message in Outlook, by importing the certificate. This works. However, next to the padlock icon, in Outlook, I get the attachment paperclip icon, which suggests that there is something attached to the message. But the message is empty. So nothing gets attached apparently. I suspect the adding of the encrypted MailMessage as an alternate view to the other MailMessage, is where I have gone wrong.
I've tried a lot of other things with no luck, and this is the closest I have come to a working solution. I need some input, so does anyone have any suggestions?
I found a working solution. The problem was this part:
byte[] encryptedBodyBytes = Encoding.ASCII.GetBytes(encryptedMailMessage.ToString());
I had to create a memorystream, which can then be converted into a byte array. I used a tool called "MimeKit", which can be installed as a nuget package.
So, instead I have:
var memStream = new MemoryStream();
var mimeMessage = MimeMessage.CreateFromMailMessage(encryptedMailMessage);
mimeMessage.WriteTo(memStream);
var messageString = Encoding.UTF8.GetString(memoryStream.ToArray());
byte[] encryptedBodyBytes = Encoding.ASCII.GetBytes(messageString);
The rest is the same.

Is there a way to Extract attachment from Email Message Mime Content without using Exchange Service?

I am having a client application which will save eml files to the local disk. Need to get the attachment inside the eml file which is saved without using the exchange service because the Mailbox keeps changing of its capacity.Please help if anyone have come across similar issue
I have tried the reverse process of getting the eml file and load it again to get the details.
You could use something like MimeKit for this. The GitHub page has examples on how to parse MIME messages and how to get attachments.
Here is an example of how to get the attachments in an array of bytes:
var mimeMessage = MimeMessage.Load(#"test.eml");
var attachments = mimeMessage.Attachments.ToList();
foreach (var attachment in attachments)
{
using (var memory = new MemoryStream())
{
if (attachment is MimePart)
((MimePart)attachment).Content.DecodeTo(memory);
else
((MessagePart)attachment).Message.WriteTo(memory);
var bytes = memory.ToArray();
}
}
First of all Big Thanks to MadDev for helping out !!!
Here is the Code which I used:
Note: Here in case, the stored email will always have another eml file attached to it and this is based on the business logic.
protected static void MimeProcessor(MemoryStream stream)
{
try
{
var parser = new MimeParser(stream, MimeFormat.Default);
var message = parser.ParseMessage();
var multipart = message.Body as Multipart;
//Found the Attachment as Message Part
var OriginalMessage = multipart.ToList().LastOrDefault();
if (OriginalMessage is MessagePart)
{
using (var memory = new MemoryStream())
{
((MessagePart)OriginalMessage).Message.WriteTo(memory);
var bytes = memory.ToArray();
File.WriteAllBytes("C:\\Test\\TestMessage.eml", bytes);
}
}
}
catch (Exception)
{
throw;
}
}

Sending a csv through email gives me different result than from a file

I have a tabular data that I want to send out using email. I can successfully send and receive the email so that is no problem. The problem is the attachment to the email.
I have attached a csv along with it. The csv is created from a Stream Writer. When I open this file, I get a different result that if I created the file from the stream. If I set the stream position to 0 before adding the stream, the csv file would only write 21/30 lines. If I don't reset the posiiton, it would write no lines. When I look at the file that I created, all the entries seems normal and as expected.
Why is that?
using (FileStream memstream = File.Create(#"C:\Users\home\Desktop\" + "test.csv"))
{
using (StreamWriter writer = new StreamWriter(memstream))
{
data.ForEach(line =>
{
writer.WriteLine(string.Join(",", line));
});
MailMessage message = new MailMessage();
message.To.Add("home#local.com");
message.Subject = "Locations sent to home on " + DateTime.Today.AddDays(-1).ToString("MM-dd-yy");
message.From = new MailAddress("areport#reports.com");
memstream.Position = 0;
Attachment attachment = new Attachment(memstream, "report.csv");
message.Attachments.Add(attachment);
SmtpClient client = new SmtpClient("outbound#out.com");
client.Send(message);
}
}
Most likely your issues are because it's attempting to send the email before you've closed the file stream. Try sending after you've closed the file stream.
As #jtimperley mentioned in the comments your StreamWriter is not being Flushed before you send the email so some contents are not written out.
Calling Flush on the writer should fix it.
writer.Flush();
memstream.Position = 0;
Attachment attachment = new Attachment(memstream, "report.csv");
message.Attachments.Add(attachment);

File are corrupted when Attaching them to MailMessage C#

I have created an application at work that generates exel files from some database data. After generating the files they are sent automatically to the customers in question. My problem is that it works fine when i run the published application. But some users when they run the application the files are generated perfectly as they are saved on the HDD and i can see them. But when they are attached to the MailMessage object they get corrupted. This is an image of the corrupted files. These files should be Excel files.
This is my code for sending a mail with attached files:
public void SendMailedFilesDK()
{
string[] sentFiles = Directory.GetFiles(sentFilesDK);
if (sentFiles.Count() > 0)
{
using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("ares"))
{
using (System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage())
{
msg.From = new MailAddress("system#mail.dk");
msg.To.Add(new MailAddress("operation#mail.dk"));
msg.To.Add(new MailAddress("bl#mail.dk"));
msg.CC.Add("lmy#mail.dk");
msg.CC.Add("ltr#mail.dk");
msg.Subject = "IBM PUDO";
msg.Body = sentFiles.Count() + " attached file(s) has been sent to the customer(s) in question ";
msg.IsBodyHtml = true;
foreach (string file in sentFiles)
{
Attachment attachment = new Attachment(file);
msg.Attachments.Add(attachment);
}
client.Send(msg);
}
}
}
}
Why are the files getting corrupted when others run the application? We are all using office 2010.
You should make sure to set the content type of the attachement to the appropriate value.
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet for xlsx files, or
application/vnd.ms-excel for xls files.
For example, your loop should look something like this.
ContentType xlsxContent = new ContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
foreach (string file in sentFiles)
{
Attachment attachment = new Attachment(file, xlsxContent);
msg.Attachments.Add(attachment);
}
We use this in our Attachment constructor and have no issues attaching Excel and PDF.
Attachment data = new Attachment(sFileName, MediaTypeNames.Application.Octet);
Also check that the users running this have permissions to access the files in whatever location is specified by sentFilesDK.
You might want to specify the mimetype which is part of one of the constructors on Attachment class.
public Attachment(string fileName, ContentType contentType);
You can also read the file in memorystream and pass it as part of the following constructor.
public Attachment(Stream contentStream, string name, string mediaType);

Attaching Multiple Files To An E-Mail From Database Image Column In .NET

are there any good examples of how to attach multiple files from a database to an e-mail in .NET? I've got a method that returns a Byte[] containing the Image column contents that I am calling in a loop to get each attachment, but I was wondering if there was a "correct"/best-practice way of doing this, especially with the possibility of introducing memory leaks by using MemoryStreams to contain the data? I'm fine creating en e-mail object and attaching the list of attachments to it, once I've got them and can do this fine with a single attachement but it seems to get slightly more complex with multiple files. Considering I wouldn't have thought this was an unusual requirement, there seems to be a dearth of articles/posts about it.
Thx - MH
Here's how to proceed. Let's suppose that you have an array of attachments that you have loaded from your database:
IEnumerable<byte[]> attachments = ... fetch from your database
We could also safely assume that along with those attachments you have loaded the filenames and probably their corresponding MIME type (information that you surely must have persisted along with those byte arrays representing your attachments). So you will probably have fetched IEnumerable<SomeAttachmentType> but that's not important for the purpose of this post.
So now you could send the mail:
using (var client = new SmtpClient("smtp.foo.com"))
using (var message = new MailMessage("from#foo.com", "to#bar.com"))
{
message.Subject = "test subject";
message.Body = "test body";
message.IsBodyHtml = false;
foreach (var attachment in attachments)
{
var attachmentStream = new MemoryStream(attachment);
// TODO: Choose a better name for your attachments and adapt the MIME type
var messageAttachment = new Attachment(attachmentStream, Guid.NewGuid().ToString(), "application/octet-stream");
message.Attachments.Add(messageAttachment);
}
client.Send(message);
}
Here's the deal:
A MailMessage (IDisposable) contains multiple Attachments (IDisposable). Each attachment references a MemoryStream (IDisposable). The MailMessage is wrapped in a using block which ensures that its Dispose method will be called which in turn calls the Dispose method of all attachments which in turn call the Dispose method of the memory streams.
Hi you can have buffered reads directly from the database, MemoryStream does NOT introduce any memory leak if you dispose it after usage. Example using SqlDataReader:
using(var stream = new MemoryStream())
{
byte[] buffer = new byte[4096];
long l, dataOffset = 0;
while ((l = reader.GetBytes(columnIndex, dataOffset, buffer, 0, buffer.Length)) > 0)
{
stream.Write(buffer, 0, buffer.Length);
dataOffset += l;
}
// here you have the whole stream and can attach it to the email...
}
similar question on how to read bytes from database has been asked already countless times, see here for example: What is the most efficient way to read many bytes from SQL Server using SqlDataReader (C#)

Categories