在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
In this article i'll try to explain how we can send emails using SMTP servers.
Here you can find a diagram that explains the network topology. This diagram shows the communication between the MTA and MUA What are the rules of SMTP protocol?
using System;
using System.Net.Sockets; using System.IO; namespace SendMailviaSMTP
Namespaces we used;
We've set the default values of our two properties in our default constructor method.
public SMTPMailSender()
tclSMTP=new TcpClient();
try If you look at the code above the method will return false value if the connection to the mta fails. 2- After our connection is ready, we create an instance of the networkstream to handle the incoming/outgoing tcpip traffic.To send and receive string information via the networkstream object, we create the streamwriter and the streamreader classes.
nstSMTP=tclSMTP.GetStream();
stwSMTP=new StreamWriter(nstSMTP); strSMTP=new StreamReader(nstSMTP); 3- All of our tools for communicating with mta is ready so we can make a start.As i have mentioned in the beginning of this article we'll use the SMTP protocol.
//mta'dan karlama mesaj?bekleniyor
//waiting for greeting message from MTA if (WaitForResponse("220")) The rest of the protocol messages will be send and the responses will be received just the same as the above code.The only difference will be the message that we send and the response we received. Now we'll have a look at our WaitForResponse method;
bool WaitForResponse(string strResponseCode)
As you can see this method takes a string parameter, strResponseCode.This parameter represents the first 3 char of the response we wait from the MTA. Remember that when we connect with the telnet application we were sending commands and waiting for the mta's "250 ok" responses.By sending the first 3 char to this method we understand tht the mta has successfully received our message and sent us the ok command.If this method returns false we understand tht some problem occured and we cut off the connection. At this stage the only thing we should consider is the timeout property and how we'll understand the timeout is exceeded or not. Mta can end the connection by any reason or we can have a network problem.If so how we'll solve this problem.If we do not create a timeout mechanizm and we have a network connectivity problem the WaitForResponse method will wait until we end our program.This will not be a good solution. We should store the current datetime value to a variable and check if the difference exceeds the timeout value.
//zamanam?kontrol?i鏸n mevcut tarih saat bilgisi al齨齳or
We store the current datetime value to the dteTimeOutCheck variable.//gathering current date time data for timeout check dteTimeOutCheck=DateTime.Now; //zamanam?de餰ri bulunuyor //calculating timeout value TimeSpan tsp=DateTime.Now-dteTimeOutCheck; //zamanam?de餰ri kullan齝齨齨 belirledi餴 de餰ri ancaya kadar d鰊g?鏰lt齬齦齳or //looping code until the timeout exceeds the user defined value while(tsp.Seconds<mvarSMTPTimeOut) Using the TimeSpan class we can find out the difference between our dteTimeOutCheck value and the current datetime and if the difference exceeds the timeout value we return a false value telling the outer block that WaitForResponse method failed. Have a look at the following code;
//MTA bize herhangi bir mesaj g鰊dermi?mi kontrol ediliyor.
We check that if there is any data waiting to be received in the first line.//checking if MTA has sent a message to us if (nstSMTP.DataAvailable) At first look this line seems unneccessary.Even we can think that we can write a smaller code with just using 搒trSMTP.ReadLine()?Correct but not all the time. If we have any problem with the mta or any problem occurs in the protocol flow you'll see how important this line is. Assume that we are waiting the 250 response from the mta and somehow the mta responded us as "502 unimplemented". If we do not use the nstSMTP.DataAvailable check we'll receive the 502 command and because of the 502 is not the message we are waiting for we'll continue to wait.If this scenario happens you'll continue waiting the 250 command in the second loop.You will wait because the strSMTP.ReadLine() command waits until he has received any message.Finally you won't reach to the checkpoint of the timeout mechanizm until the mta sends us the message we wait. Beleive me it's good to write more code, thinking all the possibilities than writing a small but unsafe code. Yahooo, the last stage , testing the code. Copy the following code and do not forget to change the mta recipient sender properties.
using System;
namespace SendMailviaSMTP |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论