A few days ago, students asked how to use C # To send emails online. Article A lot. I found an article for the students and asked them to take a look. As a result, it was not completed in a day, so I had to take a closer look. Use C # To send emails. Many articles on the Internet use smtpclient and System. Web. Mail. However, system. Web. Mail is no longer recommended. The following is a common method for sending emails on the Internet: Code : Code
1 Mailmessage myemilmessage = New Mailmessage ();
2 String Attachfile; // String variables used to construct attachments
3
4 Myemailmessage. From = " Writer@sadf.com " ;
5 Myemailmessage. = " Geter@dff.com tome@asd.com " ;
6 Myemailmessage. Subject = " A simple example of email sending " ;
7 Myemailmessage. Body = " The cat king calls the mouse. Please answer !!! " ;
8 Myemailmessage. bodyformat = Mailformat. text;
9 Myemailmessage. Priority = Mailpriority. High;
10
11 // Add attachments
12 Attachfile = @" D: \ test \ test.txt " ;
13 Mailattachment mailattach = New Mailattachment (attachfile );
14 Myemailmessage. attachments. Add (mailattach );
15
16 // Send email
17 Smtpmail. smtpserver = " SMTP address or IP address used to send an email " ; // SMTP Server
18 Smtpmail. Send (myemailmessage ); // Send email
19
There are basically no errors in these codes, and compilation will also pass. However, if you use this method to send, you can basically ensure that your email is not sent successfully. Then, there is a lack of authentication, and it is troublesome to do so. The following is an example for students: Code
1 String Mailcontent = " Email content " ;
2
3 Mailmessage msg = New System. net. Mail. mailmessage ();
4 MSG. to. Add ( " * ***** @ Gmail.com " ); // Recipient
5
6 // Sender Information
7 MSG. From = New Mailaddress ( " * ***** @ Gmail.com " , " Sender name " , System. Text. encoding. utf8 );
8 MSG. Subject = " This is the test email " ; // Email Subject
9 MSG. subjectencoding = System. Text. encoding. utf8; // Title Encoding
10 MSG. Body = Mailcontent; // Email Subject
11 MSG. bodyencoding = System. Text. encoding. utf8;
12 MSG. isbodyhtml = True ; // HTML?
13 MSG. Priority = Mailpriority. High; // Priority
14
15 Smtpclient Client = New Smtpclient ();
16 // Set the Gmail mailbox and password
17 Client. Credentials = New System. net. networkcredential ( " Account @ gmail.com " , " Password " );
18 Client. Port = 587 ;
19 Client. Host = " Smtp.gmail.com " ;
20 Client. enablessl = True ;
21 Object Userstate = MSG;
22 Try
23 {
24Client. Send (MSG );
25MessageBox. Show ("Sent successfully");
26}
27 Catch (Exception ex)
28 {
29MessageBox. Show (ex. Message,"An error occurred while sending the email.");
30}
31
the focus here is on the location of email authentication. You must not write an error. Otherwise, the email cannot be sent. However, you will find that the same Program can be successfully sent on some machines, on some machines, sending always fails. Why is it the system? It does not seem to be because some of the same systems have successfully sent and some failed to send. That is, why is the framework? Neither does it seem.
here is a very confusing phenomenon. In the same system and framework, some of them can still be sent successfully, while others cannot. The specific cause of the failure is that the mail header encoding is incorrect. So we can remove the subject without writing it. Can we use the default one? The same is true. This problem is a very bad thing, so I used etherpeek to capture packets and found an interesting phenomenon. Here there is a very important package:
note the red dashes. "ehlo xqxpc" indicates my machine name is xqxpc. The problem lies here, when smtpclient sends an email, it needs to send an EHLO command to the email server. The parameter is the machine name. If the machine name is in Chinese, it cannot be sent successfully.
Summary:
1. It is very easy to use smtpclient to send emails, as long as the mailmessage object and smtpclient are created correctly, the email can be easily sent out.
2. If the email server requires authentication [it seems that authentication is required now, unless configured on its own], pay attention to the following sentence:
smtpclient. credentials = new system. net. networkcredential ("account @ gmail.com", "password");
3. The machine name cannot use Chinese characters. PS: Why do people disagree with such an article? Why? Can't I write anything here in the future? Haha ......, I 've been hesitating whether to open a blog here. Keep hesitating ......