Friday, January 10, 2014

SPUtility.SendEmail Issue in CSS styles applied to an email when sending email from SharePoint

Recently I was working on a timer job that sends (html) emails via SharePoint's SPUtility.SendEmail function. The CSS styles I used in the email body were being inconsistently applied to the email when viewed in Outlook 2010. The method I was using was:

Create the email body, applying the styles at the top of the email body.
emailBody.Append(AddStyles());
emailBody.Append(body);

Send the email setting the fAppendHtmlTag parameter to true.
SPUtility.SendEmail(web, true, false, recipient, subject, emailBody)

To fix the issue, I set the fAppendHtmlTag parameter to false, instead adding the HTML tags myself, as such:
var body = String.Format("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html><head><meta http-equiv=\"X-UA-Compatible\" content=\"IE=8\" /><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />{0}</head><body>{1}</body></html>", AddStyles(), emailBody);

Then send the email:
SPUtility.SendEmail(web, false, false, recipient, subject, body)

This worked nicely, and the CSS styles were now applied consistently throughout the body of the email.

 but if you further find some issue you can below method for sending mail

public static bool SendMailUitility(SPWeb web, string Subject, string Body, bool IsBodyHtml, string From, string To, string Cc, string Bcc)
        {
            bool mailSent = false;
            try
            {
                SmtpClient smtpClient = new SmtpClient();
               
               // smtpClient.Host = SPContext.Current.Site.WebApplication.
                smtpClient.Host = web.Site.WebApplication.
                OutboundMailServiceInstance.Server.Address;
                MailMessage mailMessage = new MailMessage(From, To, Subject, Body);
                if (!String.IsNullOrEmpty(Cc))
                {
                    MailAddress CCAddress = new MailAddress(Cc);
                    mailMessage.CC.Add(CCAddress);
                }
                if (!String.IsNullOrEmpty(Bcc))
                {
                    MailAddress BCCAddress = new MailAddress(Bcc);
                    mailMessage.Bcc.Add(BCCAddress);
                }
                mailMessage.IsBodyHtml = IsBodyHtml;
                smtpClient.Send(mailMessage);
                mailSent = true;
            }
            catch (Exception) { return mailSent; }
            return mailSent;
        }

    }
}


or using gmail smtp

   public void SendMailThroughGmail(string subject, string Body)
        {

            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("abc100@gmail.com");
            mail.To.Add("newemail@7p.com");
            mail.IsBodyHtml = true;
            mail.Subject = subject;
            mail.Body = Body;
            //Attachment attachment = new Attachment(filename);
            //  mail.Attachments.Add(attachment);

            SmtpServer.Port = 25;
            SmtpServer.Credentials = new System.Net.NetworkCredential("abc100@gmail.com", "******");
            SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);

        }



Thursday, January 2, 2014

Referenced assembly does not have a strong name

when you are using third party .dll files or cause is an assembly is not signed with a strong name, the strong name could not be verified, or the strong name would not be valid without the current registry settings of the compute.

The strong name protects clients from unknowingly loading an assembly that has been tampered with. Assemblies without strong names should not be deployed outside of very limited scenarios.

An assembly without a strong name  cannot be loaded into the global assembly cache.


Step 1 : Run visual studio command prompt and go to directory where your DLL located.

  For Example my DLL located in D:/test/perceptiveMCAPI.dll


Step 2 : Now create il file using below command.

  D:/test> ildasm /all /out=perceptiveMCAPI.il  perceptiveMCAPI.dll
  (this command generate code library)


Step 3 : Generate new Key for sign your project.

  D:/test> sn -k mykey.snk


Step 4 : Now sign your library using ilasm command.

  D:/test> ilasm /dll /key=mykey.snk perceptiveMCAPI.il

so after this step your assembly contains strong name and signed.
just add reference this new assembly in your project and compile project its running now. 

If  your current assemble framework is 2.0 or lesser and your system contain higer framework 4.0 then strong name assemble generated in 4.0 framework. If you want to generate assemble in 2.0 frame work you change Step 4 as

D:/test>C:\Windows\Microsoft.NET\Framework\v2.0.50727\ ilasm /dll /key=mykey.snk perceptiveMCAPI.il

In this step you generate assemble in 2.0 framework