code is developed to attach pdf and send as email. But email received dont contain the pdf format. On click of file, it gives option of "open with". mentioned is code used.... "attach1" is the path of the pdf which is in library. string email = string.Empty; string smtpServer =SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address; string smtpFrom = SPAdministrationWebApplication.Local.OutboundMailSenderAddress; MemoryStream memoryStreamOfFile1 = null;

data1 = webClient.DownloadData(attach1); memoryStreamOfFile1 = new MemoryStream(data1); mail.Attachments.Add(new System.Net.Mail.Attachment(memoryStreamOfFile1, empID,"application/pdf")); smtpClient.Send(mail);


Plz try below code..

SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = SPContext.Current.Site.WebApplication.OutboundMailServiceInstance.Server.Address;
string fromAddress = SPContext.Current.Site.WebApplication.OutboundMailReplyToAddress;
List<Attachment> attachments = new List<Attachment>();
string mimeType = "application/pdf";
System.IO.Stream stream = yourpdfSPFileObject.OpenBinaryStream();
Attachment attach = new Attachment(stream, mimeType);
attach.Name = file.Name;
MailMessage mailMessage = new MailMessage(fromAddress, emailAddress, Subject, Body);

                if (!String.IsNullOrEmpty(Cc))
                    MailAddress CCAddress = new MailAddress(Cc);
                if (!String.IsNullOrEmpty(Bcc))
                    MailAddress BCCAddress = new MailAddress(Bcc);
                if (attachments.Count > 0)
                    foreach (Attachment attachment in attachments)
                mailMessage.IsBodyHtml = IsBodyHtml;

Hope it helps..

| improve this answer | |

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.