locked
visual c++, calling a c# class to access e-mail account via pop, imap and smtp RRS feed

  • Question

  • Here is code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Lesnikowski.Client;
    using Lesnikowski.Client.IMAP;
    using Lesnikowski.Mail;
    using Lesnikowski.Mail.Fluent;
    using Lesnikowski.Mail.Headers;
    using Lesnikowski.Mail.Headers.Constants;

    namespace Email
    {
        public class Class1
        {
           public void POP3_Subject_Message(String User, String Password,String Server)
           {
              Pop3 pop3 = new Pop3();
              pop3.Connect(Server);             
              pop3.User = User;
              pop3.Password = Password;
              pop3.Login();
              pop3.GetAccountStat();                     
              SimpleMailMessageBuilder builder = new SimpleMailMessageBuilder();
              for (int i = 1; i <= pop3.MessageCount; i++)
              { ISimpleMailMessage mail = builder.CreateFromEml(pop3.GetMessage(i));
                Console.WriteLine(mail.Subject);
              }
               pop3.Close(true);
            }

           public void IMAP_Subject_Message(String User, String Password, String Server)
           {
               Imap imap = new Imap();
               imap.Connect(Server);

               imap.User = User;
               imap.Password = Password;
               imap.Login();

               imap.SelectInbox();
               List<long> uidList = imap.SearchFlag(Flag.Unseen);
               foreach (long uid in uidList)
               {
                   ISimpleMailMessage mail = new SimpleMailMessageBuilder()
                       .CreateFromEml(imap.GetMessageByUID(uid));
                   Console.WriteLine(mail.Subject);
               }
               imap.Close(true);
            }

           public void SMTP_Send_Message(String User, String Password, String Server)
           {
               ISimpleMailMessage message = Mail
               .Html(@"Html with an image: <img src=""cid:lena"" />")
               .AddVisual(@"c:\lena.jpeg").SetContentId("lena")
               .AddAttachment(@"c:\tmp.doc").SetFileName("document.doc")
               .To("to@mail.com")
               .From("from@mail.com")
               .Subject("Subject")
               .Create();
               Smtp smtp = new Smtp();
               smtp.Connect(Server);
               smtp.Ehlo(HeloType.EhloHelo, "yourname");

               smtp.User = User;
               smtp.Password = Password;
               smtp.Login();

               smtp.SendMessage(message);

               smtp.Close(true);  
           }
         }
    }

    c++ code to call it:

    #using "Email.dll"
    using namespace Email;

    int main()
    {
       Class1 Object1;
      Object1.POP3_Subject_Message("surgey46@gmail.com","My_Password_Here","pop.gmail.com");
    }

    It suppose to access my gmail account to view e-mail messages vis pop server. I enable pop server in settings in my gmail account.
    When I run I get exception error,

    All the credentials are correct. Can someone tell me what is wrong with c# class,
    what should be changed or modified?

    Thanks in advance, Gennady46

     

    Thursday, February 4, 2010 3:55 AM

Answers

All replies

  • This is a Visual C++ Language forum. Your question completely OT here.
    Thursday, February 4, 2010 4:22 AM
  • I see you have edited your original post, adding a little C++ client code. Are you hoping this makes your question more relevant to this forum?
    Thursday, February 4, 2010 4:36 PM
  • what was the exception message?, As per yor code you are missing port number. Some webmail servers commuicate via diffrent servers, some may enabled SSL too. You have to check that with Gmail Pop3 settings.
    Thanks Mike --------Please mark as answer if it is useful----------
    Thursday, February 4, 2010 4:58 PM
  • The error is: The exception unknown software exception (0xe0434f4d) occured in the application at location 0x7c812afb.

    Do you think I am missing a port number? I checked with pop I did not see a port ssl. If you would ad port # what is code for that for pop3?

    Thanks gennady46
    Thursday, February 4, 2010 7:40 PM
  • here is a good article on this http://www.codeproject.com/KB/cs/karavaev_denis.aspx .
    Thanks Mike --------Please mark as answer if it is useful----------
    • Marked as answer by Gennady46 Friday, February 5, 2010 7:35 PM
    Friday, February 5, 2010 11:33 AM
  • Since the majority of your code is C# and that is where the error is originating, you really should ask there.
    Visit my (not very good) blog at http://c2kblog.blogspot.com/
    Friday, February 5, 2010 1:12 PM