Sending Mail with Attachment in SharePoint 2010

                     string smtpServer = SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;
                    string smtpFrom = SPAdministrationWebApplication.Local.OutboundMailSenderAddress;

                     string Email= "EmailID"
                     MailMessage mailMessage = new MailMessage(smtpFrom, Email);
                     mailMessage.Subject = txtSubject.Text;
                     mailMessage.Body = txtBody.Text;
                     WebClient webClient = new WebClient();
                     webClient.Credentials = CredentialCache.DefaultNetworkCredentials;
                     FileStream fs = new FileStream(pathToCheck, FileMode.Open, FileAccess.Read);
                     byte[] data = new byte[fs.Length];
                     fs.Read(data, 0, data.Length);
                     MemoryStream memoryStreamOfFile = new MemoryStream(data);
                     mailMessage.Attachments.Add(new                                   System.Net.Mail.Attachment(memoryStreamOfFile, filename));
                     SmtpClient smtpClient = new SmtpClient(smtpServer);
                     smtpClient.Send(mailMessage);

Comments

Popular posts from this blog

This content database has a schema version which is not supported in this farm

Export all documents/Folders permissions to CSV using SharePoint PowerShell

SPUtility.js