1: internal static void SendEmail_Client(EmailProperties properties)
2: {
3: SPWeb web = SPContext.Current.Web;
4: if (web == null)
5: {
6: throw new SPException(SPResource.GetString("ContextWebNotFound",
7: new object[0]));
8: }
9: if (properties.To.Count == 0)
10: {
11: throw SPUtility.GetInvalidRecipientsException(web.LanguageCulture);
12: }
13: web.CheckPermissions(SPBasePermissions.CreateAlerts);
14: if (SPAdministrationWebApplication.Local.OutboundMailServiceInstance == null ||
15: string.IsNullOrEmpty(SPAdministrationWebApplication.Local.
16: OutboundMailServiceInstance.Server.Address))
17: {
18: throw new ConfigurationErrorsException();
19: }
20: Encoding encoding = Encoding.UTF8;
21: try
22: {
23: encoding = Encoding.GetEncoding(web.Site.WebApplication.
24: OutboundMailCodePage);
25: }
26: catch (ArgumentException)
27: {
28: encoding = Encoding.UTF8;
29: }
30: using (MailMessage mm = new MailMessage())
31: {
32: SPUtility.ResolveAddressesForEmail(web, properties.To, delegate(MailAddress a)
33: {
34: mm.To.Add(a);
35: });
36: SPUtility.ResolveAddressesForEmail(web, properties.CC, delegate(MailAddress a)
37: {
38: mm.CC.Add(a);
39: });
40: SPUtility.ResolveAddressesForEmail(web, properties.BCC, delegate(MailAddress a)
41: {
42: mm.Bcc.Add(a);
43: });
44: if (mm.To.Count == 0 && mm.CC.Count == 0 && mm.Bcc.Count == 0)
45: {
46: throw SPUtility.GetInvalidRecipientsException(web.LanguageCulture);
47: }
48: try
49: {
50: SPPrincipalInfo sPPrincipalInfo = SPUtility.ResolvePrincipal(web, properties.From,
51: SPPrincipalType.All, SPPrincipalSource.All, null, false);
52: if (sPPrincipalInfo != null && !string.IsNullOrEmpty(sPPrincipalInfo.Email))
53: {
54: mm.From = new MailAddress(sPPrincipalInfo.Email,
55: (sPPrincipalInfo.DisplayName != null) ?
56: sPPrincipalInfo.DisplayName : "", Encoding.UTF8);
57: }
58: }
59: catch (FormatException)
60: {
61: }
62: if (mm.From == null)
63: {
64: mm.From = new MailAddress(web.Site.WebApplication.OutboundMailSenderAddress,
65: web.Title, Encoding.UTF8);
66: }
67: mm.Subject = properties.Subject;
68: mm.IsBodyHtml = true;
69: mm.BodyEncoding = encoding;
70: mm.SubjectEncoding = encoding;
71: mm.Body = properties.Body;
72: foreach (KeyValuePair<string, string> current in properties.AdditionalHeaders)
73: {
74: mm.Headers[current.Key] = current.Value;
75: }
76: mm.Headers["SharePointSiteId"] = web.Site.ID.ToString("B");
77: SmtpClient smtpClient = new SmtpClient(SPAdministrationWebApplication.Local.
78: OutboundMailServiceInstance.Server.Address);
79: smtpClient.Send(mm);
80: }
81: }