none
Enviar e-mail para o Windows Mobile em C# sem utilizar o PocketOutlook RRS feed

  • Pergunta

  • Pessoal tenho uma aplicação de Windows Mobile, preciso cirar uma rotina que faça essa aplicação enviar e-mail mas sem utilizar o Pocket Outlook , estou tentando utilizar classe MAPI , esse é o codigo que estou usando :

     

    using System;

    using System.Runtime.InteropServices;

    using System.IO;

    using System.Collections.Generic;

    using System.Windows.Forms;

    namespace Mapi

    {

    public class MAPI

    {

    public bool AddRecipientTo(string email)

    {

    return AddRecipient(email, HowTo.MAPI_TO);

    }

    public int SendMailPopup(string strSubject, string strBody)

    {

    return SendMail(strSubject, strBody, MAPI_LOGON_UI | MAPI_DIALOG);

    }

    public int SendMailDirect(string strSubject, string strBody)

    {

    return SendMail(strSubject, strBody, MAPI_LOGON_UI);

    }

     

    [DllImport("C:\\WINDOWS\\system32\\Mapi32.dll")]

    static extern int MAPISendMail(IntPtr sess, IntPtr hwnd,

    MapiMessage message, int flg, int rsv);

    int SendMail(string strSubject, string strBody, int how)

    {

    MapiMessage msg = new MapiMessage();

    msg.subject = strSubject;

    msg.noteText = strBody;

    msg.recips = GetRecipients(out msg.recipCount);

    m_lastError = MAPISendMail(new IntPtr(0), new IntPtr(0), msg, how, (0));

    if (m_lastError > 1)

    MessageBox.Show(" MAPISendMail failed! " + GetLastError(), " MAPISendMail");

    Cleanup(ref msg);

    return m_lastError;

    }

    bool AddRecipient(string email, HowTo howTo)

    {

    MapiRecipDesc recipient = new MapiRecipDesc();

    recipient.recipClass = (int)howTo;

    recipient.name = email;

    m_recipients.Add(recipient);

    return true;

    }

    IntPtr GetRecipients(out int recipCount)

    {

    recipCount = 0;

    if (m_recipients.Count == 0)

    return IntPtr.Zero;

    int size = Marshal.SizeOf(typeof(MapiRecipDesc));

    IntPtr intPtr = Marshal.AllocHGlobal(m_recipients.Count * size);

    int ptr = (int)intPtr;

    foreach (MapiRecipDesc mapiDesc in m_recipients)

    {

    Marshal.StructureToPtr(mapiDesc, (IntPtr)ptr, false);

    ptr += size;

    }

    recipCount = m_recipients.Count;

    return intPtr;

    }

    void Cleanup(ref MapiMessage msg)

    {

    int size = Marshal.SizeOf(typeof(MapiRecipDesc));

    int ptr = 0;

    if (msg.recips != IntPtr.Zero)

    {

    ptr = (int)msg.recips;

    for (int i = 0; i < msg.recipCount; i++)

    {

    Marshal.DestroyStructure((IntPtr)ptr, typeof(MapiRecipDesc));

    ptr += size;

    }

    Marshal.FreeHGlobal(msg.recips);

    }

    m_recipients.Clear();

    m_lastError = 0;

    }

    public string GetLastError()

    {

    if (m_lastError <= 26)

    return errors[m_lastError];

    return " MAPI error [" + m_lastError.ToString() + " ]";

    }

    public readonly string[] errors = new string[]

    {

    " OK [0]" , " User abort [1]" , " General MAPI failure [2]" ,

    " MAPI login failure [3]" , " Disk full [4]" ,

    " Insufficient memory [5]" , " Access denied Devil" ,

    " -unknown- [7]" , " Too many sessions Music" ,

    " Too many files were specified [9]" ,

    " Too many recipients were specified [10]" ,

    " A specified attachment was not found [11]" ,

    " Attachment open failure [12]" ,

    " Attachment write failure [13]" , " Unknown recipient [14]" ,

    " Bad recipient type [15]" , " No messages [16]" ,

    " Invalid message [17]" , " Text too large [18]" ,

    " Invalid session [19]" , " Type not supported [20]" ,

    " A recipient was specified ambiguously [21]" ,

    " Message in use [22]" , " Network failure [23]" ,

    " Invalid edit fields [24]" , " Invalid recipients [25]" ,

    " Not supported [26]"

    };

     

    List<MapiRecipDesc> m_recipients = new List<MapiRecipDesc>();

    int m_lastError = 0;

    const int MAPI_LOGON_UI = 0x00000001;

    const int MAPI_DIALOG = 0x00000008;

    enum HowTo { MAPI_ORIG = 0, MAPI_TO };

    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]

    public class MapiMessage

    {

    public int reserved;

    public string subject;

    public string noteText;

    public string messageType;

    public string dateReceived;

    public string conversationID;

    public int flags;

    public IntPtr originator;

    public int recipCount;

    public IntPtr recips;

    public int fileCount;

    public IntPtr files;

    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]

    public class MapiRecipDesc

    {

    public int reserved;

    public int recipClass;

    public string name;

    public string address;

    public int IDSize;

    public IntPtr entryID;

    }

    }

     

    esse é o evento do botão :

    private void Enviar_Click(object sender, EventArgs e)

    {

    MAPI mapi = new MAPI();

    mapi.AddRecipientTo( "email@exemplo.com.br " );

    mapi.SendMailDirect( " Teste" , " E-mail de Teste" );

    }

     

    Aparece a seguinte mensagem de erro :

    _emptyArray = Cannot fetch the value of field '_emptyArray' because information about the containing class is unavailable.

     

    Esse erro aparece quando faço:

    List<MapiRecipDesc> m_recipients = new List<MapiRecipDesc>();

     

    Se alguem poder me ajudar , ou sober algum outro modo de fazer isso.

    Obrigado .

     

     

    quarta-feira, 28 de janeiro de 2009 13:27

Todas as Respostas