Tuesday, 18 March 2014

Programmatically Sending Email in SharePoint

The following code snippet can be used to Programmatically Sending Email in SharePoint
 SPWebApplication webApp = SPWebApplication.Lookup(new Uri("http://ServerName:7777/"));
            var mail = new MailMessage();
            mail.From = new MailAddress("Test@Example.com");
            mail.To.Add("Test@Example.com");
            mail.Subject = "Subject of the email";
            mail.Body = "Body of the email";
            SmtpClient smtp = new SmtpClient(webApp.OutboundMailServiceInstance.Server.Address);
            smtp.UseDefaultCredentials = true;
            smtp.Send(mail);


Programmatically Sending Email in SMTP
The following code snippet can be used to Programmatically Sending Email in SMTP
var message = new MailMessage("Test@Example.com", "Test@Example.com", "Subject of the email", "Body of the email");
            message.IsBodyHtml = true;
            var smtp = new SmtpClient("172.XX.XX.XX");
            smtp.Send(message);
            Console.WriteLine("Mail Sent Successfull");

Programmatically Sending Email in C#
The following code snippet can be used to Programmatically Sending Email in C#
 var mailSend = new MailMessage("Test@gmail.com", "Test@gmail.com");
            mailSend.Subject = "Subject of the email";
            mailSend.Body = "Body of the email";
            mailSend.Priority = MailPriority.High;
            var objSmtp = new SmtpClient();
            objSmtp.Host = "smtp.gmail.com";
            objSmtp.Credentials = new NetworkCredential("Test@gmail.com", "Password");
            objSmtp.EnableSsl = true;
            objSmtp.Send(mailSend);

No comments:

Post a comment