• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Sendingemailwithc#usingSMTPServers

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

In this article i'll try to explain how we can send emails using SMTP servers.
First of all, what is SMTP what is MTA,MUA, protocol? Let's start talking about these subjects.

Protocol?/b>
Protocol is a group of rules which is used by applications that communicate each other from network.?br> SMTP
Simple Mail Transfer Protocol.
Is a protocol used to transfer emails.First RFC was published in August 1982.We can say that when we compose a message in an outlook application and send it, the outlook application sends this email to mail server by using the smtp protocol.
MTA
Mail Transfer Agent.
Is the mail servers which communicates both with other email servers and clients to fetch emails.For example : Qmail,Postfix,Courier...
MUA
Mail User Agent
Is the applications that lets users to read & compose emails.For example: Outlook Express

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?

SMTP is a protocol (application free) therefore we are able to send an email without using C# which i prefer to show this using telnet.Now we'll send an email to [email protected] by using telnet.Our mail transfer agent's address is mail.somemta.com and we'll send this email by using our existing account [email protected] .

Run the command prompt and enter the following line;
C:\>telnet mail.somemta.com 25

When we enter this command the mta will respond us as follows;
220 mail.somemta.com ESMTP

To finish handshake with the mta enter the following line;
HELO mail.somemta.com

The mta will respond us, telling us that handshaking is ok;
250 mail.somemta.com

At this stage we will inform mta about the email's sender and recipient.
To inform the sender account enter the following line;
mail from:[email protected]

Mta will respond us telling us tht he has received the information.
250 ok

Now we'll send the recipient information by entering the following line;
rcpt to:[email protected]

Mta should respond us as follows;
250 ok

Hope you don't have any problems J
Now we are ready to write the content of our email.The information we'll enter here will be shown in mua.
We declare mta tht we are ready to enter the email content by entering the following line;
data

Mta should respond as follows;
354 go ahead

Till now mta was responding us on each of our command by sending us the message "250 ok"
But after we enter the "data" command mta won't respond our comments until we enter the escape command.
The escape command is ?lt;Enter> . <Enter>?(press enter , press . and enter , press enter)?br>
Let's write the contentFrom:Levent YILDIZ ACME Inc.
To:Levent YILDIZ MINDSTORM Inc.
Subject:our first mail sent by C#?br>
This mail has been sent by C#

.

After we enter the escape command mta should respond as follows;
250 ok 1118680536 qp 21248

This ok message can vary by the type of the mta.The important section is the "250 ok" part.

Now we can check our email.

If you have experienced any problems till this stage, that problems may have been occured by the securty properties of your mta.Most of the mta systems use "POP-before-SMTP" securty which does not allow users to connect to SMTP port if the user has not been granted by the POP protocol.I advise you to get support from your mail administrator.

So let's get to the point.How we will achieve this by C#?Below code is all you need.Take a look at it...

using System;
using System.Net.Sockets;
using System.IO;
namespace SendMailviaSMTP

 

Namespaces we used;
System
System.Net.Sockets
System.IO

We used sockets ns to connect to MTA by tcpclient class, IO ns to receive and send the information arrived to netstream object.
You can find the members of our class below

Member Name Description
mvarSMTPServerAddress 揻ully qualified domain name?or the ip address of the MTA (string)
mvarSenderName The sender name that will shown in MUA (string)
mvarSenderEmailAddress The sender email account that we'll inform to MTA. (string)
mvarRecipientName The recipient name that will shown in MUA (string)
mvarRecipientEmailAddress The recipient email account that we'll inform to MTA (string)
mvarEmailSubject Subject of our email (string)
mvarEmailBody The body of our email (string)
mvarSMTPTimeOut Timeout in seconds. (int, default=60)
mvarSMTPRemotePort The port number of the MTA (int, default=25)
SendEmail The method that sends the email. (bool)

We've set the default values of our two properties in our default constructor method.

public SMTPMailSender()        



1- We connect to MTA
The method will return a false value at any line if any error occurs.This will help us to know that the mail is sent successfully or not.

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.
I'll explain the WaitForResponse method later so it is ok for you to know that this method waits for the mta response.  

 

//mta'dan karlama 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.  
//zamanam?kontrol?i鏸n mevcut tarih saat bilgisi al齨齳or
//gathering current date time data for timeout check
dteTimeOutCheck=DateTime.Now;
//zamanam?de餰ri bulunuyor
//calculating timeout value
TimeSpan tsp=DateTime.Now-dteTimeOutCheck;
//zamanam?de餰ri kullan齝齨齨 belirledi餴 de餰ri ancaya kadar d鰊g?鏰lt齬齦齳or
//looping code until the timeout exceeds the user defined value
while(tsp.Seconds<mvarSMTPTimeOut)
We store the current datetime value to the dteTimeOutCheck variable.
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.
//checking if MTA has sent a message to us
if (nstSMTP.DataAvailable)
We check that if there is any data waiting to be received in the first line.

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#图像处理超酷效果(附有例程)发布时间:2022-07-10
下一篇:
【转】C#网络编程(异步传输字符串)发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap