Skip to content

Instantly share code, notes, and snippets.

@mortymacs
Created June 15, 2016 19:48
Show Gist options
  • Select an option

  • Save mortymacs/9ece6b3d604c17596602284bd68e1bcf to your computer and use it in GitHub Desktop.

Select an option

Save mortymacs/9ece6b3d604c17596602284bd68e1bcf to your computer and use it in GitHub Desktop.
Add email attachment with specific user (owner) in SharePoint list.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
using S22.Imap;
using System.Net.Mail;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Threading;
using System.IO;
namespace Send_Payroll_Emails_To_SharePoint
{
class Program
{
public static byte[] read_fully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
string fetch_username(string email)
{
SqlConnection conn = new SqlConnection("Server=localhost;Trusted_Connection=yes;");
conn.Open();
string sql = "SELECT [UserName] FROM [Membership] WHERE [Email] = '" + email + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows == false) return "";
string username = dr["UserName"].ToString();
conn.Close();
return username;
}
bool save_to_sharepoint(string title, string employee, byte[] attach)
{
using (SPSite osite = new SPSite("http://sharepoint-server/"))
{
using(SPWeb oweb = osite.RootWeb)
{
SPList olist = oweb.Lists["Payroll"];
SPListItem oitem = olist.Items.Add();
SPUser e = oweb.EnsureUser(employee);
SPFieldUserValue uv = new SPFieldUserValue(oweb, e.ID, e.LoginName);
oitem["Title"] = title;
oitem["Employee"] = uv;
oitem.Attachments.Add("Payroll File.pdf", attach);
oitem.Update();
return true;
}
}
}
static void Main(string[] args)
{
string employee_username;
using (ImapClient c = new ImapClient("your-mail.com", 143, "[email protected]", "123456", AuthMethod.Login, false))
{
Console.WriteLine("Payroll service started...");
IEnumerable<uint> uids = c.Search(
SearchCondition.From("[email protected]").And(SearchCondition.Subject("Payroll"))
);
IEnumerable<MailMessage> messages = c.GetMessages(uids, FetchOptions.Normal);
Program action = new Program();
foreach(MailMessage x in messages)
{
employee_username = action.fetch_username(x.Headers[8].ToString());
if (employee_username.Trim() == ""){
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Invalid SharePoint account for: {0}", x.Headers[8].ToString());
Console.ResetColor();
continue;
}
action.save_to_sharepoint(x.Subject, employee_username, read_fully(x.Attachments[0].ContentStream));
Console.WriteLine("{0} [Saved success]", employee_username);
Thread.Sleep(5000);
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Task Completed Successfully!");
Console.ResetColor();
Console.ReadKey();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment