3

I am new to Windows Mobile Application. In my project, I want to send emails using microsoft.windowsmobile.pocketoutlook. I have the following code so far:

private void btnsubmit_Click(object sender, EventArgs e)
{
    try
    { 
        totleave();
        OutlookSession ol = new OutlookSession();
        EmailMessage em = new EmailMessage();
        //Recipient  s1 = em.From;
        //Console.WriteLine(s1);
        Recipient r = new Recipient("iyalarasi.r", "iyalarasi.r@winxsolutions.com");
        em.To.Add(r);
        em.Subject = "Request For Leave";
        em.BodyText =txtename.Text +"(Emp id:"+txteno.Text+ ")" + " request "+ cb1.SelectedItem.ToString()+" leave from "+dtpfrom .Value
            .ToShortDateString ()+" to "+dtpto .Value.ToShortDateString ()  + "\n The reason is " + txtreason.Text;
        EmailAccount ea = ol.EmailAccounts[0];
        ea.Send(em);
        // em.Send("iyalarasi.r");//Account name in outlook
        //MessagingApplication.Synchronize("iyalarasi.r");
        MessageBox.Show("mail sent");
        f2.Show();
        f2.lblmsg.Text = "You have Applied Leave";
    }
    catch (PocketOutlookException ex)
    {
        lblmsg1.Text = ex.ToString();
    }
    catch (Exception e1)
    {
        lblmsg1.Text = e1.ToString();
    }
}

With this code, emails appear in the outbox, but never appears in the inbox. Using Gmail, It shows the following message:

Messages could not be sent. Check that you have network coverage, and that your account information is correct, and then try again.

My account information is correct. What is going on?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Iyalarasi.R
  • 41
  • 1
  • 3
  • 7
  • Do you have an email account set up on the device? Can you send directly from PocketOutlook? – ctacke Oct 24 '12 at 12:08
  • Yes.I can setup email on device.I configured gmail account on my device, But for that also,I am not able to send mail,The mail resides on outbox folder itself,And it displays error synchronizing message box. I configured another mail,in I receiving mail says,System Administrator error message:505 should be authenticated. – Iyalarasi.R Oct 25 '12 at 08:27
  • Hi, the main diff between your and mine code is the setup of the session var. Mine uses a known account string to search thru the accounts whereas your code simply uses the first returned accoung by index "eMailAccounts[0]" ~ josef – josef Oct 27 '12 at 04:09
  • hi,Is that necessary to have activesync software on emulator,because while i am sending mail ,it shows error synchoronizing,and one more doubt is that, can I use your class file for webmail.1and1.com provider? – Iyalarasi.R Oct 29 '12 at 06:44

2 Answers2

2

I already use POOM to send eMail from Windows Mobile devices:

I use code to iterate thru the available outlook eMail accounts. The string sMailAccount has the name of the account as displayed in pocketOuttlook, for example "Google Mail":

class code of sendMail class...

    public sendMail(string sMailAccount)
    {
        session = new OutlookSession();
        //eMail = new EmailMessage();
        bool bFound = false;
        foreach (Account acc in session.EmailAccounts)
        {
            System.Diagnostics.Debug.WriteLine(acc.Name);
            if (acc.Name == sMailAccount)
                bFound = true;
        }
        if (bFound)
            account = session.EmailAccounts[sMailAccount];
        if (account != null)
            _bIsValidAccount = true;
    }
    ...

My code was used to send images to recipients:

    public bool send(string sImagePath)
    {
        if (account == null)
            return false;
        try
        {
            eMail = new EmailMessage();
            rcp = new Recipient(_to);
            eMail.To.Add(rcp);
            eMail.Subject = "Visitenkarten";
            eMail.BodyText = "VCard " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + "\r\nsent from eMDI2Mail";

            attachement = new Attachment(sImagePath);
            eMail.Attachments.Add(attachement);                
            eMail.Send(account);                
            //account.Send(eMail);
            if (this._syncImmediately)
            {
                if (this.account != null)
                    Microsoft.WindowsMobile.PocketOutlook.MessagingApplication.Synchronize(this.account);
            }
            return true;
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("Exception in send(): " + ex.Message);
        }
        return false;
    }

Another issue to know about is that POutlook will not send messages immediately. Therefor I introduced a var called _syncImmediately. If this is true, Outlook will send the message immediately. If not, I offer another function called syncNow():

    /// <summary>
    /// sync eMail in outlook
    /// </summary>
    /// <param name="pHandle">handle to forground window</param>
    public void syncNow(IntPtr pHandle)
    {
        if (this.account != null)
        {
            Microsoft.WindowsMobile.PocketOutlook.MessagingApplication.Synchronize(this.account);
            SetForegroundWindow(pHandle);
        }
    }

The SetForeGroundWindow() is used to bring us back to the application.

Here is the whole class:

    using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

using System.Runtime.InteropServices;

using Microsoft.WindowsMobile.PocketOutlook;

namespace eMdiMail
{
    class sendMail:IDisposable
    {
        [DllImport("coredll", EntryPoint = "SetForegroundWindow")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        OutlookSession session;
        public EmailAccount account;
        EmailMessage eMail;
        public String _to = "TheRecipient@intermec.com";
        Attachment attachement;
        Recipient rcp;

        bool _syncImmediately = false;
        public bool syncImmediately
        {
            get { return _syncImmediately; }
            set { _syncImmediately = value; }
        }
        bool _bIsValidAccount = false;
        public bool bIsValidAccount
        {
            get
            {
                return _bIsValidAccount;
            }
        }
        public bool setAccount(string sMailAccount)
        {
            session.Dispose();
            session = new OutlookSession();
            //eMail = new EmailMessage();
            bool bFound = false;
            foreach (Account acc in session.EmailAccounts)
            {
                if (acc.Name == sMailAccount)
                {
                    account = session.EmailAccounts[sMailAccount];
                    bFound = true;
                }
            }
            return bFound;
        }
        public sendMail(string sMailAccount)
        {
            session = new OutlookSession();
            //eMail = new EmailMessage();
            bool bFound = false;
            foreach (Account acc in session.EmailAccounts)
            {
                System.Diagnostics.Debug.WriteLine(acc.Name);
                if (acc.Name == sMailAccount)
                    bFound = true;
            }
            if (bFound)
                account = session.EmailAccounts[sMailAccount];
            else
            {
                if(this.createAccountGoogle())
                    account = session.EmailAccounts[sMailAccount];
            }
            if (account != null)
                _bIsValidAccount = true;
        }
        public sendMail()
        {
            session = new OutlookSession();
            //eMail = new EmailMessage();
            bool bFound = false;
            foreach (Account acc in session.EmailAccounts)
            {
                System.Diagnostics.Debug.WriteLine(acc.Name);
                if (acc.Name == "Google Mail")
                    bFound = true;
            }
            if (bFound)
                account = session.EmailAccounts["Google Mail"];
            else
            {
                if(this.createAccountGoogle())
                    account = session.EmailAccounts["Google Mail"];
            }
            if (account != null)
                _bIsValidAccount = true;
        }
        /// <summary>
        /// sync eMail using send and recv in foreground
        /// </summary>
        public void syncNow()
        {
            if (this.account != null)
                Microsoft.WindowsMobile.PocketOutlook.MessagingApplication.Synchronize(this.account);
        }
        /// <summary>
        /// sync eMail in outlook
        /// </summary>
        /// <param name="pHandle">handle to forground window</param>
        public void syncNow(IntPtr pHandle)
        {
            if (this.account != null)
            {
                Microsoft.WindowsMobile.PocketOutlook.MessagingApplication.Synchronize(this.account);
                SetForegroundWindow(pHandle);
            }
        }
        public bool send(string sImagePath)
        {
            if (account == null)
                return false;
            try
            {
                eMail = new EmailMessage();
                rcp = new Recipient(_to);
                eMail.To.Add(rcp);
                eMail.Subject = "Visitenkarten LogiMAT";
                eMail.BodyText = "LogiMat " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + "\r\nsent from eMDI2Mail";

                attachement = new Attachment(sImagePath);
                eMail.Attachments.Add(attachement);                
                eMail.Send(account);                
                //account.Send(eMail);
                if (this._syncImmediately)
                {
                    if (this.account != null)
                        Microsoft.WindowsMobile.PocketOutlook.MessagingApplication.Synchronize(this.account);
                }
                return true;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception in send(): " + ex.Message);
            }
            return false;
        }

        public void Dispose()
        {
            if (account != null)
                account.Dispose();
            if (session != null)
                session.Dispose();
        }

        public bool createAccountHotmail()
        {
            XMLConfig.Settings sett = new XMLConfig.Settings();

            return XMLConfig.DMProcessConfig.ProcessXML(sett.appPath + "hotmail.xml");
        }
        public bool createAccountGoogle(){
            XMLConfig.Settings sett= new XMLConfig.Settings();

            return XMLConfig.DMProcessConfig.ProcessXML(sett.appPath + "gmail.xml");
/*
            bool bRet = false;
            string strTemp = "";
            strTemp += "<wap-provisioningdoc>\r\n";
            strTemp += "  <characteristic type=\"EMAIL2\" recursive=\"true\">\r\n";
            strTemp += "    <characteristic type=\"{D45D5BE0-B96C-87A5-60B8-A59B69C733E4}\">\r\n";
            strTemp += "      <parm name=\"SERVICENAME\" value=\"Google Mail\" />\r\n";
            strTemp += "      <parm name=\"SERVICETYPE\" value=\"IMAP4\" />\r\n";
            strTemp += "      <parm name=\"INSERVER\" value=\"imap.googlemail.com\" />\r\n";
            strTemp += "      <parm name=\"AUTHNAME\" value=\"YourName@googlemail.com\" />\r\n";
            strTemp += "      <parm name=\"DOMAIN\" value=\"\" />\r\n";
            strTemp += "      <parm name=\"OUTSERVER\" value=\"smtp.googlemail.com\" />\r\n";
            strTemp += "      <parm name=\"REPLYADDR\" value=\"YourName@googlemail.com\" />\r\n";
            strTemp += "      <parm name=\"SMTPALTAUTHNAME\" value=\"YourName@googlemail.com\" />\r\n";
            strTemp += "      <parm name=\"SMTPALTDOMAIN\" value=\"\" />\r\n";
            strTemp += "      <parm name=\"NAME\" value=\"YourName 2011\" />\r\n";
            strTemp += "      <parm name=\"LINGER\" value=\"120\" />\r\n";
            strTemp += "      <parm name=\"RETRIEVE\" value=\"2048\" />\r\n";
            strTemp += "      <parm name=\"KEEPMAX\" value=\"0\" />\r\n";
            strTemp += "      <parm name=\"DWNDAY\" value=\"3\" />\r\n";
            strTemp += "      <parm name=\"FORMAT\" value=\"2\" />\r\n";
            strTemp += "      <parm name=\"AUTHREQUIRED\" value=\"1\" />\r\n";
            strTemp += "      <parm name=\"AUTHSECRET\" value=\"YourPassword\"/>\r\n";
            strTemp += "    </characteristic>\r\n";
            strTemp += "  </characteristic>\r\n";
            strTemp += "</wap-provisioningdoc>";

            return bRet;
*/
        }
    }
}

As you can see, the class is also able to create eMail accounts by a given XML WAP Provisioning file.

regards

Josef

BTW: the app was designed to make a special photo of business cards at fair using Intermec eMDI technology. These should be then send directly to a secretary to create leads of these.

OK, to make more simple and clear: 1. To be able to use POutlook you need a reference to an poutlook session. 2. To be able to send an email via code, you need to specify the mail account that poutlook has to use. 3. Then create an eMail object and fill the fields 4. Finally use the send method of the email object with the existing account object

In more detail

Create a session object

      OutlookSession session = new OutlookSession();

Specify the account to use for the email sending. The String has to exactly match your defined PocketOutlook eMail account name. If you use a number for reference, you cannot be sure, which account is choosen.

      EmailAccount account = session.EmailAccounts[sMailAccount];

Check the returned account. Is it NULL? Now create a new EMailMessage (in contrast to a TextMessage(SMS))

      EmailMessage eMail = new EmailMessage();

and then fill the fields of the EmailMessage object

        Recipient rcp = new Recipient(_to);
        eMail.To.Add(rcp);
        eMail.Subject = "Visitenkarten";
        eMail.BodyText = "Enter some eMail text to send";

and finally send the eMail:

     eMail.Send(account); 

as poutlook does normally send eMail in background periodically at some time, you may want to let poutlook send the mail immediately. If so, you can use that code:

     Microsoft.WindowsMobile.PocketOutlook.MessagingApplication.Synchronize(account);

this will make poulook sync the email of the specified account immediately, but also brings the outlook window to foreground.

Is this clear and simple?

josef
  • 5,951
  • 1
  • 13
  • 24
  • I can't use,Xmlconfig.settings.configuration.The assembly does not support configuration.I am using .net 3.5. – Iyalarasi.R Nov 06 '12 at 11:22
  • Sorry,I am not able to understand your code clearly.Could you please give me some more explanation.Thanks in advance. – Iyalarasi.R Nov 06 '12 at 12:02
  • There is no prob with the xml I use in the code. XMLConfig is a class created by me to hold some app config. - Do you like the more simple example at bottom of my Anser? – josef Nov 06 '12 at 18:06
  • I am also using same set of codings in my project,But mail still appears in outbox only.I am having doubt,I can't set from address for my mails,in From address it shows as (outlook mail).Actually,I am using webmail.1and1.com.And I am not able to configure outlook server address,incoming and outgoing address in outlook. – Iyalarasi.R Nov 08 '12 at 07:08
  • @Iyalarasi.R Can you please tell me how can i send mail to my comoany collegues.. using microsot outlook.. i dont have internet in my system. But i have my computer connected to server.. can i send mail through server.. any way please help me out... Link to question i have asked http://stackoverflow.com/questions/25472821/how-to-send-mail-from-my-work-place-computer-which-is-connected-to-server-not-t?noredirect=1#comment39752921_25472821 – Milan phir Aug 24 '14 at 17:53
  • @josef Can you please tell me how can i send mail to my comoany collegues.. using microsot outlook.. i dont have internet in my system. But i have my computer connected to server.. can i send mail through server.. Anyway, please help me out... Link to question i have asked http://stackoverflow.com/questions/25472821/how-to-send-mail-from-my-work-place-computer-which-is-connected-to-server-not-t?noredirect=1#comment39752921_25472821 – Milan phir Aug 24 '14 at 17:55
  • To see how one can CREATE an Outlook Mobile eMail ACCOUNT from code see: https://github.com/hjgode/eMdiMail/blob/master/eMdiMail/sendMail.cs and there createAccountGoogle(). This code uses a XML to provision an outlook mail account. – josef Aug 26 '14 at 17:01
0

There is nothing faulty in your code.

You need to look into how to get a message to send from Pocket Outlook through the GMail Server. Once that is set, your code should work fine.

  • I configured one new email setup in windows mobile,in that I am able to send and receive mails,But how can I achived that through coding? how can I chane my code?I am using smtp.1and1.com for outgoing mail server and pop.1and1.com for incoming server. – Iyalarasi.R Oct 26 '12 at 06:32
  • To see how one can CREATE an Outlook Mobile eMail ACCOUNT from code see: https://github.com/hjgode/eMdiMail/blob/master/eMdiMail/sendMail.cs and there createAccountGoogle(). This code uses a XML to provision an outlook mail account. – josef Aug 26 '14 at 17:03