Let's take a look at the synchronous email sendingCode
Code
Smtpclient _ smtpclient = New Smtpclient ();
_ Smtpclient. Host = " Email server " ;
_ Smtpclient. Credentials = New Networkcredential ( " User Name " , " Password " );
Mailmessage message = New Mailmessage ();
Message. From = New Mailaddress ( " Hamptest@hampoo.com " , " Sender " , Encoding. getencoding ( 936 ));
Message. to. Add ( " Tianhu.jin@hampoo.com " ); // This is a correct address.
Message. to. Add ( " Dayan.qiu@hampoo.com " ); // This is a correct address.
Message. to. Add ( " Jieyuan.jin@hampoo.com " ); // This is an invalid address.
Message. Subject = " 111 " ;
Message. Body = " 222 " ;
// Synchronous Transmission
_ Smtpclient. Send (Message );
There are three Email recipients, and the third is invalid email addresses.
An exception is thrown: the email name cannot be used. Server Response: RP: rdn http://www.ipmotor.com/smtp_err.htm
However, the other two recipients still receive the email, for example:
Next let's take a look at asynchronous sending. The Code is as follows:
Code
Smtpclient _ smtpclient = New Smtpclient ();
_ Smtpclient. Host = " Server address " ;
_ Smtpclient. Credentials = New Networkcredential ( " User Name " , "Password " );
Mailmessage message = New Mailmessage ();
Message. From = New Mailaddress ( " Hamptest@hampoo.com " , " Sender " , Encoding. getencoding ( 936 ));
Message. to. Add ( " Tianhu.jin@hampoo.com " ); // This is a correct address.
Message. to. Add ( " Dayan.qiu@hampoo.com " ); // This is a correct address.
Message. to. Add ( " Jieyuan.jin@hampoo.com " ); // This is an invalid address.
Message. Subject = " 111 " ;
Message. Body = " 222 " ;
// Asynchronous transmission
_ Smtpclient. sendcompleted + = New Sendcompletedeventhandler (sendcompletedcallback );
_ Smtpclient. sendasync (message, Null );
There are also three Email recipients, and the third is invalid email addresses.
After execution, the callback function can capture the exception information "the object application is not set to the object instance", but the other two recipients will not receive the email.
According to my personal understanding, synchronous and asynchronous operations are only executed in different ways, and the execution results are always the same, right? But obviously the results are different in sending emails,
Should it be a bug?
PS: I wanted to use it to send emails asynchronously in the project. Later, because the email address provided by the customer is not necessarily legal, I found this bug and finally had to switch
A new thread is opened to send emails synchronously in this thread.