Click here to Skip to main content
15,915,089 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to remove characters from string value Pin
Not Active23-Jan-11 6:00
mentorNot Active23-Jan-11 6:00 
GeneralRe: How to remove characters from string value Pin
PIEBALDconsult23-Jan-11 6:44
mvePIEBALDconsult23-Jan-11 6:44 
GeneralRe: How to remove characters from string value-[Minor Changes] Pin
RaviRanjanKr25-Jan-11 4:38
professionalRaviRanjanKr25-Jan-11 4:38 
AnswerRe: How to remove characters from string value Pin
Estys23-Jan-11 5:27
Estys23-Jan-11 5:27 
AnswerRe: How to remove characters from string value Pin
RaviRanjanKr23-Jan-11 6:16
professionalRaviRanjanKr23-Jan-11 6:16 
AnswerRe: How to remove characters from string value [modified] Pin
Abhinav S23-Jan-11 6:38
Abhinav S23-Jan-11 6:38 
GeneralRe: How to remove characters from string value Pin
Richard MacCutchan23-Jan-11 8:22
mveRichard MacCutchan23-Jan-11 8:22 
JokeRe: How to remove characters from string value Pin
Not Active23-Jan-11 10:19
mentorNot Active23-Jan-11 10:19 
GeneralRe: How to remove characters from string value Pin
Abhinav S23-Jan-11 17:30
Abhinav S23-Jan-11 17:30 
AnswerRe: How to remove characters from string value Pin
Paladin200024-Jan-11 3:54
Paladin200024-Jan-11 3:54 
QuestionBuild and use user control (vs 2010) Pin
ladoota22-Jan-11 20:13
ladoota22-Jan-11 20:13 
AnswerRe: Build and use user control (vs 2010) Pin
Richard MacCutchan22-Jan-11 21:38
mveRichard MacCutchan22-Jan-11 21:38 
AnswerRe: Build and use user control (vs 2010) Pin
RaviRanjanKr23-Jan-11 17:36
professionalRaviRanjanKr23-Jan-11 17:36 
QuestionRemoving duplicates / extracting unique vals from String array Pin
csrss22-Jan-11 1:25
csrss22-Jan-11 1:25 
AnswerRe: Removing duplicates / extracting unique vals from String array Pin
Luc Pattyn22-Jan-11 1:54
sitebuilderLuc Pattyn22-Jan-11 1:54 
GeneralRe: Removing duplicates / extracting unique vals from String array Pin
csrss22-Jan-11 2:07
csrss22-Jan-11 2:07 
AnswerRe: Removing duplicates / extracting unique vals from String array Pin
dan!sh 22-Jan-11 2:04
professional dan!sh 22-Jan-11 2:04 
GeneralRe: Removing duplicates / extracting unique vals from String array Pin
csrss22-Jan-11 2:09
csrss22-Jan-11 2:09 
AnswerRe: Removing duplicates / extracting unique vals from String array Pin
PIEBALDconsult22-Jan-11 3:30
mvePIEBALDconsult22-Jan-11 3:30 
AnswerRe: Removing duplicates / extracting unique vals from String array [modified] Pin
DaveyM6922-Jan-11 5:47
professionalDaveyM6922-Jan-11 5:47 
GeneralRe: Removing duplicates / extracting unique vals from String array Pin
Mirko198024-Jan-11 22:11
Mirko198024-Jan-11 22:11 
QuestionPlease Check Pin
M Riaz Bashir21-Jan-11 23:25
M Riaz Bashir21-Jan-11 23:25 
Hi, Friends. and Mr Danish

These following works very well in Windows XP but when I execute this class in Windows 7, it does'nt work. Frown | :(

Kindly check it and reply me.

Thank you
(Riaz)

class MAPI
{
    public bool AddRecipientTo(string email)
    {
        return AddRecipient(email, HowTo.MAPI_TO);
    }

    public bool AddRecipientCC(string email)
    {
        return AddRecipient(email, HowTo.MAPI_TO);
    }

    public bool AddRecipientBCC(string email)
    {
        return AddRecipient(email, HowTo.MAPI_TO);
    }

    public void AddAttachment(string strAttachmentFileName)
    {
        m_attachments.Add(strAttachmentFileName);
    }

    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("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);
        msg.files = GetAttachments(out msg.fileCount);

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

    IntPtr GetAttachments(out int fileCount)
    {
        fileCount = 0;
        if (m_attachments == null)
            return IntPtr.Zero;

        if ((m_attachments.Count <= 0) || (m_attachments.Count > maxAttachments))
            return IntPtr.Zero;

        int size = Marshal.SizeOf(typeof(MapiFileDesc));
        IntPtr intPtr = Marshal.AllocHGlobal(m_attachments.Count * size);

        MapiFileDesc mapiFileDesc = new MapiFileDesc();
        mapiFileDesc.position = -1;
        int ptr = (int)intPtr;

        foreach (string strAttachment in m_attachments)
        {
            mapiFileDesc.name = Path.GetFileName(strAttachment);
            mapiFileDesc.path = strAttachment;
            Marshal.StructureToPtr(mapiFileDesc, (IntPtr)ptr, false);
            ptr += size;
        }

        fileCount = m_attachments.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);
        }

        if (msg.files != IntPtr.Zero)
        {
            size = Marshal.SizeOf(typeof(MapiFileDesc));

            ptr = (int)msg.files;
            for (int i = 0; i < msg.fileCount; i++)
            {
                Marshal.DestroyStructure((IntPtr)ptr, typeof(MapiFileDesc));
                ptr += size;
            }
            Marshal.FreeHGlobal(msg.files);
        }

        m_recipients.Clear();
        m_attachments.Clear();
        m_lastError = 0;
    }

    public string GetLastError()
    {
        if (m_lastError <= 26)
            return errors[m_lastError];
        return "MAPI error [" + m_lastError.ToString() + "]";
    }

    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 [6]", "-unknown- [7]",
    "Too many sessions [8]", "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>();
    List<string> m_attachments = new List<string>();
    int m_lastError = 0;

    const int MAPI_LOGON_UI = 0x00000001;
    const int MAPI_DIALOG = 0x00000008;
    const int maxAttachments = 20;

    enum HowTo { MAPI_ORIG = 0, MAPI_TO, MAPI_CC, MAPI_BCC };
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
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.Ansi)]
public class MapiFileDesc
{
    public int reserved;
    public int flags;
    public int position;
    public string path;
    public string name;
    public IntPtr type;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class MapiRecipDesc
{
    public int reserved;
    public int recipClass;
    public string name;
    public string address;
    public int eIDSize;
    public IntPtr entryID;
}

AnswerRe: Please Check Pin
OriginalGriff21-Jan-11 23:42
mveOriginalGriff21-Jan-11 23:42 
GeneralRe: Please Check Pin
M Riaz Bashir21-Jan-11 23:48
M Riaz Bashir21-Jan-11 23:48 
GeneralRe: Please Check Pin
Henry Minute22-Jan-11 4:00
Henry Minute22-Jan-11 4:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.