Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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);

share|improve this question
add comment

1 Answer

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;
attachments.Add(attach);
MailMessage mailMessage = new MailMessage(fromAddress, emailAddress, 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);
                }
                if (attachments.Count > 0)
                {
                    foreach (Attachment attachment in attachments)
                    {
                        mailMessage.Attachments.Add(attachment);
                    }
                }
                mailMessage.IsBodyHtml = IsBodyHtml;
                smtpClient.Send(mailMessage);

Hope it helps..

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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