Skip to content

Instantly share code, notes, and snippets.

@istupakov
Created October 20, 2015 16:00
Show Gist options
  • Save istupakov/0522e25e67c78dc46d27 to your computer and use it in GitHub Desktop.
Save istupakov/0522e25e67c78dc46d27 to your computer and use it in GitHub Desktop.
Simple program for create and send invitation to conference.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Xml;
using Word = Microsoft.Office.Interop.Word;
namespace SendInvitations
{
class Participant
{
public static Participant Parse(string line)
{
var words = line.Split('\t');
return new Participant
{
Id = int.Parse(words[0]),
Name = words[1].Trim(),
Email = words[2].Trim(),
Section = words[3].Trim(),
ReportName = words[4].Trim(),
};
}
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Section { get; set; }
public string ReportName { get; set; }
public string InvitationText { get; set; }
public string InvitationDocFilename
{
get
{
return string.Format("{0}. {1}.doc", Id, Name);
}
}
public string InvitationPdfFilename
{
get
{
return string.Format("{0}. {1}.pdf", Id, Name);
}
}
public string InvitationTxtFilename
{
get
{
return string.Format("{0}. {1}.txt", Id, Name);
}
}
public string NameWithPrefix
{
get
{
return string.Format("Уважаем{1} {0}", Name, Name.Last() == 'а' ? "ая" : "ый");
}
}
public string NameForExpert { get; private set; }
public void FindNameForExpert()
{
var doc = new XmlDocument();
doc.Load("http://api.morpher.ru/WebService.asmx/GetXml?s=" + Uri.EscapeDataString(Name));
var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("morpher", "http://morpher.ru/");
var res = doc.SelectSingleNode("//morpher:Р", nsmgr).InnerText.Split(' ');
NameForExpert = string.Format("{0} {1}.{2}.", res[0], res[1][0], res[2][0]);
Console.WriteLine("{0} -> {1}", Name, NameForExpert);
}
}
class Program
{
static List<Participant> ReadPartisipants(string filename)
{
return File.ReadAllLines(filename, Encoding.GetEncoding(1251)).Select(Participant.Parse).ToList();
}
static void CreateInvitations(List<Participant> participants)
{
var app = new Word.Application();
try
{
var txt = File.ReadAllText("Invitation.txt", Encoding.GetEncoding(1251));
foreach (var participant in participants)
{
Console.WriteLine("Creating invitation to {0}", participant.Name);
participant.InvitationText = string.Format(txt, participant.NameWithPrefix, participant.Section, participant.ReportName);
var doc = app.Documents.Open(Path.Combine(Environment.CurrentDirectory, "Invitation.docx"));
foreach (Word.Range range in doc.StoryRanges)
{
range.Find.Execute2007("{0}", ReplaceWith: participant.NameWithPrefix, Replace: Word.WdReplace.wdReplaceAll);
range.Find.Execute2007("{1}", ReplaceWith: participant.Section, Replace: Word.WdReplace.wdReplaceAll);
range.Find.Execute2007("{2}", ReplaceWith: participant.ReportName, Replace: Word.WdReplace.wdReplaceAll);
}
//doc.SaveAs(Path.Combine(Environment.CurrentDirectory, participant.InvitationDocFilename), Word.WdSaveFormat.wdFormatDocument);
doc.SaveAs(Path.Combine(Environment.CurrentDirectory, participant.InvitationPdfFilename), Word.WdSaveFormat.wdFormatPDF);
doc.Close(SaveChanges: false);
File.WriteAllText(participant.InvitationTxtFilename, participant.InvitationText);
}
}
finally
{
app.Quit();
}
}
static void CreateExpert(List<Participant> participants)
{
var app = new Word.Application();
try
{
foreach (var participant in participants)
{
participant.FindNameForExpert();
Console.WriteLine("Creating expert to {0}", participant.NameForExpert);
var doc = app.Documents.Open(Path.Combine(Environment.CurrentDirectory, "Expert.doc"));
foreach (Word.Range range in doc.StoryRanges)
{
range.Find.Execute2007("{0}", ReplaceWith: participant.NameForExpert, Replace: Word.WdReplace.wdReplaceAll);
range.Find.Execute2007("{1}", ReplaceWith: participant.ReportName, Replace: Word.WdReplace.wdReplaceAll);
}
//doc.SaveAs(Path.Combine(Environment.CurrentDirectory, participant.InvitationDocFilename), Word.WdSaveFormat.wdFormatDocument);
doc.SaveAs(Path.Combine(Environment.CurrentDirectory, participant.InvitationPdfFilename), Word.WdSaveFormat.wdFormatPDF);
doc.Close(SaveChanges: false);
}
}
finally
{
app.Quit();
}
}
static void SendInvitations(List<Participant> participants)
{
using (var client = new SmtpClient())
{
var reply = new MailAddress("result@center.nstu.ru", "Оргкомитет конференции");
foreach (var participant in participants)
{
var to = new MailAddress(participant.Email, participant.Name);
using (var attachment = new Attachment(participant.InvitationPdfFilename))
{
using (var message = new MailMessage())
{
message.To.Add(to);
message.Subject = "Приглашение на конференцию НТИ";
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.Body = participant.InvitationText;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.ReplyToList.Add(reply);
message.Attachments.Add(attachment);
client.Send(message);
}
}
Console.WriteLine("Send invitation to {0} completed.", participant.Name);
}
}
}
static void Main(string[] args)
{
string filename = "list.txt";
if (args.Length > 0 && !string.IsNullOrWhiteSpace(args[0]))
filename = args[0];
if (!File.Exists(filename))
{
Console.WriteLine("File {0} not found!", filename);
return;
}
var participants = ReadPartisipants(args[0]);
//CreateExpert(participants);
CreateInvitations(participants);
SendInvitations(participants);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment