本文整理匯總了C#中Org.Mentalis.Security.Ssl.SecureSocket.ChangeSecurityProtocol方法的典型用法代碼示例。如果您正苦於以下問題:C# SecureSocket.ChangeSecurityProtocol方法的具體用法?C# SecureSocket.ChangeSecurityProtocol怎麽用?C# SecureSocket.ChangeSecurityProtocol使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Org.Mentalis.Security.Ssl.SecureSocket
的用法示例。
在下文中一共展示了SecureSocket.ChangeSecurityProtocol方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Start
public void Start()
{
try {
// ask the user for SMTP server, port and the type of connection
Console.Write("Host to connect to: ");
string server = Console.ReadLine().Trim();
Console.Write("Port: ");
string port = Console.ReadLine().Trim();
Console.Write("Connection type [0 = normal connection, 1 = direct TLS connection, 2 = indirect TLS connection (using STARTTLS command)]: ");
string type = Console.ReadLine().Trim();
if (Array.IndexOf(new string[]{"0", "1", "2"}, type) == -1) {
Console.WriteLine("Invalid connection type.");
return;
}
Console.WriteLine("Please enter the email address you wish to send an email to:");
string address = Console.ReadLine().Trim();
// create a new SecurityOptions instance
SecurityOptions options = new SecurityOptions(SecureProtocol.None);
// allow only secure ciphers to be used. Currently, the seure ciphers are:
// the AES algorithm [128 or 256 bit keys], the RC4 algorithm [128 bit keys]
// or TipleDES [168 bit keys]
options.AllowedAlgorithms = SslAlgorithms.SECURE_CIPHERS;
// the SecureSocket should be a client socket
options.Entity = ConnectionEnd.Client;
// we will use manual verification of the server certificate
options.VerificationType = CredentialVerification.Manual;
// when the server certificate is receives, the SecureSocket should call
// the OnVerify method
options.Verifier = new CertVerifyEventHandler(OnVerify);
// use the default flags
options.Flags = SecurityFlags.Default;
// the common name is the domain name or IP address of the server
options.CommonName = server;
// if the user chose a direct TLS connection, set the protocol to TLS1
if (type == "1") {
options.Protocol = SecureProtocol.Tls1;
}
// create the new secure socket
Connection = new SecureSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, options);
// connect to the SMTP server
Connection.Connect(new IPEndPoint(Dns.GetHostEntry(server).AddressList[0], int.Parse(port)));
// wait for the server hello message
if (!IsReplyOK(Receive())) {
Console.WriteLine("Server disallowed connection.");
return;
}
// if the user selected an indirect TLS connection, issue
// a EHLO command. Otherwise, stick to the standard HELO command.
if (type == "2") // STARTTLS connection
Send("EHLO SmtpClient.Mentalis.org\r\n");
else
Send("HELO SmtpClient.Mentalis.org\r\n");
if (!IsReplyOK(Receive())) {
Console.WriteLine("HELLO failed.");
return;
}
// if the user selected an indirect TLS connection, issue
// the STARTTLS command
if (type == "2") {
// send the STARTTLS command to the server
Send("STARTTLS\r\n");
// make sure the server supports the STARTTLS command
if (!IsReplyOK(Receive())) {
Console.WriteLine("STARTTLS failed.");
return;
}
// change the protocol from SecureProtocol.None
// to SecureProtocol.Tls1
options.Protocol = SecureProtocol.Tls1;
// start the TLS handshake
Connection.ChangeSecurityProtocol(options);
// after this line, we're using an encrypted TLS connection
}
// from here on, act as if the SecureSocket is a normal Socket
Send("MAIL FROM: [email protected]\r\n"); // [email protected] is not a real email address
if (!IsReplyOK(Receive())) {
Console.WriteLine("MAIL FROM failed.");
return;
}
Send("RCPT TO:" + address + "\r\n");
if (!IsReplyOK(Receive())) {
Console.WriteLine("RCPT TO failed.");
return;
}
Send("DATA\r\n");
if (!IsReplyOK(Receive())) {
Console.WriteLine("DATA failed.");
return;
}
Send(TestMail.Replace("#ADDRESS#", address) + "\r\n.\r\n");
if (!IsReplyOK(Receive())) {
Console.WriteLine("Sending of e-mail failed.");
return;
}
Send("QUIT\r\n");
if (!IsReplyOK(Receive())) {
Console.WriteLine("QUIT failed.");
}
Connection.Shutdown(SocketShutdown.Both);
} catch (Exception e) {
//.........這裏部分代碼省略.........