Friday, October 28, 2011

.Net SmtpClient e-mail send with attachments coding

 public void SendMail(string FromUserName, string ToUserName, string strContent, string strSubject, string strAttachmentPath, bool IsAttachment)
        {
            try
            {
                string mstrContents;
                //Retrives the contact mail ids
                if (FromUserName != null && FromUserName.Trim() != "" && ToUserName != null && ToUserName.Trim() != "")
                {
                    //Retrives the mailing contents

                    string strSiteName = DefaultSettings.SiteID;//ConfigurationManager.AppSettings["siteId"].ToString().Trim(); ;
                    SPSite site = new SPSite(strSiteName);
                    SPWeb web = site.OpenWeb();
                    string ToEmailid = web.AllUsers[ToUserName].Email;
                    string FromEmailId = ConfigurationManager.AppSettings["FromEmailId"].ToString().Trim(); ;
                    SmtpClient objSmtp = new SmtpClient(ConfigurationManager.AppSettings["SMTP"].ToString().Trim());


                    if (SPUtility.IsEmailServerSet(web))
                    {

                        MailAddress from = new MailAddress(FromEmailId);
                        MailAddress to = new MailAddress(ToEmailid);
                        MailMessage message = new MailMessage(from, to);
                        Attachment data = new Attachment(strAttachmentPath, MediaTypeNames.Application.Octet);
                        // Add time stamp information for the file.
                        ContentDisposition disposition = data.ContentDisposition;
                        if(strSubject.Contains("Workflow Status Report"))
                        {
                        disposition.FileName = "WorkflowStatusReport.xls";
                        }
                        if(strSubject.Contains("Live Agreement Report"))
                        {
                        disposition.FileName = "LiveAgreementReport.xls";
                        }
                        disposition.CreationDate = System.IO.File.GetCreationTime(strAttachmentPath);
                        disposition.ModificationDate = System.IO.File.GetLastWriteTime(strAttachmentPath);
                        disposition.ReadDate = System.IO.File.GetLastAccessTime(strAttachmentPath);
                        // Add the file attachment to this e-mail message.
                        message.Attachments.Add(data);

                        message.Subject = strSubject;
                        mstrContents = strContent;
                        message.Body = mstrContents;
                        objSmtp.Send(message);
                        data.Dispose();
                    }
                    site.Close();
                    site.Dispose();
                    web.Close();
                    web.Dispose();
                }
            }
            catch (Exception ex)
            {
                string strError = ex.Message.ToString();
                objErrorLog.WriteintoLogFile(ex.Message);
                objErrorLog.WriteintoLogFile(ex.StackTrace);
            }
        }

No comments:

Post a Comment