Sending Emails in Sharepoint
Asked Answered
B

4

9

I need to know what is the best practice for sending emails from my sharepoint webparts and/or customized features.

Should I just use the normal .Net classes to send email ? or is their a better way to do it through integration with an outlook server ?

Bryner answered 3/7, 2009 at 0:24 Comment(0)
B
16

Easy way is to use the built in Utilities, this will then use the mail server setttings setup in central admin

using Microsoft.SharePoint.Utilities;
SPUtility.SendEmail(SPContext.Current.Web, false, false,
     "[email protected]", "subject",
     "body");
Bitner answered 3/7, 2009 at 0:29 Comment(1)
This uses the from address specified in CA admin. If you want custom from addresses, go with normal .net classes.Reheat
O
1

Universal way to send email in any context(where SPWeb not available) is read OutboundMailService settings which is used in SPUtility. Then create SmtpClient manually:

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 );
...
Occur answered 26/6, 2012 at 13:40 Comment(0)
F
0

You also can use this code for dynamic mail id. this code gets the mail according to the user. I have used CAML query to get the data for the email content from the lists.

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;
}
Flita answered 22/11, 2012 at 6:52 Comment(2)
Can you explain when is this code useful? It seems very specific, and doesn't really add anything to the accepted answer. Of course you can get parameters from a list or the current user... Maybe I'm missing the point here.Neptunium
The Content of the email is stored in a separate list and using this caml query the contents can be obtained and used for email.Flita
H
0

using overload with StringDictionary arguments (source)

  StringDictionary headers = new StringDictionary();                             
  headers.Add("to", currCtxt.Web.CurrentUser.Email);
  headers.Add("cc", "[email protected]");
  headers.Add("bcc", "");
  headers.Add("from", "[email protected]");
  headers.Add("subject", "Email Subject");
  headers.Add("content-type", "text/html");
  string bodyText = "Hello how are you?";
  SPUtility.SendEmail(currCtxt.Web, headers, bodyText.ToString());
Homage answered 17/8, 2013 at 12:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.