none
Is there a way to create an email message from a Forms app? RRS feed

  • Question

  • Hi all;

    In a web app giving someone a way to send us an email is easy - we just add in link to name@domain.com. and they click on it.

    But from a Forms app, everything I have tried requires that we know the name of their mail server. And most users don't know it so even if we prompted, it wouldn't work.

    Is there a way to send an email to us from a Forms app without having to ask them for any info. Or is there a way to launch an Outlook message with the To, Subject, and Message pre-populated, but they then have to click Send?

    thanks - dave


    Who will win The International Collegiate Programming Championships?

    Monday, June 18, 2012 9:21 PM

Answers

  • Use SNTPClient class.  SNTP has two method that can be used

    1) Send Mail over the network using a mail server

    2) Use the credential setup on the computer (normally a POP3 accunt or the credentials outlook normally uses).  You can see the users mail credentials by going to Control Panel - Mail.

    The property below will use the login credentials that is setup in control Panel

    client.Credentials = CredentialCache.DefaultNetworkCredentials;

    Sample code is on the webpage below.

    http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.credentials


    jdweng

    Monday, June 18, 2012 10:06 PM
  • Hi, 

    You can use MAPISendMail function to open mail message in default mail provider in client machine with included details.

    Pseudo code,

      [DllImport("MAPI32.DLL")]
      static extern int MAPISendMail(IntPtr sess, IntPtr hwnd, MapiMessage message, int flg, int rsv);

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] 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)]
        class MapiRecipDesc
        {
            public int reserved;
            public int recipClass;
            public string name;
            public string address;
            public int eIDSize;
            public IntPtr entryID;
        }

    // Describes mail details 

      public class MailDetails : IDisposable
        {
            internal IntPtr Handle { get; set; }
            public void SetWindowHandle(IntPtr handle)
            {
                this.Handle = handle;
            }
            public MailDetails()
            {
                FromAddress = string.Empty;
                Subject = string.Empty;
                Body = string.Empty;
            }

            public void SetSubject(string subject)
            {
                this.Subject = subject;
            }

            public void SetMessageBody(string body)
            {
                this.Body = body;
            }
            internal string FromAddress { get; set; }//def empty
            public void SetFromAddress(string address)
            {
                FromAddress = address;
            }
            IList<MapiRecipDesc> listOfToAddress = new List<MapiRecipDesc>();

            internal IList<MapiRecipDesc> GetToAddresses() { return listOfToAddress; }
            public void AddToAddress(string name, string emailAddress)
            {
                MapiRecipDesc recipient = new MapiRecipDesc();

                recipient.recipClass = 1;
                recipient.address = string.Format("SMTP:{0}", emailAddress);
                recipient.name = name;
                listOfToAddress.Add(recipient);
            }
            internal string Subject { get; set; }//default empty
            internal string Body { get; set; }//default empty
            public void AddToAddress(string emailAddress)
            {
                AddToAddress(emailAddress.Substring(0, emailAddress.IndexOf("@")), emailAddress);
            }

            #region IDisposable Members

            public void Dispose()
            {
                this.listOfToAddress.Clear();
            }

            #endregion
        }
         
         private IntPtr GetRecipients(MailDetails mailDetails, out int recipCount)
            {
                int size = Marshal.SizeOf(typeof(MapiRecipDesc));
                IList<MapiRecipDesc> listOfAddresses = mailDetails.GetToAddresses();
                IntPtr intPtr = Marshal.AllocHGlobal(listOfAddresses.Count * size);

                int ptr = (int)intPtr;
                foreach (MapiRecipDesc mapiDesc in listOfAddresses)
                {
                    Marshal.StructureToPtr(mapiDesc, (IntPtr)ptr, false);
                    ptr += size;
                }
                recipCount = listOfAddresses.Count;
                return intPtr;
            }

    // and use like

    MapiMessage msg = new MapiMessage(); try { msg.subject = mailDetails.Subject; msg.noteText = mailDetails.Body; msg.recips = GetRecipients(mailDetails, out msg.recipCount); int resultCode = MAPISendMail(new IntPtr(0), new IntPtr(0), msg, MAPI_LOGON_UI | MAPI_NEW_SESSION | MAPI_DIALOG, 0); }


    I Hope this helps you.. 


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Tuesday, June 19, 2012 3:23 AM
  • Be aware this is the forum for the base class of Net and related questions System.Object

    You can say everything is related to that, yes it is even the new surface from Microsoft.

    However, mail is very complicated if you know that Outlook can mean Outlook Express and Office Outlook and that there are beside that at least 10 differen mail systems which need completely different implementation then you know that it is difficult.

    However you can always use the default mail client solution, problem with that it has no attachments. 

    http://www.vb-tips.com/DefaultMailClient.aspx

    In my perception is what I give you now Of Topic for this forum.


    Success
    Cor

    Tuesday, June 19, 2012 5:44 AM

All replies

  • Use SNTPClient class.  SNTP has two method that can be used

    1) Send Mail over the network using a mail server

    2) Use the credential setup on the computer (normally a POP3 accunt or the credentials outlook normally uses).  You can see the users mail credentials by going to Control Panel - Mail.

    The property below will use the login credentials that is setup in control Panel

    client.Credentials = CredentialCache.DefaultNetworkCredentials;

    Sample code is on the webpage below.

    http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.credentials


    jdweng

    Monday, June 18, 2012 10:06 PM
  • Hi, 

    You can use MAPISendMail function to open mail message in default mail provider in client machine with included details.

    Pseudo code,

      [DllImport("MAPI32.DLL")]
      static extern int MAPISendMail(IntPtr sess, IntPtr hwnd, MapiMessage message, int flg, int rsv);

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] 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)]
        class MapiRecipDesc
        {
            public int reserved;
            public int recipClass;
            public string name;
            public string address;
            public int eIDSize;
            public IntPtr entryID;
        }

    // Describes mail details 

      public class MailDetails : IDisposable
        {
            internal IntPtr Handle { get; set; }
            public void SetWindowHandle(IntPtr handle)
            {
                this.Handle = handle;
            }
            public MailDetails()
            {
                FromAddress = string.Empty;
                Subject = string.Empty;
                Body = string.Empty;
            }

            public void SetSubject(string subject)
            {
                this.Subject = subject;
            }

            public void SetMessageBody(string body)
            {
                this.Body = body;
            }
            internal string FromAddress { get; set; }//def empty
            public void SetFromAddress(string address)
            {
                FromAddress = address;
            }
            IList<MapiRecipDesc> listOfToAddress = new List<MapiRecipDesc>();

            internal IList<MapiRecipDesc> GetToAddresses() { return listOfToAddress; }
            public void AddToAddress(string name, string emailAddress)
            {
                MapiRecipDesc recipient = new MapiRecipDesc();

                recipient.recipClass = 1;
                recipient.address = string.Format("SMTP:{0}", emailAddress);
                recipient.name = name;
                listOfToAddress.Add(recipient);
            }
            internal string Subject { get; set; }//default empty
            internal string Body { get; set; }//default empty
            public void AddToAddress(string emailAddress)
            {
                AddToAddress(emailAddress.Substring(0, emailAddress.IndexOf("@")), emailAddress);
            }

            #region IDisposable Members

            public void Dispose()
            {
                this.listOfToAddress.Clear();
            }

            #endregion
        }
         
         private IntPtr GetRecipients(MailDetails mailDetails, out int recipCount)
            {
                int size = Marshal.SizeOf(typeof(MapiRecipDesc));
                IList<MapiRecipDesc> listOfAddresses = mailDetails.GetToAddresses();
                IntPtr intPtr = Marshal.AllocHGlobal(listOfAddresses.Count * size);

                int ptr = (int)intPtr;
                foreach (MapiRecipDesc mapiDesc in listOfAddresses)
                {
                    Marshal.StructureToPtr(mapiDesc, (IntPtr)ptr, false);
                    ptr += size;
                }
                recipCount = listOfAddresses.Count;
                return intPtr;
            }

    // and use like

    MapiMessage msg = new MapiMessage(); try { msg.subject = mailDetails.Subject; msg.noteText = mailDetails.Body; msg.recips = GetRecipients(mailDetails, out msg.recipCount); int resultCode = MAPISendMail(new IntPtr(0), new IntPtr(0), msg, MAPI_LOGON_UI | MAPI_NEW_SESSION | MAPI_DIALOG, 0); }


    I Hope this helps you.. 


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Tuesday, June 19, 2012 3:23 AM
  • Be aware this is the forum for the base class of Net and related questions System.Object

    You can say everything is related to that, yes it is even the new surface from Microsoft.

    However, mail is very complicated if you know that Outlook can mean Outlook Express and Office Outlook and that there are beside that at least 10 differen mail systems which need completely different implementation then you know that it is difficult.

    However you can always use the default mail client solution, problem with that it has no attachments. 

    http://www.vb-tips.com/DefaultMailClient.aspx

    In my perception is what I give you now Of Topic for this forum.


    Success
    Cor

    Tuesday, June 19, 2012 5:44 AM