|
|
aspNetPOP3
|
 |
Subscribe to category: aspNetPOP3
by Dave
27. September 2011 13:44
Ian (on the forums) had a question about how to download email from a POP3 server, process it using his own custom rules, and then resending it.
The trick to resending it, is that he wanted to the original email to be resent and unmodified. This is a little different than the usual request. Usually, developers need to modify the original email in some fashion, rebuild it, and resend it.
In this example, Ian wanted to resend the email, as if it's never been sent. It needs to dynamically determine who to send it to, and forward it on. This makes his application work like an email proxy of sorts.
Below is a code example that would get Ian started on something like this.
As always, if anyone has any questions about this, feel free to use the Contact Us page, to send any questions.
Thanks, Dave
Here is the code
void Execute()
{
//connect to the pop3 server
POP3 pop = new POP3();
pop.Server = "mail.blah.com";
pop.Username = "dave@blah.com";
pop.Password = "test";
//enable logging
pop.LogPath = "c:\\pop.log";
//login
pop.Login();
//get the first message as text
string content = pop.GetMessageAsText(0);
//process the message
Process( content );
ResendAsOriginal( content );
//if we want to delete the message, uncomment the following lines
//pop.Delete(0);
//pop.CommitDeletes();
//disconnect
pop.Disconnect();
}
void Process( string emailContent )
{
//create the MimeMessage
MimeMessage m = MimeMessage.ParseString( emailContent );
//do whatever we need to with the extracted data
if( m.Subject != null )
{
string subject = m.Subject.Value;
}
}
void ResendAsOriginal( string emailContent )
{
//create the EmailMessage object
EmailMessage msg = new EmailMessage();
//set the server
msg.Server = "192.168.1.106";
//set the TO and FROM values
//these may need to be dynamic, and depend up on the
//value of emailContent
//for this example, they are hard coded
msg.To = "CustomerService@blah.com";
msg.FromAddress = "proxy@myserver.com";
msg.SmtpData = emailContent;
msg.Send();
}
by Dave
2. March 2011 02:15
aspNetPOP3 can easily fetch email from Gmail. To do this you must do the following:
a) Configure POP3 access in Gmail b) Download the AdvancedIntellect.Ssl.dll from http://www.advancedintellect.com/download.aspx c) Use aspNetPOP3 over SSL at port 995 to connect.
Lets discuss this below.
Configure POP3 access in Gmail Before you can access email, you must first configure POP3 in Gmail. To do this, log into Gmail, and click on the settings tab. You should see something similar to what is below. Basically, you want select "Enable POP3 email".

You have a couple of different options. For example, when you connect over POP3 you can have Gmail behave like: a) Keep the email in the Inbox b) Mark the email as read, and kept in the Inbox c) Archive the Message d) Delete the Message
It's important to realize that in the POP3 protocol, there is only the Inbox. The POP3 protocol does not recognize any concept of mail folders. Thus, it's impossible to navigate or change to a different folder.
Download the AdvancedIntellect.Ssl.dll Once you have POP3 configured in Gmail, you need to download the AdvancedIntellect,Ssl.dll plugin to use with aspNetPOP3. You can download the dll from: http://www.advancedintellect.com/download.aspx Gmail only allows you to connect over POP3 using SSL. The AdvancedIntellect,Ssl.dll allows you to do this.
Use aspNetPOP3 over SSL at port 995 to connect. Once you've downloaded the dll, you can integrate it into your project.
If you are using VS.NET, import the dll into your application, and set a reference to it.
If you are not using VS.NET, you should be able to just copy the dll to your /bin directory.
Once that is done, the following code example will get you started. This example simply downloads the message count from your inbox, and loops through the headers of the individual messages.
C#
POP3 pop = new POP3( "pop.gmail.com" );
//create and load the ssl socket
AdvancedIntellect.Ssl.SslSocket ssl = new AdvancedIntellect.Ssl.SslSocket();
pop.LoadSslSocket( ssl );
//logging on the ssl socket (not required)
ssl.Logging = true;
ssl.LogPath = "c:\\ssl.log";
//some logging in case we have any problems (not required)
pop.LogPath = "c:\\gmailpopop.log";
//rest of the POP3 properties
pop.Port = 995;
pop.Username = "MyAccount@gmail.com";
pop.Password = "MyPassword";
pop.Connect();
//get the message count as an example
int count = pop.MessageCount();
Response.Write( count.ToString() );
for( int i=0;i<count;i++)
{
string headers = pop.GetHeaders(i);
//Process the Headers using a custom method
DoWork( headers );
}
pop.Disconnect();
VB.NET
Dim pop As New POP3("pop.gmail.com")
'create and load the ssl socket
Dim ssl As New AdvancedIntellect.Ssl.SslSocket()
pop.LoadSslSocket(ssl)
'logging on the ssl socket (not required)
ssl.Logging = True
ssl.LogPath = "c:\ssl.log"
'some logging in case we have any problems (not required)
pop.LogPath = "c:\gmailpopop.log"
'rest of the POP3 properties
pop.Port = 995
pop.Username = "MyAccount@gmail.com"
pop.Password = "MyPassword"
pop.Connect()
'get the message count as an example
Dim count As Integer = pop.MessageCount()
Response.Write(count.ToString())
Dim i As Integer
For i = 0 To count - 1
Dim headers As String = pop.GetHeaders(i)
'Process the Headers using a custom method
DoWork(headers)
Next i
pop.Disconnect()
As always, if anyone has any questions, feel free to contact me over at the Contact Us web page.
Thanks, Dave Wanta
by Dave
14. February 2011 04:38
I had a great question from Robert today. He sent me the following question:
We are currently testing the aspNetPOP3 product. I might be missing the obvious, but I didn’t find where I could get an email, and then move it to a folder. Is this possible?
That's a great question. Usually someone will ask this, or a similar question every few months.
aspNetPOP3 implements the POP3 protocol. Everything that is possible by the POP3 protocol, is possible in aspNetPOP3.
As far as internet standards go, the POP3 protocol is pretty old. POP3 stands for Post Office Protocol version 3. It was developed in 1984.
POP3 has no concept of folders. In the POP3 (and consequently aspNetPOP3) world, there is only the Inbox. When you see additional folders in POP3 mail clients (such as Outlook, Outlook Express, Eudora, etc..) when you are actually seeing is a client side feature/functionality. When you move an email from the Inbox to a different folder, what your mail client is actually doing is deleting the message from the server, and moving the message to a local folder. Once that happens, it is no longer accessible via the POP3 protocol.
The same thing happens with webmail clients such as Gmail. (Although, gmail is a little different because it implements something called labels.) . Once the message is moved to a different folder (or label) in the webclient, you no longer have access to that message. Using the POP3 protocol, you can only get at messages in the Inbox.
Once again, great question Robert.
If anyone else has any other questions, comments or feedback relating to aspNetPOP3, feel free to contact me.
Thanks! Dave Wanta
by Dave
9. November 2010 02:29
I have a number of customers who need to parse cell phone (mobile) generated emails. These emails come in configurations I never dreamed possible. Although they are technically Mime compliant, their individual parts can be ordered strangely, or have unique headers that normally aren't found in a standard message.
I was sent an email request from a developer (Thanks Cara!) about parsing a T-Mobile generated message. She needed to find and extract the text body part. If a text body part wasn't found, then the html body part was to be found, and converted to plain text. The other requirement was to find all of the images, and download (save) them to a directory.
Below is a code example is something I whipped up for her. She used it as a basis to get started with her application. Error checking has been left out, to keep the code short, workable, but complete.
As always, if anyone has any questions or comments, please let me know.
Thanks! Dave Wanta
string path = "t_mobile.eml";
MimeMessage m = MimeMessage.ParseFile( path );
MimePart textPart = m.TextMimePart;
if( textPart == null )
{
//try and find it, by finding the first inline text part
MimePartCollection inlineParts = m.InLineParts;
if( ( inlineParts != null ) && ( inlineParts.Count >0 ) )
{
foreach( MimePart part in inlineParts )
{
if( (part.Name != null ) && ( part.Name.EndsWith(".txt") ) && ( part.ContentTypeString == "text/plain") )
{
//then we found our part
textPart = part;
break;
}
}
}
}
string plainText = string.Empty;
if( textPart != null )
{
plainText = textPart.DecodedText();
}
if( plainText.Length == 0 )
{
//find the first html part.
//get the plain text from the html part
foreach( MimePart part in m.RetrieveAllParts() )
{
if( (part.ContentTypeString != null ) && ( part.ContentTypeString.ToLower() == "text/html") )
{
string html = part.DecodedText();
//convert the Html to plain formatted text
plainText = Utility.ConvertHtmlToText( html).Trim();
break;
}
}
}
//extract all images, and save them to the temp directory
foreach( MimePart part in m.RetrieveAllParts() )
{
if( part.IsImage() )
{
//normally, would need to check for images that already have the same filename
//and rename accordingly
//in this example, just save the images to the temp directory
part.Save("c:\\temp\\");
}
}
by Dave
9. November 2010 01:46
** NOTE: ALL OF OUR PRODUCTS RUN ON ALL VERSIONS OF .NET. **
The instructions below are for people using VS2010 and beyond.
--------------------------
As more and more people are upgrading to VS2010, and beyond, I am getting more of the following emails:
aspNetEmail (or any of our other products) doesn't work in later versions of Visual Studio. I usually get one of the following errors:
"aspNetEmail is not declared, it may be inaccessible due to its protection level."
Or
"The referenced assembly "…" could not be resolved because it has a dependency upon System.Web (or some other internal .NET namespace). Please remove references to assemblies not in the targeted framework or consider retargeting your project"
Usually these exceptions occur when the developer is building a client side application.
Starting in VS2010, VS tries to be too smart for it's own good. When you build a client application (console.exe, winform, etc…) VS limits the number of namespaces you need access too, because it thinks you shouldn't need them.
To change this behavior, what you need to do, is change the target framework from a subset of namespaces, to all of them.
To change this, in VS.NET Solution Explorer, Right-Click on your project, an select Properties. On the Application tab, set the Target Framework to be ".NET Framework XX". By default it is set to ".NET Framework XX Client Profile". Press Ctrl-S for save, and you are done.
Below are 2 pictures that display changing the target framework.
As always, if anyone has any questions, feel free to contact me.
Thanks! Dave Wanta

C# Screenshot:

VB.NET Screenshot (this option is found under the Compile tab. Then, click the "Advanced Compile Option" button.

Testimonial
 |
I'm very pleased with the feature set offered by your product and your EXTREMELY helpful support!
"
|
 |
| Read more testimonials |
|