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 ?

Bekh
  • 219
  • 3
  • 5

4 Answers4

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,
     "toaddress@mail.com", "subject",
     "body");
Daniel Pollard
  • 1,230
  • 1
  • 12
  • 11
  • 1
    This uses the from address specified in CA admin. If you want custom from addresses, go with normal .net classes. – ArjanP Jul 05 '09 at 11:58
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 );
...
gdbdable
  • 4,445
  • 3
  • 30
  • 46
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;
}
Kobi
  • 135,331
  • 41
  • 252
  • 292
Prasath S
  • 78
  • 8
  • 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. – Kobi Nov 22 '12 at 07:04
  • 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. – Prasath S Dec 12 '12 at 07:52
0

using overload with StringDictionary arguments (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());
Iman
  • 17,932
  • 6
  • 80
  • 90