public void SendMail(EmailAddress from, EmailAddressCollection to, EmailAddressCollection cc, string subject, string body) { Connect(); Debug.Print(ReadLine()); if (_requireAuth) { string str = WriteReadLine("EHLO test.domain.com"); Debug.Print(str); while (str[3] == '-') { Debug.Print(str = ReadLine()); } byte[] buf = new byte[2 + _user.Length + _password.Length]; buf[0] = 0; byte[] userData = Encoding.UTF8.GetBytes(_user); Array.Copy(userData, 0, buf, 1, userData.Length); buf[1 + userData.Length] = 0; byte[] passData = Encoding.UTF8.GetBytes(_password); Array.Copy(passData, 0, buf, 2 + userData.Length, passData.Length); Debug.Print(WriteReadLine("AUTH PLAIN " + ConvertBase64.ToBase64String(buf))); } else { Debug.Print(WriteReadLine("HELO test.domain.com")); } Debug.Print(WriteReadLine("MAIL FROM: <" + from.Address + ">")); if (to != null) { foreach (EmailAddress toAddress in to) { Debug.Print(WriteReadLine("RCPT TO: <" + toAddress.Address + ">")); } } if (cc != null) { foreach (EmailAddress ccAddress in cc) { Debug.Print(WriteReadLine("RCPT TO: <" + ccAddress.Address + ">")); } } Debug.Print(WriteReadLine("DATA\r\n")); #region send data WriteLine("From: \"" + from.Name + "\" <" + from.Address + ">"); if (to != null) { foreach (EmailAddress toAddress in to) { WriteLine("To: \"" + toAddress.Name + "\" <" + toAddress.Address + ">"); } } if (cc != null) { foreach (EmailAddress ccAddress in cc) { WriteLine("Cc: \"" + ccAddress.Name + "\" <" + ccAddress.Address + ">"); } } WriteLine("Subject: " + subject + "\r\n"); WriteLine(body); Debug.Print(WriteReadLine(".")); #endregion Debug.Print(WriteReadLine("QUIT")); Disconnect(); }
private string toAuthorizationHeader() { return("Basic " + ConvertBase64.ToBase64String(Encoding.UTF8.GetBytes(this.Username + ":" + this.password))); }