Sending email for SharePoint, with/without attachment

One of great things we can do with Sharepoint is sending an email. The most easiest way is using SPUtility. This will work if you have set up Outgoing email in Central Admin.
outgoing email

private void SendEmail()
{
	// Build the email message headers
	StringDictionary headers = new StringDictionary();
	headers.Add("to", "user@email.com");
	headers.Add("subject", "Item Records");	
	headers.Add("content-type", "text/html");
	
	// Send the email
	SPSecurity.RunWithElevatedPrivileges(delegate()
	{
		using (var elevatedSite = new SPSite(SPContext.Current.Site.ID))
		{
			using (var elevatedWeb = elevatedSite.OpenWeb())
			{
				SPUtility.SendEmail(elevatedWeb, headers, GetEmailBody());
			}
		}
	});
}

private String GetEmailBody()
{
	StringBuilder sb = new StringBuilder();
	sb.Append(String.Format("<p>Below is the {1} records on {0:dd MMM yyyy HH:mm}.</p>", DateTime.Now, "Item List"));
	
	return sb.ToString();
}

Continue reading

Advertisements