Advertisement
soton_gr

Perception report sender

Feb 5th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.DirectoryServices;
  6. using System.DirectoryServices.AccountManagement;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Net.Mail;
  12. using System.Net.Mime;
  13. using System.Text;
  14. using System.Windows.Forms;
  15.  
  16. namespace FileToEmailAddress
  17. {
  18.     public partial class Form1 : Form
  19.     {
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.             this.AllowDrop = true;
  24.             this.DragEnter += new DragEventHandler(Form1_DragEnter);
  25.             this.DragDrop += new DragEventHandler(Form1_DragDrop);
  26.         }
  27.  
  28.         void Form1_DragEnter(object sender, DragEventArgs e)
  29.         {
  30.             if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
  31.         }
  32.  
  33.         void Form1_DragDrop(object sender, DragEventArgs e)
  34.         {
  35.             if (!checkready())
  36.             {
  37.                 MessageBox.Show("You cannot drag files onto this application until the text boxes have been filled in", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  38.                 return;
  39.             }
  40.             if (checkusernameandpassword(txtUsername.Text, txtPassword.Text) == -1)
  41.             {
  42.                 MessageBox.Show("The system cannot confirm your username and password. Are you on a University computer connected to the network?"
  43.                     , "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  44.                 return;
  45.             }
  46.             else if (checkusernameandpassword(txtUsername.Text, txtPassword.Text) == 0)
  47.             {
  48.                 MessageBox.Show("Your username and password are not correct. These are required to send email."
  49.                     , "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  50.                 return;
  51.             }
  52.            
  53.             string[] filesString = (string[])e.Data.GetData(DataFormats.FileDrop);
  54.  
  55.             int count = 0;
  56.  
  57.             foreach (string file in filesString)
  58.             {
  59.                 string[] fileSplit = Path.GetFileName(file).Split('_');
  60.                 string username = "";
  61.                 try
  62.                 {
  63.                      username = fileSplit[0];
  64.                 }
  65.                 catch
  66.                 {
  67.                     MessageBox.Show("Failed find a username in the file " + file, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  68.                     return;
  69.                 }
  70.                 try
  71.                 {
  72.                     SendMail(username + "@xxxx.ac.uk", file);
  73.                     count++;
  74.                 }
  75.                 catch
  76.                 {
  77.                     MessageBox.Show("Failed to send message to " + username + "@xxxx.ac.uk", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  78.                 }
  79.             }
  80.  
  81.             MessageBox.Show("Emails have been sent to " + count.ToString() + " recipients", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
  82.             this.Close();
  83.         }
  84.  
  85.         private bool checkready()
  86.         {
  87.             if (txtBody.TextLength > 0 && txtEmail.TextLength > 0 && txtName.TextLength > 0 && txtPassword.TextLength > 0
  88.                 && txtSubject.TextLength > 0 && txtUsername.TextLength > 0)
  89.             {
  90.                 return true;
  91.             }
  92.             else
  93.             {
  94.                 return false;
  95.             }
  96.        }
  97.  
  98.         private void SendMail(string recipientEmail, string attachment)
  99.         {
  100.             string from = txtEmail.Text;
  101.             string body = txtBody.Text ;
  102.             string mailServerName = "smtp.xxxx.ac.uk";
  103.             int mailServerPort = 25;
  104.             string toAddress = recipientEmail;
  105.             string subject = txtSubject.Text;
  106.  
  107.             string username = txtUsername.Text;
  108.             string password = txtPassword.Text;
  109.  
  110.             SmtpClient mailClient = new SmtpClient(mailServerName,
  111.                                                    mailServerPort);
  112.             mailClient.Host = mailServerName;
  113.             mailClient.Credentials = new NetworkCredential(username,
  114.                                                            password);
  115.             mailClient.EnableSsl = false;
  116.  
  117.             MailMessage message = new MailMessage(from, toAddress, subject, body);
  118.  
  119.             // Create  the file attachment for this e-mail message.
  120.             Attachment data = new Attachment(attachment, MediaTypeNames.Application.Octet);
  121.             // Add time stamp information for the file.
  122.             ContentDisposition disposition = data.ContentDisposition;
  123.             disposition.CreationDate = System.IO.File.GetCreationTime(attachment);
  124.             disposition.ModificationDate = System.IO.File.GetLastWriteTime(attachment);
  125.             disposition.ReadDate = System.IO.File.GetLastAccessTime(attachment);
  126.             // Add the file attachment to this e-mail message.
  127.             message.Attachments.Add(data);
  128.             // Send
  129.             mailClient.Send(message);
  130.         }
  131.  
  132.         private void textBox2_TextChanged(object sender, EventArgs e)
  133.         {
  134.             txtEmail.Text = txtUsername.Text + "@xxxx.ac.uk";
  135.             anyText_TextChanged(sender, e);
  136.         }
  137.  
  138.         private void anyText_TextChanged(object sender, EventArgs e)
  139.         {
  140.             if (checkready())
  141.             {
  142.                 lblDrop.Text = "Drop files here";
  143.             }
  144.             else
  145.             {
  146.                 lblDrop.Text = "All boxes must first be filled in";
  147.             }
  148.         }
  149.  
  150.         /// <summary>
  151.         /// Checks the username and password against AD
  152.         /// </summary>
  153.         /// <param name="username">AD username to compare to password</param>
  154.         /// <param name="password">AD password to compare to username</param>
  155.         /// <returns></returns>
  156.         public int checkusernameandpassword(string username, string password)
  157.         {
  158.             // Presume that the username and password is wrong to start with
  159.             int intToReturn = 0;
  160.             try
  161.             {
  162.                 // Use the existing domain that the computer is logged into
  163.                 PrincipalContext adContext = new PrincipalContext(ContextType.Domain);
  164.  
  165.                 using (adContext)
  166.                 {
  167.                     // Use the domain to check if the username and password go together
  168.                     bool validPassword = adContext.ValidateCredentials(username.Trim(), password.Trim());
  169.  
  170.                     // If the password is correct
  171.                     if (validPassword)
  172.                     {
  173.                         intToReturn = 1;
  174.                     }
  175.                     // If the password isn't correct
  176.                     else
  177.                     {
  178.                         intToReturn = 0;
  179.                     }
  180.                 }
  181.             }
  182.             // If we failed to connect to the domain then return a failed result
  183.             catch
  184.             {
  185.                 intToReturn = -1;
  186.             }
  187.  
  188.             // Return the result to the main program
  189.             return intToReturn;
  190.         }
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement