也许我们习惯用通过使用System.Net.Mail中的相关类进行邮件的发送。

这其中,需要我们手动指定邮件服务器、发件人地址进行邮件的发送。

而在SharePoint中,这些东西都可以通过应用程序》传出电子邮件来进行设置。那么,如果我们要将.Net.Mail与SharePoint结合起来,该怎么做呢?

关键就在于,如何通过SP的对象模型获取到设置的服务器、地址等数据,下面是我一些小的总结,希望能对大家有所帮助:

//第一步:获取应用程序,因为 传出电子邮件设置是在应用程序中进行的 site.WebApplication
Microsoft.SharePoint.Administration.SPWebApplication webApp = site.WebApplication;
//第二步:获取邮件服务器主机名 webApp.OutboundMailServiceInstance.Server.Name
client.Host = webApp.OutboundMailServiceInstance.Server.Name;
//第三步:获取发送人地址:webApp.OutboundMailSenderAddress;
//第四步:获取邮件编码 :
message.BodyEncoding = System.Text.Encoding.GetEncoding(webApp.OutboundMailCodePage);

----在我目前的工作中,碰到这样一个场景:在某个列表中添加一项列表项后,自动获取列表中的“收件人”和“发件人”字段进行邮件发送。
因此,我写了如下的测试代码,

该测试代码实现:
1、选择列表项字段“收件人”作为邮件收件人,选择列表项字段“抄送发”作为邮件抄送人,值得一提的是,在SP中,这2个被设置成“用户或用户组”后,还可以选择组进行发送目标,这一个非常爽。
2、自动将列表项的附件作为邮件附件发送。
特别注意的是,虽然我此处是写的一个列表事件处理程序,但是,邮件的发送方法以及传出电子邮件设置的获取是通过型的。

运行通过的代码:
  1.   1using System;
  2.   2using System.Collections.Generic;
  3.   3using System.Text;
  4.   4using System.IO;
  5.   5using Microsoft.SharePoint;
  6.   6using Microsoft.SharePoint.Utilities;
  7.   7using System.Net.Mail;
  8.   8namespace ListMailEvent
  9.   9{
  10. 10    /**//// <summary>
  11. 11    /// SharePoint 邮件发送示例程序
  12. 12    ///  [url]www.cnblogs.com/mcjeremy[/url]
  13. 13    /// </summary>
  14. 14    public class ListMailEvent: SPItemEventReceiver
  15. 15    {
  16. 16       
  17. 17        public override void ItemAdded(SPItemEventProperties properties)
  18. 18        {
  19. 19            //邮件主题
  20. 20            string mailSubject = string.Empty;
  21. 21            //邮件正文
  22. 22            string mailBody = string.Empty;
  23. 23
  24. 24            using (SPSite site = new SPSite(properties.SiteId))
  25. 25            {
  26. 26                using (SPWeb web = site.OpenWeb(site.OpenWeb().ID))
  27. 27                {
  28. 28                List<string> userEmails = new List<string>(); //收件人
  29. 29                    List<string> choiceUserEmails = new List<string>(); //抄送人
  30. 30                    try
  31. 31                    {
  32. 32                        SPList meetingList = web.Lists[properties.ListId];
  33. 33                        SPListItem meetingItem = meetingList.GetItemById(properties.ListItemId);
  34. 34                        if (null != meetingItem["收件人"])
  35. 35                        {
  36. 36                            SPFieldUserValueCollection values = meetingItem["收件人"] as SPFieldUserValueCollection;
  37. 37                            GetUsersFromUserField(web, userEmails, values);
  38. 38                        }
  39. 39                        if (null != meetingItem["抄送人"])
  40. 40                        {
  41. 41                            SPFieldUserValueCollection values_options = meetingItem["抄送人"] as SPFieldUserValueCollection;
  42. 42                            GetUsersFromUserField(web, choiceUserEmails, values_options);
  43. 43                        }
  44. 44                        if (userEmails.Count <= 0)
  45. 45                            return;
  46. 46                        mailBody="<h1>这是测试的正文</h1>";
  47. 47            mailSubject="测试邮件"
  48. 48                        SendMail(mailSubject, mailBody, userEmails, choiceUserEmails,site, web, meetingItem);
  49. 49                       
  50. 50                    }
  51. 51                    catch(Exception eee)
  52. 52                    {
  53. 53                        return;
  54. 54                    }
  55. 55                }
  56. 56            }
  57. 57        }
  58. 58        public void GetUsersFromUserField(SPWeb web, List<string> userEmails, SPFieldUserValueCollection values)
  59. 59        {
  60. 60            if (values.Count <= 0 || null==values)
  61. 61                return;
  62. 62            foreach (SPFieldUserValue userValue in values)
  63. 63            {
  64. 64                try
  65. 65                {
  66. 66                    if (userValue.User != null)
  67. 67                    {
  68. 68                        userEmails.Add(userValue.User.Email);
  69. 69                    }
  70. 70                    else
  71. 71                    {
  72. 72  //可以选择用户组进行发送
  73. 73                        SPGroup group = web.SiteGroups.GetByID(userValue.LookupId);
  74. 74                        foreach (SPUser user in group.Users)
  75. 75                        {
  76. 76                            userEmails.Add(user.Email);
  77. 77                        }
  78. 78                    }
  79. 79                }
  80. 80                catch
  81. 81                {
  82. 82                    continue;
  83. 83                }
  84. 84            }
  85. 85        }
  86. 86        public void SendMail(string Title, string Body,  List<string> userEmails, List<string> choiceUserEmails,SPSite site, SPWeb web, SPListItem item)
  87. 87        {
  88. 88    //获取应用程序,因为 传出电子邮件设置是在应用程序中进行的 site.WebApplication
  89. 89            Microsoft.SharePoint.Administration.SPWebApplication webApp = site.WebApplication;
  90. 90            System.Net.Mail.SmtpClient client = new SmtpClient();
  91. 91            //获取邮件服务器主机名 webApp.OutboundMailServiceInstance.Server.Name
  92. 92            client.Host = webApp.OutboundMailServiceInstance.Server.Name;
  93. 93            //设置Credentials
  94. 94            client.UseDefaultCredentials = true;
  95. 95            client.DeliveryMethod = SmtpDeliveryMethod.Network;
  96. 96
  97. 97          //获取发件人邮件地址  webApp.OutboundMailSenderAddress
  98. 98            string senderName = webApp.OutboundMailSenderAddress;
  99. 99
  100. 100            System.Net.Mail.MailMessage message = new MailMessage();                 
  101. 101            message.From=new MailAddress(senderName);
  102. 102            /**//*添加收件人*/
  103. 103            foreach(string userEmail in userEmails)
  104. 104            {
  105. 105                message.To.Add(userEmail);
  106. 106            }
  107. 107            /**//*添加抄送,达到可选效果*/
  108. 108            foreach(string userEmail in choiceUserEmails)
  109. 109            {
  110. 110                message.CC.Add(userEmail);
  111. 111            }
  112. 112            message.Subject = Title;
  113. 113            message.Body = Body;           
  114. 114            try
  115. 115            {
  116. 116    //获取传出电子邮件地址中的邮件编码  webApp.OutboundMailCodePage
  117. 117                message.BodyEncoding = System.Text.Encoding.GetEncoding(webApp.OutboundMailCodePage);
  118. 118            }
  119. 119            catch (System.Exception ex)            {    }                        message.IsBodyHtml = true;
  120. 120//获取列表项的附件作为邮件附件一起发送         
  121. 121            if(item.Attachments.Count>0)
  122. 122            {
  123. 123                SPAttachmentCollection attaches = item.Attachments;
  124. 124                for (int i = 0; i < attaches.Count; i++)
  125. 125                {
  126. 126                    string url = attaches.UrlPrefix + attaches;
  127. 127                    SPFile file = web.GetFile(url);                   
  128. 128                    Attachment mailAttach = new Attachment(file.OpenBinaryStream(),System.Net.Mime.MediaTypeNames.Application.Octet);
  129. 129                    System.Net.Mime.ContentDisposition cd = mailAttach.ContentDisposition;
  130. 130                    cd.FileName = file.Name;    //记得此处             
  131. 131                    message.Attachments.Add(mailAttach);                 
  132. 132                }
  133. 133            }
  134. 134            try
  135. 135            {
  136. 136                client.Send(message);
  137. 137            }
  138. 138            catch (Exception ex)    {            }
  139. 139        }
  140. 140    }
  141. 141}
  142. 142
复制代码
文/McJeremy&Fan
TOP