using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using Microsoft.SharePoint;
namespace SendEmail
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("https://serverName/sites/Test/"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists.TryGetList("Shared Documents");
if (list != null)
{
SPFile file = list.Items[0].File;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test";
mail.Body = "TEST";
mail.Attachments.Add(new Attachment(file.OpenBinaryStream(), file.Name));
// SmtpClient class sends the email by using the specified SMTP server
SmtpClient smtp = new SmtpClient(site.WebApplication.OutboundMailServiceInstance.Server.Address);
smtp.UseDefaultCredentials = true;
smtp.Send(mail);
}
}
}
}
}
}