Skip to content

Instantly share code, notes, and snippets.

@darbio
Created January 4, 2016 04:12
Show Gist options
  • Save darbio/1cdbf2700013e37e530e to your computer and use it in GitHub Desktop.
Save darbio/1cdbf2700013e37e530e to your computer and use it in GitHub Desktop.
// Set the variables
string smtpAddress = "mail.example.com";
string fromAddress = "from@example.com";
// Create the SMTPClient
var smtp = new SmtpClient(smtpAddress);
smtp.Credentials = new NetworkCredential("username", "password");
// Construct the MailMessage
var mail = new MailMessage();
mail.From = new MailAddress(fromAddress);
mail.To.Add("to@example.com");
mail.Subject = "Email subject here";
mail.Body = "Body";
// Attach the files
mail.Attachments.Add(new Attachment("filename"));
mail.Attachments.Add(new Attachment("filename"));
// Send the MailMessage
smtp.Send(mail);
using (var web = new SPSite("http://localhost").OpenWeb())
{
// Get the variables from SP
string smtpAddress = web.Site.WebApplication.OutboundMailServiceInstance.Server.Address;
string fromAddress = web.Site.WebApplication.OutboundMailSenderAddress;
// Create the SMTPClient
var smtp = new SmtpClient(smtpAddress);
smtp.Credentials = CredentialCache.DefaultNetworkCredentials;
// Construct the MailMessage
var mail = new MailMessage();
mail.From = new MailAddress(fromAddress);
mail.To.Add("to@example.com");
mail.Subject = "Email subject here";
mail.Body = "Body";
// Attach the files
mail.Attachments.Add(new Attachment("filename"));
mail.Attachments.Add(new Attachment("filename"));
// Send the MailMessage
smtp.Send(mail);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment