SharePoint 2013에서 첨부 파일로 이메일을 보내는 방법은 무엇입니까?

sharepoint.stackexchange https://sharepoint.stackexchange.com//questions/96158

  •  10-12-2019
  •  | 
  •  

문제

Custom Workflow 응용 프로그램을 구현하려면 SharePoint 2013을 사용하고 있습니다.

상태 전이 중 하나에서는 지난 5 일 동안 업로드 된 문서의 ZIP 파일이있는 특정 그룹의 일부인 사람들에게 이메일을 보내고 싶습니다.

그러나 SharePoint에서 첨부 파일을 보내는 가장 좋은 방법은 무엇인지 모르겠습니다.API 자체가 지원하는지 찾을 수 없었습니다.

http://msdn.microsoft.COM / ko / library / microsoft.sharepoint.utiles.sputility.sendemail.aspx

권장 방식이 될 것인지 제안하십시오.

도움이 되었습니까?

해결책

SPUtility.SendEmail doesn't support sending attachments you will need to use the classes in the System.Net.Mail namespace.

Below is an example of how to do this, it is not functional as you will need to provide additional code to load your zip file as an SPFile object in order to call the OpenBinaryStream() method (or provide some other way of opening the zip file as a Stream). Also it will need additional error checking and such.

using(SPSite site = new SPSite("http://siteurl"))
{
  using(SPWeb web = site.OpenWeb())
   {
       // Check there is an email server configured
        if (SPUtility.IsEmailServerSet(web)) 
        {
            // Get the web app so we can get the email server SP is configured to use
            SPWebApplication webApp = web.Site.WebApplication; 

            // Get the mail server details
            string smtpServerAddress = webApp.OutboundMailServiceInstance.Server.Address;
            string fromAddress = webApp.OutboundMailSenderAddress;

            var email = new MailMessage();
            email.From = new MailAddress(fromAddress);

            // Your code to load the zip file as an SPFile              

            Stream contentStream = spFile.OpenBinaryStream();
            var attachment = new Attachment(contentStream, spFile.Name);
            email.Attachments.Add(attachment);

            email.Subject = "Your email subject";
            email.Body = "Your email body text";

            // Set up the mail server and sent the email
            SmtpClient mailServer = new SmtpClient(smtpServerAddress);
            mailServer.Credentials = CredentialCache.DefaultNetworkCredentials;
            mailServer.Send(email);
        }
   }
}

다른 팁

This should work:

using (SPSite site = new SPSite("http://website.com/sites/site"))
{
    using (SPWeb web = site.OpenWeb())
    {
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        System.IO.StreamWriter writer = new System.IO.StreamWriter(ms);
        writer.Write("Hello its my sample file");
        writer.Write(Environment.NewLine);
        writer.Write(Environment.NewLine);
        writer.Write("Hello its my sample file");
        writer.Flush();

        SPWebApplication webApp = web.Site.WebApplication;
        string smtpServerAddress = webApp.OutboundMailServiceInstance.Server.Address;
        string fromAddress = webApp.OutboundMailSenderAddress;

        ms.Position = 0;     // read from the start of what was written             

        System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain);

        MailMessage email = new MailMessage();
        email.From = new MailAddress(fromAddress);
        email.To.Add("myemail@gmail.com");           

        email.Attachments.Add(new Attachment(ms, "myfile.txt", "text/txt"));
        email.Subject = "Your email subject";
        email.Body = "Your email body text";

        // Set up the mail server and sent the email
        SmtpClient mailServer = new SmtpClient(smtpServerAddress);
        mailServer.Credentials = CredentialCache.DefaultNetworkCredentials;
        mailServer.Send(email);

        writer.Dispose();
        ms.Close();
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 sharepoint.stackexchange
scroll top