我已经使用 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() 的函数:
/// <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);
}
}
SetForeGroundWindow() 用于将我们带回应用程序。
这是整个班级:
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;
*/
}
}
}
如您所见,该类还能够通过给定的 XML WAP 配置文件创建电子邮件帐户。
问候
约瑟夫
顺便说一句:该应用程序旨在使用 Intermec eMDI 技术在展会上制作一张特别的名片照片。然后应将这些直接发送给秘书以创建这些线索。
好的,为了更简单明了: 1. 为了能够使用 POutlook,您需要对 poutlook 会话的引用。2. 为了能够通过代码发送电子邮件,您需要指定 poutlook 必须使用的邮件帐户。3. 然后创建一个 eMail 对象并填写字段 4. 最后将 email 对象的 send 方法与现有的 account 对象一起使用
更详细
创建会话对象
OutlookSession session = new OutlookSession();
指定用于发送电子邮件的帐户。该字符串必须与您定义的 PocketOutlook 电子邮件帐户名称完全匹配。如果您使用数字作为参考,您无法确定选择了哪个帐户。
EmailAccount account = session.EmailAccounts[sMailAccount];
检查退回的帐户。它是空的吗?现在创建一个新的 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 窗口带到前台。
这清楚和简单吗?