首页 > 其他 > 详细

Outlook 开发2

时间:2014-02-28 23:40:11      阅读:683      评论:0      收藏:0      [点我收藏+]

转自:http://www.cnblogs.com/madebychina/archive/2011/09/20/madebychina_2.html

 

 

C#使用如下代码调用Outlook2003发送邮件:

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Create the Outlook application.
                Outlook.Application  oApp = new Outlook.Application();
                //Outlook.ApplicationClass  oApp = new Outlook.ApplicationClass();
 
                // Get the NameSpace and Logon information.
                Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
 
                // Log on by using a dialog box to choose the profile.
                oNS.Logon(Missing.Value, Missing.Value, true, true);
 
                // Alternate logon method that uses a specific profile.
                // TODO: If you use this logon method,
                //  change the profile name to an appropriate value.
                //oNS.Logon("YourValidProfile", Missing.Value, false, true);
             
                // Create a new mail item.
                Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
 
                // Set the subject.
                oMsg.Subject = "mail";
                //oMsg.Attachments.Add("C:\\mailLog.txt", Outlook.OlItemType.olMailItem, 1, "report");
 
                // Set HTMLBody.
                String sHtml;
                sHtml = "<HTML>\n" +
                    "<HEAD>\n" +
                    "<TITLE>Sample GIF</TITLE>\n" +
                    "</HEAD>\n" +
                    "<BODY><P>\n" +
                    "<h1><Font Color=Green>Inline graphics</Font></h1></P>\n" +
                    "</BODY>\n" +
                    "</HTML>";
                oMsg.HTMLBody = sHtml;
 
                // Add a recipient.
                Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
                // TODO: Change the recipient in the next line if necessary.
                string reciMailAdress=this.txtMail.Text;
                Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(reciMailAdress);
                oRecip.Resolve();
 
                // Send.
                oMsg.Send();
                //((Microsoft.Office.Interop.Outlook._MailItem)oMsg).Send();
 
                // Log off.
                oNS.Logoff();
 
                // Clean up.
                oRecip = null;
                oRecips = null;
                oMsg = null;
                oNS = null;
                oApp = null;

  

 

  但是因为Outlook2003的安全机制,总是会弹出安全提示对话框,如下:

bubuko.com,布布扣

  而且在我们选择允许访问,点击“是”按钮后还会弹出另外一个对话框,如下:

bubuko.com,布布扣

  并且“是”按钮还会被冻结5秒钟。分析原因是我们使用的是Outlook.MailItem邮件模式,这是一种非安全的模式,所以在第三方程序调用Outlook2003时会弹出确认对话框,如何来避免弹出安全提示对话框呢?我们可以使用SafeMailItem模式。但是在使用这种模式时需要引用Redemption.dll,而且还要安装Redemption插件,代码如下:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
private void Send()
        {
            Outlook.Application olApp = new Outlook.ApplicationClass();
            Outlook.NameSpace olNS = olApp.GetNamespace("MAPI");
            olNS.Logon(Missing.Value, Missing.Value, false, false);
            SafeMailItem sItem = new Redemption.SafeMailItem();
 
            Outlook.MailItem olItem = olApp.CreateItem(0) as Outlook.MailItem;
 
            String sHtml;
            sHtml = "<HTML>\n" +
                "<HEAD>\n" +
                "<TITLE>Sample GIF</TITLE>\n" +
                "</HEAD>\n" +
                "<BODY><P>\n" +
                "<h1><Font Color=Green>Inline graphics</Font></h1></P>\n" +
                "</BODY>\n" +
                "</HTML>";
            olItem.HTMLBody = sHtml;
 
            sItem.Item = olItem;
 
            string reciMailAdress=txtMail.Text;
            sItem.Recipients.Add(reciMailAdress);
            sItem.Recipients.ResolveAll();
            SetPropValue(sItem, "Subject", "Testing Redemption");
            sItem.Send();
        }
 
        private static object GetPropValue(object item, string propertyName)
        {
 
            try
            {
                object[] args = new Object[]{}; // dummy argument array
                Type type = item.GetType();
                return type.InvokeMember(
                    propertyName,
                    BindingFlags.Public | BindingFlags.GetField
                    | BindingFlags.GetProperty,
                    null,
                    item,
                    args);
            }
            catch (SystemException ex)
            {
                //                Console.WriteLine(
                //                    string.Format(
                //                    "GetPropValue for {0} Exception: {1} ",
                //                    propertyName, ex.Message));
            }
            return null;
        }
        private static void SetPropValue(object item, string propertyName,object propertyValue)
        {
            try
            {
                object[] args = new Object[1];
                args[0] = propertyValue;
                Type type = item.GetType();
                type.InvokeMember(propertyName,
                    BindingFlags.Public | BindingFlags.SetField |
                    BindingFlags.SetProperty,
                    null,
                    item,
                    args);
            }
            catch (SystemException ex)
            {
                //                Console.WriteLine(
                //                    string.Format(
                //                    "SetPropValue for {0} Exception: {1} ",
                //                    propertyName, ex.Message));
            }
            return;
        }

  

 

  这样就能避免弹出对话框了,其他高版本的Outlook好像没有此问题,Outlook2007在开启反病毒软件时不会弹出对话框。

  Redemption下载地址:http://www.dimastr.com/redemption/download.htm

  本文参考资料地址:http://www.outlookcode.com/article.aspx?id=52

           http://www.dotnet247.com/247reference/message.aspx?id=92601

Outlook 开发2,布布扣,bubuko.com

Outlook 开发2

原文:http://www.cnblogs.com/ifreesoft/p/3573127.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!