SMTP using. Net to send emails (simple version)

Source: Internet
Author: User
Tags mailmessage
This article mainly discusses using the mailmessage and smtpmail classes in the system. Web. Mail namespace to simply implement the mail sending function.

Procedure andCodeAs follows: (the example is implemented using winform)

1. Add the reference to the project -->. Net --> Find system. Web. dll, OK, and add it.

2. Add the using code:UsingSystem. Web. mail;

3. Write the core sending code: Private   Void Sendmail ( String Server, String From, String To, String Subject, String Body, String Username, String Password)
{
Try
{
Mailmessage mail =   New Mailmessage ();
Mail. From = From;
Mail. = To;
Mail. Subject = Subject;
Mail. Body = Body;
Mail. bodyformat = Mailformat. html;
Mail. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate " , " 1 " );
Mail. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/sendusername " , Username );
Mail. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/sendpassword " , Password );

Smtpmail. smtpserver = Server;
Smtpmail. Send (Mail );
}
Catch (Exception ex)
{
MessageBox. Show (ex. Message+Ex. innerexception. Message );
}
}

Note that the Sendmail method has the following three sentences: Mail. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate " , " 1 " );
Mail. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/sendusername " , Username );
Mail. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/sendpassword " , Password );

Verification information. If no information is written or the user name and password are incorrect, a prompt is displayed.
"Failed to access the CDO. Message object"
This is the most common error on the Internet. You can set a breakpoint to capture the error point and check the cause.
Here are several solutions to common CDO errors:
Jun. Net: http://blog.joycode.com/ghj/archive/2004/10/22/36274.aspx
Systemwebmail: http://www.systemwebmail.com/faq/4.2.3.aspx

4. You can call this method as needed. Remember to input the appropriate parameters.

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.