private void Send_Click(object sender, EventArgs e) { if (this.CheckInputValidation(SmtpServer.Text, SmtpPort.Text, UserName.Text, Password.Text, From.Text, To.Text,Cc.Text,Bcc.Text)) { if (this.EmailValidation(this.From.Text)) { bool isRecipient = false; if (this.To.Text.Length > 0) { if (this.RecipientsEmailValidation(this.To.Text)) { isRecipient = true; } else { MessageBox.Show(this, "Recipients' email address is not in the correct format, in \"To: \" field.", "Email Client", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } if (this.Cc.Text.Length > 0) { if (!(this.RecipientsEmailValidation(this.Cc.Text))) { MessageBox.Show(this, "Recipients' email address is not in the correct format, in \"Cc: \" field.", "Email Client", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { isRecipient = true; } } if (this.Bcc.Text.Length > 0) { if (!(this.RecipientsEmailValidation(this.Bcc.Text))) { MessageBox.Show(this, "Recipients' email address is not in the correct format, in \"Bcc: \" field.", "Email Client", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { isRecipient = true; } } if (Internet.IsConnectedToInternet()) { if (isRecipient == true) { Rtf2Html rtf = new Rtf2Html(); string Html = rtf.ConvertRtfToHtml(this.MailMessage); MailMessage mail_message = new MailMessage(); mail_message.From = this.From.Text; mail_message.To = this.To.Text; mail_message.CC = this.Cc.Text; mail_message.BCC = this.Bcc.Text; mail_message.Subject = this.Subject.Text; mail_message.MailType = MailEncodingType.HTML; mail_message.MailPriority = MailSendPriority.NORMAL; mail_message.Message = Html; mail_message.Attachments = this.attachments; Thread thread = new Thread(new ParameterizedThreadStart(this.SendEmail)); thread.Start(mail_message); } else { return; } } else { MessageBox.Show(this, "You must connect to the internet.", "Email Client", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { MessageBox.Show(this, "Sender email address is not in the correct format.", "Email Client", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
//Methods public void SendMail(MailMessage mailMessage) { this.mailMessage = mailMessage; this.tcp_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress[] ip_Addresses; try { ip_Addresses = Dns.GetHostAddresses(this.smtpServer); } catch (Exception) { this.errorMessage = "Smtp server \"" + this.smtpServer + "\" does not exist."; throw new SmtpClientException(this.errorMessage); } IPEndPoint remote_EndPoint = new IPEndPoint(ip_Addresses[0], this.SmtpPort); //Raise Connection Establishing Event this.RaiseConnectionEstablishing(this.smtpServer, this.smtpPort); try { tcp_Socket.Connect((EndPoint)remote_EndPoint); } catch { this.errorMessage = "Unable to connect to SMTP server: " + this.smtpServer + ",on port " + this.smtpPort + "."; throw new SmtpClientException(this.errorMessage); } this.networkStream = new NetworkStream(this.tcp_Socket); byte[] buffer; string responseText = ""; string[] commands = new string[] { "EHLO", "MAIL FROM:", "RCPT TO:", "DATA", "", "QUIT" }; string[] responses = new string[] {"250","250","250","354","250","221"}; string message = ""; bool flag = false; ASCIIEncoding encoding = new ASCIIEncoding(); if (this.WaitForResponse("220",ref responseText,true) == false) { this.errorMessage = "Did not get welcome message from SMTP server: " + this.smtpServer +".\r\nServer said: " + responseText; this.tcp_Socket.Close(); throw new SmtpClientException(this.errorMessage); } else { //Raise Connection Established Event this.RaiseConnectionEstablished(this.smtpServer, this.smtpPort); for (int i = 0; i < commands.Length; i++) { message = commands[i]; switch (i) { case 0: message += " " + Dns.GetHostEntry("127.0.0.1").HostName + "\r\n"; break; case 1: message += "<" + this.mailMessage.From + ">\r\n"; break; case 2: flag = true; break; case 3: message += "\r\n"; break; case 4: message += this.GetEmailInformation(); break; case 5: message += "\r\n"; break; } if (flag == true) { string recipientAddress = ""; if (this.mailMessage.To.Length > 0) { string[] split = this.mailMessage.To.Split(new char[]{',',';'}); for (int j = 0; j < split.Length; j++) { if (split[j].Trim() != "") { recipientAddress = "<" + split[j] + ">"; message = "RCPT TO: " + recipientAddress + "\r\n"; this.SendDataThroughSecureStream(message); if (this.WaitForResponse("250", ref responseText, false) == false) { this.errorMessage = "Server rejected the email address: " + recipientAddress + "."; this.tcp_Socket.Close(); throw new SmtpClientException(this.errorMessage); } } } } if (this.mailMessage.CC.Length > 0) { string[] split = this.mailMessage.CC.Split(new char[] { ',', ';' }); for (int j = 0; j < split.Length; j++) { recipientAddress = "<" + split[j] + ">"; message = "RCPT TO: " + recipientAddress + "\r\n"; this.SendDataThroughSecureStream(message); if (this.WaitForResponse("250", ref responseText, false) == false) { this.errorMessage = "Server rejected the email address: " + recipientAddress + "."; this.tcp_Socket.Close(); throw new SmtpClientException(this.errorMessage); } } } if (this.mailMessage.BCC.Length > 0) { string[] split = this.mailMessage.BCC.Split(new char[] { ',', ';' }); for (int j = 0; j < split.Length; j++) { recipientAddress = "<" + split[j] + ">"; message = "RCPT TO: " + recipientAddress + "\r\n"; this.SendDataThroughSecureStream(message); if (this.WaitForResponse("250", ref responseText, false) == false) { this.errorMessage = "Server rejected the email address: " + recipientAddress + "."; this.tcp_Socket.Close(); throw new SmtpClientException(this.errorMessage); } } } flag = false; } else { if (i == 0) { this.SendDataThroughNetworkStream(message); if (this.WaitForResponse(responses[0], ref responseText, true) == false) { this.errorMessage = "Did not get " + responses[0] + " reply from server for command " + commands[0] + ".\r\nServer said: " + responseText; this.tcp_Socket.Close(); throw new SmtpClientException(this.errorMessage); } else { message = "STARTTLS\r\n"; this.SendDataThroughNetworkStream(message); if (this.WaitForResponse("220", ref responseText, true) == false) { this.errorMessage = "Did not get " + "220" + " reply from server for command " + "STARTTLS" + ".\r\nServer said: " + responseText; this.tcp_Socket.Close(); throw new SmtpClientException(this.errorMessage); } else { this.secureStream = new SslStream(this.networkStream, true, new RemoteCertificateValidationCallback(OnCertificateValidation)); this.secureStream.AuthenticateAsClient("localhost"); if (this.secureStream.IsAuthenticated) { message = "Ehlo " + Dns.GetHostEntry("127.0.0.1").HostName + "\r\n"; this.SendDataThroughSecureStream(message); if (this.WaitForResponse(responses[0], ref responseText, false) == false) { this.errorMessage = "Did not get " + responses[0] + " reply from server for command " + commands[0] + ".\r\nServer said: " + responseText; this.tcp_Socket.Close(); throw new SmtpClientException(this.errorMessage); } else { if (responseText.IndexOf("Auth Login", StringComparison.OrdinalIgnoreCase) >= 0) { message = "Auth Login" + "\r\n"; this.SendDataThroughSecureStream(message); if (this.WaitForResponse("334", ref responseText, false) == false) { this.errorMessage = "Did not get 334 reply from server for AUTH LOGIN" + ".\r\nServer said: " + responseText; this.tcp_Socket.Close(); throw new SmtpClientException(this.errorMessage); } else { buffer = encoding.GetBytes(this.userName); // Not include Domain Name message = Convert.ToBase64String(buffer) + "\r\n"; //Raise Authentication Began Event this.RaiseAuthenticationBegan(this.userName); this.SendDataThroughSecureStream(message); if (this.WaitForResponse("334", ref responseText, false) == false) { this.errorMessage = "Did not get 334 reply from server for UserName Confirmation" + ".\r\nServer said: " + responseText; this.tcp_Socket.Close(); throw new SmtpClientException(this.errorMessage); } else { buffer = encoding.GetBytes(this.password); message = Convert.ToBase64String(buffer) + "\r\n"; this.SendDataThroughSecureStream(message); if (this.WaitForResponse("235", ref responseText, false) == false) { this.errorMessage = "Did not get 235 reply from server for Password Confirmation" + ".\r\nServer said: " + responseText; this.tcp_Socket.Close(); throw new SmtpClientException(this.errorMessage); } else { //Raise Authentication Finished Event this.RaiseAuthenticationFinished(this.userName); } } } } } } } } } else if (i == 4) { // Raise Started Data Transfer Event this.RaiseStartedDataTransfer(); this.SendDataThroughSecureStream(message); if (this.WaitForResponse(responses[i], ref responseText, false) == false) { this.errorMessage = "Did not get " + responses[i] + " reply from server for command " + commands[i] + ".\r\nServer said: " + responseText; this.tcp_Socket.Close(); throw new SmtpClientException(this.errorMessage); } else { // Raise Ended Data Transfer Event this.RaiseEndedDataTransfer(); } } else { this.SendDataThroughSecureStream(message); if (this.WaitForResponse(responses[i], ref responseText, false) == false) { this.errorMessage = "Did not get " + responses[i] + " reply from server for command " + commands[i] + ".\r\nServer said: " + responseText; this.tcp_Socket.Close(); throw new SmtpClientException(this.errorMessage); } if (i == 5) { if (tcp_Socket.Connected) { tcp_Socket.Close(); //Raise Disconnected Event this.RaiseDisconnected(this.smtpServer); } } } } } } if (tcp_Socket.Connected) { tcp_Socket.Close(); //Raise Disconnected Event this.RaiseDisconnected(this.smtpServer); } return; }
public void SendMail(string from, string recipient, string subject, string body) { this.mailMessage = new MailMessage(from, recipient, subject, body); this.SendMail(this.mailMessage); }
private void Send_Click(object sender, EventArgs e) { if (this.CheckInputValidation(SmtpServer.Text, SmtpPort.Text, UserName.Text, Password.Text, From.Text, To.Text, Cc.Text, Bcc.Text)) { if (this.EmailValidation(this.From.Text)) { bool isRecipient = false; if (this.To.Text.Length > 0) { if (this.RecipientsEmailValidation(this.To.Text)) { isRecipient = true; } else { MessageBox.Show(this, "Recipients' email address is not in the correct format, in \"To: \" field.", "Email Client", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } if (this.Cc.Text.Length > 0) { if (!(this.RecipientsEmailValidation(this.Cc.Text))) { MessageBox.Show(this, "Recipients' email address is not in the correct format, in \"Cc: \" field.", "Email Client", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { isRecipient = true; } } if (this.Bcc.Text.Length > 0) { if (!(this.RecipientsEmailValidation(this.Bcc.Text))) { MessageBox.Show(this, "Recipients' email address is not in the correct format, in \"Bcc: \" field.", "Email Client", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } else { isRecipient = true; } } if (Internet.IsConnectedToInternet()) { if (isRecipient == true) { Rtf2Html rtf = new Rtf2Html(); string Html = rtf.ConvertRtfToHtml(this.MailMessage); MailMessage mail_message = new MailMessage(); mail_message.From = this.From.Text; mail_message.To = this.To.Text; mail_message.CC = this.Cc.Text; mail_message.BCC = this.Bcc.Text; mail_message.Subject = this.Subject.Text; mail_message.MailType = MailEncodingType.HTML; mail_message.MailPriority = MailSendPriority.NORMAL; mail_message.Message = Html; mail_message.Attachments = this.attachments; Thread thread = new Thread(new ParameterizedThreadStart(this.SendEmail)); thread.Start(mail_message); } else { return; } } else { MessageBox.Show(this, "You must connect to the internet.", "Email Client", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { MessageBox.Show(this, "Sender email address is not in the correct format.", "Email Client", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }