2

I a have a SharePoint 2013 list. This list has a visual studio sequential workflow. This Form will go to director then if he approved it will go to the approver to approve and then it will reach to the manager to fill the email field and to approve. When the manager approved, the status will change to Completed. I want to make an event receiver to send an email to the email that the manager added and the email should contain uniform Body every time, the subject should be changed every time. Also it should contain attachment property that enable the manager to attach document. And finally the manager will addCC person and will send the email. How Can I do it since I am new in SharePoint. If you have code sample will help me, please put it here.

It should attach document or image from desktop.

I am using the code in this account SPUtility.SendEmail() with attachment (.ics files) Put I change it to the following:

My questions are: 1. Is this code will attach from desktop?

  1. what should I write instead of insert_ attachment_url?

  2. Can someone take a look on a code and tell me the wrong things?

        base.ItemAdded(properties);
    
        SPListItem listItem = properties.ListItem;
    
        String OS = properties.AfterProperties ["Status"].ToString();
    
        String Email = listItem ["EmailAddress"].ToString();
    
        //Get the Sharepoint SMTP information from the SPAdministrationWebApplication
        string smtpServer = SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;
    
        string smtpFrom = SPAdministrationWebApplication.Local.OutboundMailSenderAddress;
    
         //Create the mail message and supply it with from and to info
        //****it will be change****
        MailMessage mailMessage = new MailMessage(smtpFrom, companyEmail);
    
        if (OS == "completed")
        {
    
        //Set the subject and body of the message
        mailMessage.Subject = " ";
    
        mailMessage.IsBodyHtml = true;
    
        mailMessage.Body = " Your request is ready ";
    
        mailMessage.To.Add(new MailAddress(Email));
    
        mailMessage.From = new MailAddress(smtpFrom);
    
        //Download the content of the file with a WebClient
        WebClient webClient = new WebClient();
    
        //Supply the WebClient with the network credentials of our user
        webClient.Credentials = CredentialCache.DefaultNetworkCredentials;
    
       //Download the byte array of the file
       byte[] data = webClient.DownloadData(insert_ attachment_url);
    
       //Dump the byte array in a memory stream because
      //we can write it to our attachment
      MemoryStream memoryStreamOfFile = new MemoryStream(data);
    
      //Add the attachment
       mailMessage.Attachments.Add(new System.Net.Mail.Attachment(memoryStreamOfFile, "LPO", ".png"));
    
       //Create the SMTP client object and send the message
        SmtpClient smtpClient = new SmtpClient(smtpServer);
        smtpClient.Send(mailMessage);
        }
       }
     }
    }
    

1 Answer 1

0

Replace the add attachment line in your code with the following line

mailMessage.Attachments.Add(new System.Net.Mail.Attachment(memoryStreamOfFile, "LPO", MediaTypeNames.Application.Octet));

2
  • This will work for a file that's uploaded somewhere. To upload a file from desktop, use the following code
    – SRS
    Aug 21, 2018 at 16:18
  • string file = "D:\\RequestFile.rtf";System.Net.Mail.Attachment data = new System.Net.Mail.Attachment(file, MediaTypeNames.Application.Octet); ContentDisposition disposition = data.ContentDisposition; disposition.CreationDate = System.IO.File.GetCreationTime(file); disposition.ModificationDate = System.IO.File.GetLastWriteTime(file); disposition.ReadDate = System.IO.File.GetLastAccessTime(file); message.Attachments.Add(data);
    – SRS
    Aug 21, 2018 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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