在Sharepoint中发送电子邮件

时间:2009-07-03 00:24:55

标签: sharepoint email sharepoint-2007 outlook email-integration

我需要知道从我的sharepoint webparts和/或自定义功能发送电子邮件的最佳做法是什么。

我应该只使用普通的.Net类来发送电子邮件吗?或者是通过与Outlook服务器集成来实现它的更好方法吗?

4 个答案:

答案 0 :(得分:16)

简单的方法是使用内置的实用程序,然后使用中央管理员中的邮件服务器设置

using Microsoft.SharePoint.Utilities;
SPUtility.SendEmail(SPContext.Current.Web, false, false,
     "toaddress@mail.com", "subject",
     "body");

答案 1 :(得分:1)

OutboundMailService中使用的SPUtility设置读取在任何上下文(SPWeb不可用)中发送电子邮件的通用方式。然后手动创建SmtpClient:

var adminApp = SPAdministrationWebApplication.Local;
var instance = adminApp.OutboundMailServiceInstance;

var server = instance.Server.Address;
var defaultFrom = adminApp.OutboundMailSenderAddress;

var client = new SmtpClient();
client.Host = server;
message.From = new MailAddress(defaultFrom );
...

答案 2 :(得分:0)

您还可以将此代码用于动态邮件ID。此代码根据用户获取邮件。我使用CAML查询从列表中获取电子邮件内容的数据。

SPUser AssigUser = oWeb.EnsureUser(Assigned_Username);
SPQuery mquery = new SPQuery();
mquery.Query = @"<Where><Eq><FieldRef Name='Email_x0020_Type' />
                    <Value Type='Text'>Review - Ready for Review</Value>
                 </Eq></Where>";
string Emailsub = "";
string Emailbody = "";
SPList mList = oWeb.Lists["Email Content"];
SPListItemCollection itemcollection = mList.GetItems(mquery);
foreach (SPListItem item in itemcollection)
{
    Emailsub = item["Email Subject"].ToString();
    Emailbody = item["Email Content"].ToString();
    SPUtility.SendEmail(oWeb, false, false, AssigUser.Email, Emailsub,
                        Emailbody + "</br>" + oWeb.Url);
    break;
}

答案 3 :(得分:0)

使用带有StringDictionary参数的重载(source

  StringDictionary headers = new StringDictionary();                             
  headers.Add("to", currCtxt.Web.CurrentUser.Email);
  headers.Add("cc", "xyz@abc.com");
  headers.Add("bcc", "");
  headers.Add("from", "email@add.com");
  headers.Add("subject", "Email Subject");
  headers.Add("content-type", "text/html");
  string bodyText = "Hello how are you?";
  SPUtility.SendEmail(currCtxt.Web, headers, bodyText.ToString());