Пример #1
0
            private static void SetField(Object m, String name, String value)
            {
                _helper.SetProp(m, "Fields", "urn:schemas:mailheader:" + name, value);
                Object fields = _helper.GetProp(m, "Fields");

                LateBoundAccessHelper.CallMethodStatic(fields, "Update", new Object[0]);
                Marshal.ReleaseComObject(fields);
            }
Пример #2
0
            internal static void Send(MailMessage message)
            {
                // create message object
                Object m = _helper.CreateInstance();

                // set properties

                if (message.From != null)
                {
                    _helper.SetProp(m, "From", message.From);
                }

                if (message.To != null)
                {
                    _helper.SetProp(m, "To", message.To);
                }

                if (message.Cc != null)
                {
                    _helper.SetProp(m, "Cc", message.Cc);
                }

                if (message.Bcc != null)
                {
                    _helper.SetProp(m, "Bcc", message.Bcc);
                }

                if (message.Subject != null)
                {
                    _helper.SetProp(m, "Subject", message.Subject);
                }


                if (message.Priority != MailPriority.Normal)
                {
                    String importance = null;
                    switch (message.Priority)
                    {
                    case MailPriority.Low:      importance = "low";     break;

                    case MailPriority.Normal:   importance = "normal";  break;

                    case MailPriority.High:     importance = "high";    break;
                    }

                    if (importance != null)
                    {
                        SetField(m, "importance", importance);
                    }
                }

                if (message.BodyEncoding != null)
                {
                    Object body = _helper.GetProp(m, "BodyPart");
                    LateBoundAccessHelper.SetPropStatic(body, "Charset", message.BodyEncoding.BodyName);
                    Marshal.ReleaseComObject(body);
                }

                if (message.UrlContentBase != null)
                {
                    SetField(m, "content-base", message.UrlContentBase);
                }

                if (message.UrlContentLocation != null)
                {
                    SetField(m, "content-location", message.UrlContentLocation);
                }

                int numHeaders = message.Headers.Count;

                if (numHeaders > 0)
                {
                    IDictionaryEnumerator e = message.Headers.GetEnumerator();
                    while (e.MoveNext())
                    {
                        SetField(m, (String)e.Key, (String)e.Value);
                    }
                }

                if (message.Body != null)
                {
                    if (message.BodyFormat == MailFormat.Html)
                    {
                        _helper.SetProp(m, "HtmlBody", message.Body);
                    }
                    else
                    {
                        _helper.SetProp(m, "TextBody", message.Body);
                    }
                }

                for (IEnumerator e = message.Attachments.GetEnumerator(); e.MoveNext();)
                {
                    MailAttachment a        = (MailAttachment)e.Current;
                    Object         bodyPart = _helper.CallMethod(m, "AddAttachment", new Object[3] {
                        a.Filename, null, null
                    });

                    if (a.Encoding == MailEncoding.UUEncode)
                    {
                        _helper.SetProp(m, "MimeFormatted", false);
                    }

                    if (bodyPart != null)
                    {
                        Marshal.ReleaseComObject(bodyPart);
                    }
                }

                // optional SMTP server
                String server = SmtpMail.SmtpServer;

                if (server.Length == 0)
                {
                    server = null;
                }

                if (server != null || message.Fields.Count > 0)
                {
                    Object config = LateBoundAccessHelper.GetPropStatic(m, "Configuration");

                    if (config != null)
                    {
                        if (server != null)
                        {
                            LateBoundAccessHelper.SetPropStatic(config, "Fields", "http://schemas.microsoft.com/cdo/configuration/sendusing", (Object)2);
                            LateBoundAccessHelper.SetPropStatic(config, "Fields", "http://schemas.microsoft.com/cdo/configuration/smtpserver", server);
                            LateBoundAccessHelper.SetPropStatic(config, "Fields", "http://schemas.microsoft.com/cdo/configuration/smtpserverport", (Object)25);
                        }

                        foreach (DictionaryEntry e in message.Fields)
                        {
                            LateBoundAccessHelper.SetPropStatic(config, "Fields", (String)e.Key, e.Value);
                        }

                        Object fields = LateBoundAccessHelper.GetPropStatic(config, "Fields");
                        LateBoundAccessHelper.CallMethodStatic(fields, "Update", new Object[0]);
                        Marshal.ReleaseComObject(fields);

                        Marshal.ReleaseComObject(config);
                    }
                }

                // send mail
                _helper.CallMethod(m, "Send", new Object[0]);

                // close unmanaged COM classic component
                Marshal.ReleaseComObject(m);
            }