Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.DirectoryServices;
- using System.DirectoryServices.AccountManagement;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Mail;
- using System.Net.Mime;
- using System.Text;
- using System.Windows.Forms;
- namespace FileToEmailAddress
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- this.AllowDrop = true;
- this.DragEnter += new DragEventHandler(Form1_DragEnter);
- this.DragDrop += new DragEventHandler(Form1_DragDrop);
- }
- void Form1_DragEnter(object sender, DragEventArgs e)
- {
- if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
- }
- void Form1_DragDrop(object sender, DragEventArgs e)
- {
- if (!checkready())
- {
- MessageBox.Show("You cannot drag files onto this application until the text boxes have been filled in", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- if (checkusernameandpassword(txtUsername.Text, txtPassword.Text) == -1)
- {
- MessageBox.Show("The system cannot confirm your username and password. Are you on a University computer connected to the network?"
- , "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- else if (checkusernameandpassword(txtUsername.Text, txtPassword.Text) == 0)
- {
- MessageBox.Show("Your username and password are not correct. These are required to send email."
- , "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- string[] filesString = (string[])e.Data.GetData(DataFormats.FileDrop);
- int count = 0;
- foreach (string file in filesString)
- {
- string[] fileSplit = Path.GetFileName(file).Split('_');
- string username = "";
- try
- {
- username = fileSplit[0];
- }
- catch
- {
- MessageBox.Show("Failed find a username in the file " + file, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- try
- {
- SendMail(username + "@xxxx.ac.uk", file);
- count++;
- }
- catch
- {
- MessageBox.Show("Failed to send message to " + username + "@xxxx.ac.uk", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- MessageBox.Show("Emails have been sent to " + count.ToString() + " recipients", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
- this.Close();
- }
- private bool checkready()
- {
- if (txtBody.TextLength > 0 && txtEmail.TextLength > 0 && txtName.TextLength > 0 && txtPassword.TextLength > 0
- && txtSubject.TextLength > 0 && txtUsername.TextLength > 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- private void SendMail(string recipientEmail, string attachment)
- {
- string from = txtEmail.Text;
- string body = txtBody.Text ;
- string mailServerName = "smtp.xxxx.ac.uk";
- int mailServerPort = 25;
- string toAddress = recipientEmail;
- string subject = txtSubject.Text;
- string username = txtUsername.Text;
- string password = txtPassword.Text;
- SmtpClient mailClient = new SmtpClient(mailServerName,
- mailServerPort);
- mailClient.Host = mailServerName;
- mailClient.Credentials = new NetworkCredential(username,
- password);
- mailClient.EnableSsl = false;
- MailMessage message = new MailMessage(from, toAddress, subject, body);
- // Create the file attachment for this e-mail message.
- Attachment data = new Attachment(attachment, MediaTypeNames.Application.Octet);
- // Add time stamp information for the file.
- ContentDisposition disposition = data.ContentDisposition;
- disposition.CreationDate = System.IO.File.GetCreationTime(attachment);
- disposition.ModificationDate = System.IO.File.GetLastWriteTime(attachment);
- disposition.ReadDate = System.IO.File.GetLastAccessTime(attachment);
- // Add the file attachment to this e-mail message.
- message.Attachments.Add(data);
- // Send
- mailClient.Send(message);
- }
- private void textBox2_TextChanged(object sender, EventArgs e)
- {
- txtEmail.Text = txtUsername.Text + "@xxxx.ac.uk";
- anyText_TextChanged(sender, e);
- }
- private void anyText_TextChanged(object sender, EventArgs e)
- {
- if (checkready())
- {
- lblDrop.Text = "Drop files here";
- }
- else
- {
- lblDrop.Text = "All boxes must first be filled in";
- }
- }
- /// <summary>
- /// Checks the username and password against AD
- /// </summary>
- /// <param name="username">AD username to compare to password</param>
- /// <param name="password">AD password to compare to username</param>
- /// <returns></returns>
- public int checkusernameandpassword(string username, string password)
- {
- // Presume that the username and password is wrong to start with
- int intToReturn = 0;
- try
- {
- // Use the existing domain that the computer is logged into
- PrincipalContext adContext = new PrincipalContext(ContextType.Domain);
- using (adContext)
- {
- // Use the domain to check if the username and password go together
- bool validPassword = adContext.ValidateCredentials(username.Trim(), password.Trim());
- // If the password is correct
- if (validPassword)
- {
- intToReturn = 1;
- }
- // If the password isn't correct
- else
- {
- intToReturn = 0;
- }
- }
- }
- // If we failed to connect to the domain then return a failed result
- catch
- {
- intToReturn = -1;
- }
- // Return the result to the main program
- return intToReturn;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement