Wednesday, June 19, 2013

Custom Code for send Mail in SharePoint using C# .net code

Create new project in Visural Studio create new Project by following the below step:

File -> New -> Project -> Visual C# -> Windows in Installed Template -> Select Console Application. Give your desire name to the application.

Add Following Reference in your code:

Microsoft.SharePoint

Add Following Name Space in you code:
using Microsoft.SharePoint;
using System.Net.Mail;

Replace Program.cs code with below code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using Microsoft.SharePoint.Administration;
namespace SendEmail
{
    class Program
    {
        static void Main(string[] args)
        {
            SPWebApplication webApp = SPWebApplication.Lookup(new Uri("https://serverName/"));
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("abc@xyz.com");
            mail.To.Add("pqr@xyz.com");
            mail.Subject = "Subject Test";
            mail.Body = "Body Test";
            // SmtpClient class sends the email by using the specified SMTP server
            SmtpClient smtp = new SmtpClient(webApp.OutboundMailServiceInstance.Server.Address);
            smtp.UseDefaultCredentials = true;
            smtp.Send(mail);
        }
    }
}

This will send mail from abc@xyz.com to pqr@xyz.com user. Subject of the mail is "Subject Test" and Body of the mail is "Body Test"

0 comments:

Post a Comment