Emails supporting ESMTP Authentication

Source: Internet
Author: User
Tags getstream

Using system. net. Mail. smtpclient under. NET Framework can easily send emails, but many email servers now support ESMTP protocol. The biggest difference between ESMTP and ESMTP is that the account and password must be provided for verification during sending. ESMTP sends commands in a stream to interact with the server to send and receive emails. For more SMTP commands, see: http://www.magicwinmail.com/technic_smtp.php

 

The following is a simple example: (no judgment or encapsulation is made for reference only)

Code

  String  SMTP  =     "Mail.com  "  ;
String From = Send@mail.com " ;
String PWD = "Pwd " ;
String To = Rec@mail.com " ;

// Create Link
Tcpclient TC = New Tcpclient (SMTP, 25 );
Byte [] B = New Byte [ 1024 ];
TC. getstream (). Read (B, 0 , B. Length );
Response. Write (encoding. utf8.getstring (B) + " <Br/> " );

// Identity
Writemsg (TC. getstream (), " Helo XXXX \ r \ n " );
Response. Write (readmsg (TC. getstream ()) + " <Br/> " );

// Authentication starts
Writemsg (TC. getstream (), " Auth login \ r \ n " );
Response. Write (readmsg (TC. getstream ()) + " <Br/> " );

// Account base64 encoding
Byte [] Btxt = New Byte [ 1024 ];
Btxt = Encoding. utf8.getbytes (from );
Writemsg (TC. getstream (), convert. tobase64string (btxt) + " \ R \ n " );
Response. Write (readmsg (TC. getstream ()) + " <Br/> " );

// Base64 encoding
Btxt = Encoding. utf8.getbytes (PWD );
Writemsg (TC. getstream (), convert. tobase64string (btxt) + " \ R \ n " );
Response. Write (readmsg (TC. getstream ()) + " <Br/> " );
// Authentication completed

// Email sending Address
Writemsg (TC. getstream (), String . Format ( " Mail from: {0} \ r \ n " , From ));
Response. Write (readmsg (TC. getstream ()) + " <Br/> " );

// Accept email address, which can have multiple RCPT
Writemsg (TC. getstream (), String . Format ( " Rcpt to: {0} \ r \ n " , ));
Response. Write (readmsg (TC. getstream ()) + " <Br/> " );

// Ru Hua
Writemsg (TC. getstream (), " Data \ r \ n " );
Response. Write (readmsg (TC. getstream ()) + " <Br/> " );

// Email content
Stringbuilder sb = New Stringbuilder ();
SB. appendformat ( " From: {0} \ r \ n " , From ); // Sender
SB. appendformat ( " To: {0} \ r \ n " , ); // There can be multiple recipients separated by semicolons. These two lines can be different from the preceding mail from and rcpt to (displayed in the mail content)
SB. appendformat ( " Date: {0} \ r \ n " , Datetime. Now. tostring ()); // Time
SB. appendformat ( " Subject: {0} \ r \ n " , " Test " ); // Topic
SB. append ( " \ R \ n " );
SB. append ( " Content " ); // Body
SB. append ( " \ R \ n. \ r \ n " );

Writemsg (TC. getstream (), SB. tostring ());
Response. Write (readmsg (TC. getstream ()) + " <Br/> " );

// End session
Writemsg (TC. getstream (), " Quit " );

// Close link
TC. Close ();

 

Code

  Private     Void  Writemsg (networkstream NS,  String  MSG)
{
Byte [] BW = Encoding. utf8.getbytes (MSG );
NS. Write (BW, 0 , BW. Length );
}
Private String Readmsg (networkstream NS)
{
Byte [] Br = New Byte [ 1024 ];
NS. Read (BR, 0 , Br. Length );
Return Encoding. utf8.getstring (BR );
}

 

 

If the operation is normal, the returned status is as follows:

 
  220 mail.com ESMTP CMailServer 5.4.6 SMTP Service ready
250 welcome here
334 vxnlcm5hbwu6
334 ugfzc3dvcmq6
235 OK
250 OK
250 OK
354 send the mail data, end.
250 OK

 

We should be familiar with the SMTP protocol and related commands, and send data in the format to interact with the server. Note the space in the center of the command and the CRLF at the end (Press enter to wrap the line ).

 
Tags: SMTP, ESMTP, email, email, identity authentication
Http://chy710.cnblogs.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.