• SharePoint 中调用传出电子邮件中的邮件服务器及地址发送邮件


    也许我们习惯用通过使用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、自动将列表项的附件作为邮件附件发送。
    特别注意的是,虽然我此处是写的一个列表事件处理程序,但是,邮件的发送方法以及传出电子邮件设置的获取是通过型的。

    运行通过的代码:

      1using System;
      2using System.Collections.Generic;
      3using System.Text;
      4using System.IO;
      5using Microsoft.SharePoint;
      6using Microsoft.SharePoint.Utilities;
      7using System.Net.Mail;
      8namespace ListMailEvent
      9{
     10    /// <summary>
     11    /// SharePoint 邮件发送示例程序
     12    ///  www.cnblogs.com/mcjeremy
     13    /// </summary>

     14    public class ListMailEvent: SPItemEventReceiver
     15    {
     16        
     17        public override void ItemAdded(SPItemEventProperties properties)
     18        
     19            //邮件主题
     20            string mailSubject = string.Empty;
     21            //邮件正文
     22            string mailBody = string.Empty;
     23
     24            using (SPSite site = new SPSite(properties.SiteId))
     25            {
     26                using (SPWeb web = site.OpenWeb(site.OpenWeb().ID))
     27                {
     28                 List<string> userEmails = new List<string>(); //收件人
     29                    List<string> choiceUserEmails = new List<string>(); //抄送人
     30                    try
     31                    {
     32                        SPList meetingList = web.Lists[properties.ListId];
     33                        SPListItem meetingItem = meetingList.GetItemById(properties.ListItemId);
     34                        if (null != meetingItem["收件人"])
     35                        {
     36                            SPFieldUserValueCollection values = meetingItem["收件人"as SPFieldUserValueCollection;
     37                            GetUsersFromUserField(web, userEmails, values);
     38                        }

     39                        if (null != meetingItem["抄送人"])
     40                        {
     41                            SPFieldUserValueCollection values_options = meetingItem["抄送人"as SPFieldUserValueCollection;
     42                            GetUsersFromUserField(web, choiceUserEmails, values_options);
     43                        }

     44                        if (userEmails.Count <= 0)
     45                            return;
     46                        mailBody="<h1>这是测试的正文</h1>";
     47            mailSubject="测试邮件" 
     48                        SendMail(mailSubject, mailBody, userEmails, choiceUserEmails,site, web, meetingItem);
     49                        
     50                    }

     51                    catch(Exception eee)
     52                    {
     53                        return;
     54                    }

     55                }

     56            }

     57        }

     58        public void GetUsersFromUserField(SPWeb web, List<string> userEmails, SPFieldUserValueCollection values)
     59        {
     60            if (values.Count <= 0 || null==values)
     61                return;
     62            foreach (SPFieldUserValue userValue in values)
     63            {
     64                try
     65                {
     66                    if (userValue.User != null)
     67                    {
     68                        userEmails.Add(userValue.User.Email);
     69                    }

     70                    else
     71                    {
     72  //可以选择用户组进行发送
     73                        SPGroup group = web.SiteGroups.GetByID(userValue.LookupId);
     74                        foreach (SPUser user in group.Users)
     75                        {
     76                            userEmails.Add(user.Email);
     77                        }

     78                    }

     79                }

     80                catch
     81                {
     82                    continue;
     83                }

     84            }

     85        }

     86        public void SendMail(string Title, string Body,  List<string> userEmails, List<string> choiceUserEmails,SPSite site, SPWeb web, SPListItem item)
     87        {
     88    //获取应用程序,因为 传出电子邮件设置是在应用程序中进行的 site.WebApplication
     89            Microsoft.SharePoint.Administration.SPWebApplication webApp = site.WebApplication;
     90            System.Net.Mail.SmtpClient client = new SmtpClient();
     91            //获取邮件服务器主机名 webApp.OutboundMailServiceInstance.Server.Name
     92            client.Host = webApp.OutboundMailServiceInstance.Server.Name;
     93            //设置Credentials
     94            client.UseDefaultCredentials = true;
     95            client.DeliveryMethod = SmtpDeliveryMethod.Network;
     96
     97           //获取发件人邮件地址  webApp.OutboundMailSenderAddress
     98            string senderName = webApp.OutboundMailSenderAddress;
     99
    100            System.Net.Mail.MailMessage message = new MailMessage();                  
    101            message.From=new MailAddress(senderName);
    102            /*添加收件人*/
    103            foreach(string userEmail in userEmails)
    104            {
    105                message.To.Add(userEmail);
    106            }

    107            /*添加抄送,达到可选效果*/
    108            foreach(string userEmail in choiceUserEmails)
    109            {
    110                message.CC.Add(userEmail);
    111            }

    112            message.Subject = Title;
    113            message.Body = Body;            
    114            try
    115            
    116    //获取传出电子邮件地址中的邮件编码  webApp.OutboundMailCodePage
    117                message.BodyEncoding = System.Text.Encoding.GetEncoding(webApp.OutboundMailCodePage);
    118            }

    119            catch (System.Exception ex)            {    }                        message.IsBodyHtml = true
    120//获取列表项的附件作为邮件附件一起发送           
    121            if(item.Attachments.Count>0)
    122            {
    123                SPAttachmentCollection attaches = item.Attachments;
    124                for (int i = 0; i < attaches.Count; i++)
    125                {
    126                    string url = attaches.UrlPrefix + attaches[i];
    127                    SPFile file = web.GetFile(url);                    
    128                    Attachment mailAttach = new Attachment(file.OpenBinaryStream(),System.Net.Mime.MediaTypeNames.Application.Octet);
    129                    System.Net.Mime.ContentDisposition cd = mailAttach.ContentDisposition;
    130                    cd.FileName = file.Name;     //记得此处               
    131                    message.Attachments.Add(mailAttach);                   
    132                }

    133            }

    134            try
    135            {
    136                client.Send(message);
    137            }

    138            catch (Exception ex)    {            }
    139        }

    140    }

    141}

    142

    以上代码,纯属原创。
    欢迎转载,但请注明出处:http://www.cnblogs.com/mcjeremy

    <h3>
       心静似高山流水不动,心清若巫峰雾气不沾。
    </h3>
  • 相关阅读:
    python笔记之发送邮件
    Python基础
    Python操作csv文件
    Python之操作Excel、异常处理、网络编程
    Python操作数据库及hashlib模块
    函数、装饰器、模块
    Python之三元运算、集合、函数
    python基础--字符串操作、列表、元组、文件操作
    抓包工具Fiddler及Charles
    input type=number maxlength无效
  • 原文地址:https://www.cnblogs.com/McJeremy/p/1515441.html
Copyright © 2020-2023  润新知