I'm working on an application that's supposed to take a link to an email from Outlook and store it in a database.
I've been looking at the Microsoft.Office.Interop.Outlook API but I can't find something that could be used for this.
Any thoughts?
You could save the email as a .msg file and then save that into the database as a byte array instead?
Otherwise there are ways to programmtically access a mailbox or outlook .pst file, you would then have to write an interface that lets them select the email to save, and then save the email in parts (subject, to, from etc.) separately in to the database.
To access a mailbox on the Exchange server (Exchange 2007+) you can do it using the Exchange Web Services Managed API 1.0. EWS API and you can download it from here
It makes it really simple to access and retrieve emails etc as pre Exchange 2007 it was a pain and involved parsing a lot of XML or using CDOEXM.
Heres an example of how to use it:
You first need to create an Exchange service. Add a reference to the EWS and add the using line below.
using Microsoft.Exchange.WebServices.Data;
...
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.AutodiscoverUrl( "First.Last#MyCompany.com" );
Once the service is up and running you can then use it to query the mailbox:
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox,
new ItemView());
This will return all the emails in the Inbox. You can then view details of the email using its properties. i.e. item.Subject;
If the emails are in a .pst file however, you will need to use the Outlook API or I recommend PST.NET (although you do have to but a license) as it makes it a lot easier.
Heres an example of using PST.NET:
using System;
using Independentsoft.Pst;
namespace Sample
{
class Program
{
static void Main(string[] args)
{
PstFile file = new PstFile("c:\\testfolder\\Outlook.pst");
using (file)
{
Folder inbox = file.MailboxRoot.GetFolder("Inbox");
if (inbox != null)
{
ItemCollection items = inbox.GetItems();
for (int m = 0; m < items.Count; m++)
{
if (items[m] is Message)
{
Message message = (Message)items[m];
Console.WriteLine("Id: " + message.Id);
Console.WriteLine("Subject: " + message.Subject);
Console.WriteLine("DisplayTo: " + message.DisplayTo);
Console.WriteLine("DisplayCc: " + message.DisplayCc);
Console.WriteLine("SenderName: " + message.SenderName);
Console.WriteLine("SenderEmailAddress: " + message.SenderEmailAddress);
Console.WriteLine("----------------------------------------------------------------");
}
}
}
}
Console.WriteLine("Press ENTER to exit.");
Console.Read();
}
}
}
There is no such thing as a link to an email in Outlook.
I suppose that you'd like to store something like shortcut in a text format which can be used later to find/open an email in Outlook. If you plan to use Outlook Interop API for that, you can use EntryID of the MailItem object that represent your email. It's a unique ID of the item, but it can be changed if item is moved somewhere else in the folder structure.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I am looking for a method of reading emails using Pop3 in C# 2.0. Currently, I am using code found in CodeProject. However, this solution is less than ideal. The biggest problem is that it doesn't support emails written in unicode.
I've successfully used OpenPop.NET to access emails via POP3.
downloading the email via the POP3 protocol is the easy part of the task. The protocol is quite simple and the only hard part could be advanced authentication methods if you don't want to send a clear text password over the network (and cannot use the SSL encrypted communication channel). See RFC 1939: Post Office Protocol - Version 3
and RFC 1734: POP3 AUTHentication command for details.
The hard part comes when you have to parse the received email, which means parsing MIME format in most cases. You can write quick&dirty MIME parser in a few hours or days and it will handle 95+% of all incoming messages. Improving the parser so it can parse almost any email means:
getting email samples sent from the most popular mail clients and improve the parser in order to fix errors and RFC misinterpretations generated by them.
Making sure that messages violating RFC for message headers and content will not crash your parser and that you will be able to read every readable or guessable value from the mangled email
correct handling of internationalization issues (e.g. languages written from righ to left, correct encoding for specific language etc)
UNICODE
Attachments and hierarchical message item tree as seen in "Mime torture email sample"
S/MIME (signed and encrypted emails).
and so on
Debugging a robust MIME parser takes months of work. I know, because I was watching my friend writing one such parser for the component mentioned below and was writing a few unit tests for it too ;-)
Back to the original question.
Following code taken from our POP3 Tutorial page and links would help you:
//
// create client, connect and log in
Pop3 client = new Pop3();
client.Connect("pop3.example.org");
client.Login("username", "password");
// get message list
Pop3MessageCollection list = client.GetMessageList();
if (list.Count == 0)
{
Console.WriteLine("There are no messages in the mailbox.");
}
else
{
// download the first message
MailMessage message = client.GetMailMessage(list[0].SequenceNumber);
...
}
client.Disconnect();
HOWTO: Download emails from a GMail account in C# (blogpost)
Rebex Mail for .NET (POP3/IMAP client component for .NET)
Rebex Secure Mail for .NET (POP3/IMAP client component for .NET - SSL enabled)
My open source application BugTracker.NET includes a POP3 client that can parse MIME. Both the POP3 code and the MIME code are from other authors, but you can see how it all fits together in my app.
For the MIME parsing, I use http://anmar.eu.org/projects/sharpmimetools/.
See the file POP3Main.cs, POP3Client.cs, and insert_bug.aspx
You can also try Mail.dll mail component, it has SSL support, unicode, and multi-national email support:
using(Pop3 pop3 = new Pop3())
{
pop3.Connect("mail.host.com"); // Connect to server and login
pop3.Login("user", "password");
foreach(string uid in pop3.GetAll())
{
IMail email = new MailBuilder()
.CreateFromEml(pop3.GetMessageByUID(uid));
Console.WriteLine( email.Subject );
}
pop3.Close(false);
}
You can download it here at https://www.limilabs.com/mail
Please note that this is a commercial product I've created.
call me old fashion but why use a 3rd party library for a simple protocol. I've implemented POP3 readers in web based ASP.NET application with System.Net.Sockets.TCPClient and System.Net.Security.SslStream for the encryption and authentication. As far as protocols go, once you open up communication with the POP3 server, there are only a handful of commands that you have to deal with. It is a very easy protocol to work with.
I wouldn't recommend OpenPOP. I just spent a few hours debugging an issue - OpenPOP's POPClient.GetMessage() was mysteriously returning null. I debugged this and found it was a string index bug - see the patch I submitted here: http://sourceforge.net/tracker/?func=detail&aid=2833334&group_id=92166&atid=599778. It was difficult to find the cause since there are empty catch{} blocks that swallow exceptions.
Also, the project is mostly dormant... the last release was in 2004.
For now we're still using OpenPOP, but I'll take a look at some of the other projects people have recommended here.
HigLabo.Mail is easy to use. Here is a sample usage:
using (Pop3Client cl = new Pop3Client())
{
cl.UserName = "MyUserName";
cl.Password = "MyPassword";
cl.ServerName = "MyServer";
cl.AuthenticateMode = Pop3AuthenticateMode.Pop;
cl.Ssl = false;
cl.Authenticate();
///Get first mail of my mailbox
Pop3Message mg = cl.GetMessage(1);
String MyText = mg.BodyText;
///If the message have one attachment
Pop3Content ct = mg.Contents[0];
///you can save it to local disk
ct.DecodeData("your file path");
}
you can get it from https://github.com/higty/higlabo or Nuget [HigLabo]
I just tried SMTPop and it worked.
I downloaded this.
Added smtpop.dll reference to my C# .NET project
Wrote the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmtPop;
namespace SMT_POP3 {
class Program {
static void Main(string[] args) {
SmtPop.POP3Client pop = new SmtPop.POP3Client();
pop.Open("<hostURL>", 110, "<username>", "<password>");
// Get message list from POP server
SmtPop.POPMessageId[] messages = pop.GetMailList();
if (messages != null) {
// Walk attachment list
foreach(SmtPop.POPMessageId id in messages) {
SmtPop.POPReader reader= pop.GetMailReader(id);
SmtPop.MimeMessage msg = new SmtPop.MimeMessage();
// Read message
msg.Read(reader);
if (msg.AddressFrom != null) {
String from= msg.AddressFrom[0].Name;
Console.WriteLine("from: " + from);
}
if (msg.Subject != null) {
String subject = msg.Subject;
Console.WriteLine("subject: "+ subject);
}
if (msg.Body != null) {
String body = msg.Body;
Console.WriteLine("body: " + body);
}
if (msg.Attachments != null && false) {
// Do something with first attachment
SmtPop.MimeAttachment attach = msg.Attachments[0];
if (attach.Filename == "data") {
// Read data from attachment
Byte[] b = Convert.FromBase64String(attach.Body);
System.IO.MemoryStream mem = new System.IO.MemoryStream(b, false);
//BinaryFormatter f = new BinaryFormatter();
// DataClass data= (DataClass)f.Deserialize(mem);
mem.Close();
}
// Delete message
// pop.Dele(id.Id);
}
}
}
pop.Quit();
}
}
}
I need to parse a few .msg files which have trail mails. Is there any way to get the sub messages and identify the initiated and the responded emails.
I do not want to use any third party tools. I am allowed to use the Outlook interop.
Below is the code that I have used to read the msg file.I am able to get the Body ,HTMLBody and other details.But I actually need all the trailing messages.
outlook._Application app = null;
outlook.MailItem item = null;
outlook.NameSpace session = null;
try
{
app = new outlook.Application();
session = app.Session;
item = session.OpenSharedItem(file) as outlook.MailItem;
}
catch(Exception ex)
{ }
If you are limited to OOM only, the only way to do that is to save each embedded message attachment as an MSG file (Attachment.SaveAsFile), then open it using Namespace.OpenSharedItem.
If using Redemption (I am its author) is an option, an MSG file can be opened using RDOSession.GetMessageFromMsgFile (similar to Namespace.OpenSharedItem in OOM), and the embedded message attachment can be accessed using the RDOAttachment.EmbeddedMsg property (returns RDOMail object) - no need to save the attachment first.
My current project runs a service on a Microsoft Exchange 2010-based email address, dedicated to apply custom rules to incoming emails.
As I am browsing through the possible C#-based solutions, EWS managed API seems like the best API to use for me. Every email action I need was found, but there is one extremely big one missing... Saving an email to a .msg file. Which is quite surprising to me given how easy of an action it is from Outlook (simply drag and drop from Outlook to any folder).
This is an absolute requirement as users keep their emails organized through drag and drop. Is there any way I've missed to do that with EWS? So far I've only found two non-EWS ways :
Using a third-party library which I am not sure we can afford (IndependentSoft)
Using a more complex method with MessageSave and outlook rules to execute a custom action (action being "run MessageSave" )
I am quite surprised that such a basic action requires so much work and would like to know, is there any easy way to save an email to a .msg file ?
Worst case scenario, is there a non-EWS API, C# based method to do so?
Thank you
Edit:
I have explored the .eml export solution. The issue is we use Outlook 2007, which does not support eml format. .msg is pretty much the requirement here
.msg is a format, which only outlook itself uses, not the exchange server you are communicating with.
For that matter, a .eml file can be created quite easily.
See here, how you can do it.
There is a non-EWS API, C# based method to do exactly what your looking for:
http://www.independentsoft.de/exchangewebservices/tutorial/downloadmessagetomsgfile.html
using System;
using System.IO;
using System.Net;
using Independentsoft.Exchange;
namespace Sample
{
class Program
{
static void Main(string[] args)
{
NetworkCredential credential = new NetworkCredential("username", "password");
Service service = new Service("https://myserver3/ews/Exchange.asmx", credential);
try
{
ItemShape itemShape = new ItemShape(ShapeType.Id);
FindItemResponse inboxItems = service.FindItem(StandardFolder.Inbox, itemShape);
for (int i = 0; i < inboxItems.Items.Count; i++)
{
Independentsoft.Msg.Message msgFile = service.GetMessageFile(inboxItems.Items[i].ItemId);
msgFile.Save("c:\\test\\message" + i + ".msg", true);
}
}
catch (ServiceRequestException ex)
{
Console.WriteLine("Error: " + ex.Message);
Console.WriteLine("Error: " + ex.XmlMessage);
Console.Read();
}
catch (WebException ex)
{
Console.WriteLine("Error: " + ex.Message);
Console.Read();
}
}
}
}
It offers a feature to save messages and other items as Outlook .msg files.
i want to write application who send email using outlook and i found this link.
i try this and it's perfect for me but the only thing that i miss here is the option to attach files to the mail, is it possible to do it ?
Better use MailMessage instead.
There's an example on how to use it with attachment here(Scroll down to "Examples"):
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx
Not only will you get a managed framework for sending mails, but also whoever runs the code will not need Outlook installed and running.
If you're stuck with outlook for some reason, try this:
using Outlook = Microsoft.Office.Interop.Outlook;
int pos = (int)email.Body.Length + 1;
int attType = (int)Outlook.OlAttachmentType.olByValue;
email.Attachments.Add("file.txt", attType, pos, "File description.");
where:
Outlook.MailItem email = (Outlook.MailItem)_outlookAppInstance.CreateItem(Outlook.OlItemType.olMailItem);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I am looking for a method of reading emails using Pop3 in C# 2.0. Currently, I am using code found in CodeProject. However, this solution is less than ideal. The biggest problem is that it doesn't support emails written in unicode.
I've successfully used OpenPop.NET to access emails via POP3.
downloading the email via the POP3 protocol is the easy part of the task. The protocol is quite simple and the only hard part could be advanced authentication methods if you don't want to send a clear text password over the network (and cannot use the SSL encrypted communication channel). See RFC 1939: Post Office Protocol - Version 3
and RFC 1734: POP3 AUTHentication command for details.
The hard part comes when you have to parse the received email, which means parsing MIME format in most cases. You can write quick&dirty MIME parser in a few hours or days and it will handle 95+% of all incoming messages. Improving the parser so it can parse almost any email means:
getting email samples sent from the most popular mail clients and improve the parser in order to fix errors and RFC misinterpretations generated by them.
Making sure that messages violating RFC for message headers and content will not crash your parser and that you will be able to read every readable or guessable value from the mangled email
correct handling of internationalization issues (e.g. languages written from righ to left, correct encoding for specific language etc)
UNICODE
Attachments and hierarchical message item tree as seen in "Mime torture email sample"
S/MIME (signed and encrypted emails).
and so on
Debugging a robust MIME parser takes months of work. I know, because I was watching my friend writing one such parser for the component mentioned below and was writing a few unit tests for it too ;-)
Back to the original question.
Following code taken from our POP3 Tutorial page and links would help you:
//
// create client, connect and log in
Pop3 client = new Pop3();
client.Connect("pop3.example.org");
client.Login("username", "password");
// get message list
Pop3MessageCollection list = client.GetMessageList();
if (list.Count == 0)
{
Console.WriteLine("There are no messages in the mailbox.");
}
else
{
// download the first message
MailMessage message = client.GetMailMessage(list[0].SequenceNumber);
...
}
client.Disconnect();
HOWTO: Download emails from a GMail account in C# (blogpost)
Rebex Mail for .NET (POP3/IMAP client component for .NET)
Rebex Secure Mail for .NET (POP3/IMAP client component for .NET - SSL enabled)
My open source application BugTracker.NET includes a POP3 client that can parse MIME. Both the POP3 code and the MIME code are from other authors, but you can see how it all fits together in my app.
For the MIME parsing, I use http://anmar.eu.org/projects/sharpmimetools/.
See the file POP3Main.cs, POP3Client.cs, and insert_bug.aspx
You can also try Mail.dll mail component, it has SSL support, unicode, and multi-national email support:
using(Pop3 pop3 = new Pop3())
{
pop3.Connect("mail.host.com"); // Connect to server and login
pop3.Login("user", "password");
foreach(string uid in pop3.GetAll())
{
IMail email = new MailBuilder()
.CreateFromEml(pop3.GetMessageByUID(uid));
Console.WriteLine( email.Subject );
}
pop3.Close(false);
}
You can download it here at https://www.limilabs.com/mail
Please note that this is a commercial product I've created.
call me old fashion but why use a 3rd party library for a simple protocol. I've implemented POP3 readers in web based ASP.NET application with System.Net.Sockets.TCPClient and System.Net.Security.SslStream for the encryption and authentication. As far as protocols go, once you open up communication with the POP3 server, there are only a handful of commands that you have to deal with. It is a very easy protocol to work with.
I wouldn't recommend OpenPOP. I just spent a few hours debugging an issue - OpenPOP's POPClient.GetMessage() was mysteriously returning null. I debugged this and found it was a string index bug - see the patch I submitted here: http://sourceforge.net/tracker/?func=detail&aid=2833334&group_id=92166&atid=599778. It was difficult to find the cause since there are empty catch{} blocks that swallow exceptions.
Also, the project is mostly dormant... the last release was in 2004.
For now we're still using OpenPOP, but I'll take a look at some of the other projects people have recommended here.
HigLabo.Mail is easy to use. Here is a sample usage:
using (Pop3Client cl = new Pop3Client())
{
cl.UserName = "MyUserName";
cl.Password = "MyPassword";
cl.ServerName = "MyServer";
cl.AuthenticateMode = Pop3AuthenticateMode.Pop;
cl.Ssl = false;
cl.Authenticate();
///Get first mail of my mailbox
Pop3Message mg = cl.GetMessage(1);
String MyText = mg.BodyText;
///If the message have one attachment
Pop3Content ct = mg.Contents[0];
///you can save it to local disk
ct.DecodeData("your file path");
}
you can get it from https://github.com/higty/higlabo or Nuget [HigLabo]
I just tried SMTPop and it worked.
I downloaded this.
Added smtpop.dll reference to my C# .NET project
Wrote the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmtPop;
namespace SMT_POP3 {
class Program {
static void Main(string[] args) {
SmtPop.POP3Client pop = new SmtPop.POP3Client();
pop.Open("<hostURL>", 110, "<username>", "<password>");
// Get message list from POP server
SmtPop.POPMessageId[] messages = pop.GetMailList();
if (messages != null) {
// Walk attachment list
foreach(SmtPop.POPMessageId id in messages) {
SmtPop.POPReader reader= pop.GetMailReader(id);
SmtPop.MimeMessage msg = new SmtPop.MimeMessage();
// Read message
msg.Read(reader);
if (msg.AddressFrom != null) {
String from= msg.AddressFrom[0].Name;
Console.WriteLine("from: " + from);
}
if (msg.Subject != null) {
String subject = msg.Subject;
Console.WriteLine("subject: "+ subject);
}
if (msg.Body != null) {
String body = msg.Body;
Console.WriteLine("body: " + body);
}
if (msg.Attachments != null && false) {
// Do something with first attachment
SmtPop.MimeAttachment attach = msg.Attachments[0];
if (attach.Filename == "data") {
// Read data from attachment
Byte[] b = Convert.FromBase64String(attach.Body);
System.IO.MemoryStream mem = new System.IO.MemoryStream(b, false);
//BinaryFormatter f = new BinaryFormatter();
// DataClass data= (DataClass)f.Deserialize(mem);
mem.Close();
}
// Delete message
// pop.Dele(id.Id);
}
}
}
pop.Quit();
}
}
}