2

I added a custom button on a display form of a list.And I want to call C# code on a click of button,that will send a email to outlook,using JavaScript.

So where i should put the C# code in SharePoint online, so that i would be able to call that code.

C# Code for sending an email-

 class mainProgram
{
    static void Main(string[] args)
    {
        eAppointmentMail mailTo = new SendEmail.eAppointmentMail();
        mailTo.Body = "Mail Body";
        mailTo.Email = "exp@outlook.com";
        mailTo.EndDate = DateTime.Now.AddDays(7);
        mailTo.Location = "Skype";
        mailTo.Name = "Arzoo Soni";
        mailTo.StartDate = DateTime.Now.AddDays(8);
        mailTo.Subject = "Subject of the mail";
        Console.WriteLine(SendAnInvitation.sendOutlookInvitationViaICSFile(mailTo));
        Console.ReadLine();
    }
}



class SendAnInvitation
{
    public static string sendOutlookInvitationViaICSFile(eAppointmentMail objApptEmail)
    {
        try
        {
            //Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
            //MailSettingsSectionGroup settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");

            SmtpClient sc = new SmtpClient("smtp.gmail.com");

            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();

            msg.From = new MailAddress("arzoosoni4@gmail.com");
            msg.To.Add(new MailAddress(objApptEmail.Email, objApptEmail.Name));
            msg.Subject = objApptEmail.Subject;
            msg.Body = objApptEmail.Body;
            StringBuilder str = new StringBuilder();
            str.AppendLine("BEGIN:VCALENDAR");
            str.AppendLine("PRODID:-//" + objApptEmail.Email);
            str.AppendLine("VERSION:2.0");
            str.AppendLine("METHOD:REQUEST");
            str.AppendLine("BEGIN:VEVENT");

            str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", objApptEmail.StartDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z")));
            str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", (objApptEmail.EndDate - objApptEmail.StartDate).Minutes.ToString()));
            str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", objApptEmail.EndDate.ToUniversalTime().ToString("yyyyMMdd\\THHmmss\\Z")));
            //str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", objApptEmail.StartDate.ToString()));
            //str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
            //str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", objApptEmail.EndDate.ToString()));
            str.AppendLine("LOCATION:" + objApptEmail.Location);
            str.AppendLine(string.Format("DESCRIPTION:{0}", objApptEmail.Body));
            str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", objApptEmail.Body));
            str.AppendLine(string.Format("SUMMARY:{0}", objApptEmail.Subject));
            str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", objApptEmail.Email));
            str.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", msg.To[0].DisplayName, msg.To[0].Address));
            str.AppendLine("BEGIN:VALARM");
            str.AppendLine("TRIGGER:-PT15M");
            str.AppendLine("ACTION:DISPLAY");
            str.AppendLine("DESCRIPTION:Reminder");
            str.AppendLine("END:VALARM");
            str.AppendLine("END:VEVENT");
            str.AppendLine("END:VCALENDAR");
            System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("text/calendar");
            ct.Parameters.Add("method", "REQUEST");
            AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), ct);
            msg.AlternateViews.Add(avCal);
            NetworkCredential nc = new NetworkCredential("****@gmail.com", "password");
            sc.Port = 25;
            sc.EnableSsl = true;
            sc.Credentials = nc;
            try
            {
                sc.Send(msg);
                return "Success";
            }
            catch (Exception ex)
            {
                return "Fail";
            }
        }
        catch { }
        return string.Empty;
    }
}

public class eAppointmentMail
{
    public string Name { set; get; }
    public string Email { set; get; }
    public string Location { set; get; }
    public DateTime StartDate { set; get; }
    public DateTime EndDate { set; get; }
    public string Subject { set; get; }
    public string Body { set; get; }
}

Please give me solution related to my problem.

4

C# code (Managed Code) is depreciated in SharePoint Online. So you cannot do this directly.

So What is the workaround?

You can create an .NET Web API and write your C# code into that. And just call that .NET Web API using your JavaScript REST API.

But in .NET Web API, the problem will be in authentication. There you have to manually authenticate by providing Site URL, Username and Password as a plain text.

Also, if you are not aware then I would like to tell you that there are email REST API are already available with SharePoint Online. You can send emails using that REST API's.

Send email using REST API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.