Open mail client from browser with an byte[] attachment from the server - c#

I looked on SO an found several ways to do kind simmilar things, but those aren't the solution.
A user is on the web page and on a click on a file (which is stored in byte[] on server) should open the mail client, where the attachment, subject and mailfrom is set.
Subject and Mail From is no problem, but the attachment. Here are some points I need/need not to:
I cannot/I am not allowed to Create a file such as mymailmessage.eml
on a client (access denied)
I cannot use MailMessage
(System.Net.Mail) because for this I need to create a file to open the mail client with Process.Start(filename)
I cannot use mailto: because there I cannot add an attachment
So the way I am on for now is to use MAPI. Unfortunately I have not found a solution. My source is a byte[] and I can write this in a MemoryStream. Is it possible to create a kind of virtual file path in C#? But I don't know how to open the mail client including the parameters. All the MAPI projects (like https://github.com/PandaWood/Simple-MAPI.NET or https://www.codeproject.com/Articles/10881/MAPIEx-Extended-MAPI-Wrapper are using local files. I tried those, but in my case the mail client isn't starting, so no mail message window is popping up.
So what I am looking for is: Create a MapiMessage with MailFrom, Subject, an attachment and then open the mail client (like with mailto) including the attachment.
An other C# project is this: https://www.codeproject.com/Articles/17561/Programmatically-adding-attachments-to-emails-in-C
According to this codeproject, I cannot find the line, where the programm is saying the mail client please open (like Process.Start()) and open the prepared mail message for me.
Is there any other way except MAPI, when I only have a byte[] in the web application and need to open a mail client message including the attachment not storing/downloading it on the user client?

Related

How do I open the default mail client and create an email?

I want to open the default mail client registering on Windows and create an email with an attachment.
The behaviour is just like when you right click on a file, the Send to -> Mail Recipient. Or when you compressing a folder and select 'compress and email' on the context menu.
I have tried:
mailto:xxx#example.com, which doesn't accept attachment.
MAPI, which works well when the outlook is shutdown, but doesn't work when the outlook is currently working. Do you know how to make it work?
Do you have any other ideas?
Thanks!
Depending on the version of your mail client, try to add &attach="C:\some_file.txt to the end of mailto:xxx#xxx.com. This should add an attachment when the mail client opens. If attach does not work, try attachment in stead. I wonder if you can't add both.

how to read mail from Google mail and mark as save and save mail in local folder with C# .NET

I am trying to build a winform app to
read mails from google app
save mail and attachment to local folder
mark mail as read
Save the email body and attachments to a SQL-Server database
The latter is ok but I'm really having trouble with the first 3.
I've look everywhere and people are telling me how to send mail on a winform app but not how to read, mark and save content and attachment.
CodeProject is probably the next best resource online after SO (IMHO.) Here are some well-reviewed sample projects that will get you a long way:
http://www.codeproject.com/Articles/14304/POP3-Email-Client-NET-2-0
http://www.codeproject.com/Articles/6062/A-POP3-Client-in-C-NET
http://www.codeproject.com/Articles/188349/Read-Gmail-Inbox-Message-in-ASP-NET
http://www.codeproject.com/Articles/34495/Building-your-own-Mail-Client-using-C
http://www.codeproject.com/Articles/15611/POP3-Email-Client-with-full-MIME-Support-NET-2-0

Saving an auto-generated email? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to save MailMessage object to disk as *.eml or *.msg file
I am working on a C# program that emails people when certain conditions are met. I want to be able to save a copy of the email for record keeping and can't figure it out. I find it surprising there is just not a built it function like message.Save().
I have included a very basic email sample below:
MailMessage message = new MailMessage("from_email", "to_email");
message.Subject = "Email Alert";
message.Body = "This is a test email.";
SmtpClient Smtp = new SmtpClient("smtp server");
Smtp.Send(message);
I would like to save a copy of the email for a record. I didn't really consider all the choices of ways to store the message, sorry for that. I would like to have copy in case the recipient didn't receive the email I could forward them a copy from archive. I think a .msg would work well.
Another addition, I would like to be able to save the email and then send a batch at the end of the day. In case I receive updates that needed to be added I could have to program add new entries to the email so that recipient wouldn't be overloaded with multiple emails. However, there would be some cases where their would be escalation level when met an email would automatically be sent regardless of the time of day.
Why not BCC the email to an admin account?
Well, you weren't too specific about what you were looking for, so here are a few options:
BCC yourself. This will (privately) send yourself a copy of the email.
Implement a save yourself if you want to save to file. It's not that hard. Really all you want to do is save a few bits of text. We could implement it like this:
private void SaveEmailToDisk(MailMessage message, string saveTo)
{
var builder = new StringBuilder();
builder.AppendFormat("To: {0}\n", String.Join("; ", message.To.Select(m => m.Address).ToArray()));
builder.AppendFormat("From: {0}\n",message.From.Address);
builder.AppendFormat("Subject: {0}", message.Subject);
builder.AppendFormat("Body: {0}", message.Body);
File.WriteAllText(saveTo, builder.ToString());
}
Of course you can tweak it to whatever needs you have.
A couple of different ways to "back up" your email messages so they could be resent if necessary:
ProcMail. Depending on the MTA you're using, it would be easy enough to write a ProcMail recipe to archive messages as your MTA sends them. If you're using Exchange, the same can be done on the server side of things.
XML Serialization. After you create each instance of the MailMessage class, serialize it and store it, either in the file system, or in a database. Should be easy enough to rehydrate instance when needed.
Pickup Directory. The SmtpClient class can be configured to "send" messages to a "Pickup Directory." This is normally used in a configuration where the MTA (message transport agent) is configured to watch a particular directory. Sending mail then consists of dropping a file containing an RFC 2822-compliant message into the directory, where it will shortly get collected by the MTA and sent on its way. If no MTA is configured to watch the pickup directory, the mail message will just get dropped there and sit.
This is a useful way of testing the app that does the mailing without involving a real MTA. People tend to get grumpy when they get slammed with junk messages.
It's also a useful technique for archiving: Configure 2 SmtpClient instances in your program: one configured to talk to your MTA and the other configured to drop the message in a pickup directory. Post each MailMessage you create to both instances and you'll have your archive.
Any one of these techniques should work for you. If you actually need to re-send the email, XML serialization might be the best option for you, as rehydrating an object instance is pretty trivial to do via XML serialization.
The important question to ask here is: Save it to where?
That's why there is not a built-in Save() method. Emails are not typically something that is easily just saved to a file-system (that's not to say they cannot be). But there is a lot of information that is not simply stored, like the To/From address, the Subject line, different parts (ie. MIME alternate parts, attachments).
Use
MailAddress bcc = new MailAddress("youremail#domain.com");
message.Bcc.Add(bcc);
You'll get a copy of the message.
Why not write the data to a database table before sending the email? Then, you have a log of what email was sent.

C# and Lotus notes attachment icon

I am using C# and need to attach a lnk (shortcut/link) file and email it to a lotus notes account. The problem is the icon defaults to a generic image and does not show the folder with the arrow as seen in Windows Explorer. A txt file icon does display as expected. I would like to control the appearance of that icon after it has been received. Can this be done through C# or is the Notes server the problem?
I think you are asking how to control the appearance of the icon once it arrives in the recipient's mail.
I do remember (from when we were on Notes here at work) that the icon was specific to the sender as opposed to the recipient (i.e. if I send you a Word document and I have Word 97 installed and you have 2003 installed, you'll see the Word 97 doc icon). But this appeared to be a function of the fact that I, the sender, was using Notes as the sending client. However, I think you may not be able to do what you want to do as one would assume you are sending regular SMTP e-mail. Assuming you are sending e-mail through the Notes server's SMTP interface, I don't believe you'll be able to control how the link appears on the recipient's side.

Send eml files saved on disk

I am creating eml's and saving them to a directory using procedure mentioned over here.
I want to know how to send these eml files?
I tried using SMTPClient class's object but it takes MailMessage object as its parameter and I couldn't find and way to create an object of type MailMessage using these saved eml files.
Loading an EML file correctly is not as easy as it looks. You can write an implementation working in 95% cases within few days. Remaining 5% would take at least several months ;-). I know, becase I involved in developing one.
Consider following dificulities:
unicode emails
right-to-left languages
correcting malformed EML files caused by well known errors in popular mail clients and servers
dealing with S/MIME (encrypted and signed email messages)
dealing correctly with several methods of encoding attachments
dealing with inline images and stylesheets embedded into HTML emails
making sure that it parses correctly a MIME torture message from Mike Crispin (coauthor of Mime and IMAP RFCs)
making sure that malformed message will not result in buffer overun or other application crash
handling hierarchical messages (message with attached messages)
making sure that it handles correctly very big emails
Maturing of such parser takes years and continuous feedback for it's users. Right now is no such parser included in the .NET Framework. Until it changes I would sugest getting a thrid party MIME parser from an established vendor.
Following code uses our Rebex Secure Mail component, but I'm sure that similar task could be replicated easily with components from other vendors as well.
The code is based on Mail Message tutorial.
// create an instance of MailMessage
MailMessage message = new MailMessage();
// load the message from a local disk file
message.Load("c:\\message.eml");
// send message
Smtp.Send(message, "smtp.example.org");
Use EMLReader to retrieve data from .eml file. It contains all the data you need to create a MailMessage object like From, To, Subject, Body & a whole lot more.
FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite);
EMLReader reader = new EMLReader(fs);
fs.Close();
MailMessage message = new System.Net.Mail.MailMessage(reader.From, reader.To, reader.Subject, reader.Body);
If you're a Microsoft shop and have an Exchange server anyway, then there's another solution which is much, much easier than everything else suggested here:
Each Exchange server has a pickup directory configured out of the box.
By default, it's %ExchangeInstallPath%TransportRoles\Pickup.
You just copy the .eml files to that directory, and Exchange automatically will send the mails.
Read this TechNet article for more information:
Pickup directory and Replay directory
As others demonstrated, EML is just not a good way to serialize a mail message. You might be better off by saving your mails in another format. While there are several serialization engines in the .Net framework to serialize any object, you might also consider just saving the components of your mails, like addresses, body, files to be attached in base64, in an Xml file of your own design.
Below is an example to get you started:
<?xml version="1.0" encoding="utf-8"?>
<mail>
<to display="Thomas Edison" address="tedison#domain.com" />
<body>
Hi Thomas,
How are you doing?
Bye
</body>
<attachment name="MaryLamb.wav">
cmF0aWUgYWFuIGluIFBERi1mb3JtYWF0LiBEZSBmYWN0dXVyIGlzIGVlbiBvZmZpY2ll
ZWwgZ2VzaWduZWVyZA0KZG9jdW1lbnQgdmFuIEV1cm9maW5zIE9tZWdhbSBCVi4gRGUg
c2lnbmF0dXJlIGt1bnQgdSB2ZXJpZmnDq3Jlbi4NCg0KVm9vciBoZXQgdmVyaWZpw6ty
...
</attachment>
</mail>
Added advantage would be that, unlike with creating EML, you do not need the smtpClient to build the concept mail files.
Xml is extremely easy to create and parse in C#.
You did not tell the rationale of saving EML's. If long term archival would be a goal, xml might have an advantage.
Do What i did ... give up.
Building the MailMessage object seems to be the focus i have a similar questions outstanding on here too ...
How do i send an email when i already have it as a string?
From what i've seen the simplest way to do this is to use a raw socket to dump the entire .eml file contents up to the mail server as is and let the mail server figure out the hard stuff like from, to subject, ect by parsing the email using it's engine.
The only problem ... RFC 821 ... such a pain, i'm trying to figure out a clean way to do this and read mail already in the mailbox quickly too.
EDIT:
I found a clean solution and covered it in my thread :)
How do i send an email when i already have it as a string?
You can do this with Windows Server’s built-in SMTP server, the same way as in the previous answer using Exchange.
Drop the .eml file to C:\inetpub\mailroot\Pickup and the raw message will be sent (local or remote).
You can forward messages by simply inserting a line in the top:
To: email#address.com
You can manipulate the mail header further if you require.
For the records:
In Nuget Packager Console write:
Install-Package LumiSoft.Net.dll
Then in your Code:
using (FileStream fs = new FileStream( cacheFileName, FileMode.Open, FileAccess.Read ))
using (LumiSoft.Net.SMTP.Client.SMTP_Client client =
new LumiSoft.Net.SMTP.Client.SMTP_Client())
{
client.SendMessage( fs );
}

Categories

Resources