Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Emails in Sharepoint

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 ?

like image 616
Bekh Avatar asked Jul 03 '09 00:07

Bekh


2 Answers

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");
like image 169
Daniel Pollard Avatar answered Sep 30 '22 02:09

Daniel Pollard


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 );
...
like image 22
gdbdable Avatar answered Sep 30 '22 01:09

gdbdable