在Windows中的Windows移动应用程序中发送邮件

我是Windows Mobile Application的新手。 在我的项目中,我想使用microsoft.windowsmobile.pocketoutlook发送电子邮件。 到目前为止,我有以下代码:

 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(); } } 

使用此代码,电子邮件会显示在发件箱中,但不会显示在收件箱中。 使用Gmail,它会显示以下消息:

无法发送邮件。 检查您是否有网络覆盖,并且您的帐户信息正确无误,然后重试。

我的帐户信息是正确的。 到底是怎么回事?

我已经使用POOM从Windows Mobile设备发送电子邮件:

我使用代码通过可用的Outlook电子邮件帐户进行迭代。 字符串sMailAccount具有pocketOuttlook中显示的帐户名称,例如“Google Mail”:

sendMail类的类代码…

  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; } ... 

我的代码用于将图像发送给收件人:

  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; } 

另一个要了解的问题是POutlook不会立即发送消息。 为此我引入了一个名为_syncImmediately的var。 如果是这样,Outlook将立即发送消息。 如果没有,我提供另一个名为syncNow()的函数:

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

SetForeGroundWindow()用于将我们带回应用程序。

这是全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; } ///  /// sync eMail using send and recv in foreground ///  public void syncNow() { if (this.account != null) Microsoft.WindowsMobile.PocketOutlook.MessagingApplication.Synchronize(this.account); } ///  /// sync eMail in outlook ///  /// handle to forground window 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 += "\r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += " \r\n"; strTemp += ""; return bRet; */ } } } 

如您所见,该类还能够通过给定的XML WAP Provisioning文件创建电子邮件帐户。

问候

约瑟夫

顺便说一句:该应用程序旨在使用Intermec eMDI技术制作一张特殊的名片照片。 然后应将这些直接发送给秘书,以创建这些的线索。

好的,更简单明了:1。为了能够使用POutlook,你需要一个poutlook会话的引用。 2.为了能够通过代码发送电子邮件,您需要指定poutlook必须使用的邮件帐户。 3.然后创建eMail对象并填写字段4.最后,使用现有帐户对象的电子邮件对象的send方法

更详细

创建会话对象

  OutlookSession session = new OutlookSession(); 

指定用于发送电子邮件的帐户。 String必须与您定义的PocketOutlook电子邮件帐户名完全匹配。 如果您使用数字作为参考,则无法确定选择了哪个帐户。

  EmailAccount account = session.EmailAccounts[sMailAccount]; 

检查退回的帐户。 它是NULL吗? 现在创建一个新的EMailMessage(与TextMessage(SMS)相反)

  EmailMessage eMail = new EmailMessage(); 

然后填写EmailMessage对象的字段

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

最后发送电子邮件:

  eMail.Send(account); 

由于poutlook通常会在某个时间定期发送电子邮件,您可能希望让poutlook立即发送邮件。 如果是这样,您可以使用该代码:

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

这将使poulook立即同步指定帐户的电子邮件,但也将outlook窗口置于前台。

这清楚而简单吗?

您的代码中没有任何错误。

您需要了解如何通过GMail Server从Pocket Outlook发送消息。 设置完成后,您的代码应该可以正常工作。