Use Visual C # to send an email

Source: Internet
Author: User
Tags mailmessage

Http://www.qqgb.com/Program/CCC/CCCnetprogram/Program_91487.html

 

Visual C # is the next generation launched by MicrosoftProgramDevelopment language. It not only has the powerful features of Visual C ++, but also has the concise and easy-to-use features of Visual Basic. So once launched, we were welcomed by the majority of program developers. One obvious difference between Visual C # and Visual C ++ is that Visual C # itself has no class libraries, while visual c ++ itself has class libraries. Although Visual C # does not have a class library, it is a very important development language in the. NET Framework. He can use the. NET Framework SDK, a common software development kit provided by the. NET Framework. This software development kit can be said to be an extension of the Visual C # function. Visual C # is a multi-function that cannot be implemented by itself. This article describes how Visual C # uses this software development kit to send emails.

1. Environment Settings for software development and operation

I>. Windows 2000 Server Edition

II>. NET Framework SDK beta 2

III>. Open "Control Panel", go to "add and delete programs", and then click "Add/delete Windows Components". The following page is displayed:

Figure 01: system configuration page

Click "Internet Information Service (IIS)" and click "details". The following page is displayed:

Figure 02: system configuration page

Click "SMTP serverce", and then click "OK. Then press "Next", the system will install the SMTP service required by the program.

Ii. Visual C # How to send an email:

In. NET Framework SDK beta 2, there is a namespace called system. Web. Mail, which encapsulates the methods, objects, and attributes for sending emails. Visual C # sends an email by calling methods, objects, and attributes in the namespace. In this article, email sending mainly uses two objects: one is the mailmessage object, which encapsulates all attributes of the email, that is, the so-called sender and recipient, the subject of the letter, the content of the letter, and the attachment of the letter. The other one is the smtpmail object. The maximum function of this object is to send the mailmessage object with various attributes defined. To complete this function, you need to call the send () of the smtpmail object () method.

3. correctly use email-related objects in Visual C:

(1). To call an object, first import the namespace of the encapsulated object at the beginning of the program, as shown below:

Using system. Web. mail;

(2) correctly define attributes of the mailmessage object:

Attributes related to emails in the mailmessage object can be expressed in the following table:

Attribute name Meaning
From Source Address
To Destination Address
Subject Email Subject
Priority Mail priority (high, low, normal)
Attachments Attachment
BCC Dark delivery address
CC CC address
Body Email Subject
Bodyformat Email format (HTML, text)
Bodyencoding Email encoding (base64, uuencode)

In the program, the specific implementation statement is as follows:

Mailmessage amessage = new mailmessage ();

// Create a new mailmessage object

Amessage. From = fromtextbox. text;

// Define the sender address. If there are multiple users, separate them with commas (,).

Amessage. To = totextbox. text;

// Define the recipient address. If there are multiple recipients, separate them with commas (,).

Amessage. Cc = cctextbox. text;

// Defines the CC address. If there are multiple recipients, separate them with commas (,).

Amessage. bcc = bcctextbox. text;

// Defines the hidden recipient address. If there are multiple recipients, separate them with commas (,).

Amessage. Subject = subjecttextbox. text;

// Define the subject of the email

Amessage. Body = messagetextbox. text;

// Define the mail content

If (attachmenttextbox. Text. length> 0)

Amessage. attachments. Add (New mailattachment (attachmenttextbox. Text, mailencoding. base64 ));

// Add an attachment to the email

Note: "=" the right side is the "text" value of the text box defined in the program.

(3). Use the smtpmail object to correctly send the email:

There are multiple methods to call the send () method of the smtpmail object in Visual C. This article only describes one of the more common call methods, that is, smtpmail. Send (mailmessage object ).

The implementation statement in the program is as follows:

Smtpmail. Send (amessage );

4. source code in this articleCode(Send. CS) and the program running interface:

Is the running interface of the compiled program:

Figure 03: Send. CS program running interface

 

The following is the source code of send. CS:

Using system;

Using system. drawing;

Using system. collections;

Using system. componentmodel;

Using system. Windows. forms;

Using system. Data;

Using system. Web;

Using system. Web. mail;

// The namespace used in the import program

Public class form1: Form

{

Private Label label1;

Private Label label2;

Private Label label3;

Private button sendbutton;

Private button exitbutton;

Private textbox fromtextbox;

Private textbox totextbox;

Private textbox subjecttextbox;

Private textbox messagetextbox;

Private textbox cctextbox;

Private Label cclabel;

Private textbox bcctextbox;

Private Label label4;

Private Label label5;

Private button browsebutton;

Private openfiledialog openfiledialog1;

Private textbox attachmenttextbox;

Private system. componentmodel. Container components = NULL;

Public form1 ()

{

Initializecomponent ();

}

// Clear all resources used in the program

Protected override void dispose (bool disposing)

{

If (disposing)

{

If (components! = NULL)

{

Components. Dispose ();

}

}

Base. Dispose (disposing );

}

Private void initializecomponent ()

{

Messagetextbox = new Textbox ();

Totextbox = new Textbox ();

Sendbutton = new button ();

Exitbutton = new button ();

Fromtextbox = new Textbox ();

Label1 = new label ();

Subjecttextbox = new Textbox ();

Label2 = new label ();

Label3 = new label ();

Cctextbox = new Textbox ();

Cclabel = new label ();

Bcctextbox = new Textbox ();

Label4 = new label ();

Label5 = new label ();

Attachmenttextbox = new Textbox ();

Browsebutton = new button ();

Openfiledialog1 = new openfiledialog ();

Fromtextbox. Location = new system. Drawing. Point (96, 16 );

Fromtextbox. Name = "fromtextbox ";

Fromtextbox. size = new system. Drawing. Size (240, 20 );

Fromtextbox. tabindex = 0;

Fromtextbox. Text = "";

Totextbox. Location = new system. Drawing. Point (96, 53 );

Totextbox. Name = "totextbox ";

Totextbox. size = new system. Drawing. Size (240, 20 );

Totextbox. Text = "";

Totextbox. tabindex = 1;

Cctextbox. Location = new system. Drawing. Point (96, 88 );

Cctextbox. Name = "cctextbox ";

Cctextbox. size = new system. Drawing. Size (240, 20 );

Cctextbox. tabindex = 2;

Cctextbox. Text = "";

Bcctextbox. Location = new system. Drawing. Point (96,120 );

Bcctextbox. Name = "bcctextbox ";

Bcctextbox. size = new system. Drawing. Size (240, 20 );

Bcctextbox. tabindex = 3;

Bcctextbox. Text = "";

Subjecttextbox. Location = new system. Drawing. Point (96,152 );

Subjecttextbox. Name = "subjecttextbox ";

Subjecttextbox. size = new system. Drawing. Size (240, 20 );

Subjecttextbox. tabindex = 4;

Subjecttextbox. Text = "";

Attachmenttextbox. Location = new system. Drawing. Point (96,184 );

Attachmenttextbox. Name = "attachmenttextbox ";

Attachmenttextbox. size = new system. Drawing. Size (168, 20 );

Attachmenttextbox. tabindex = 5;

Attachmenttextbox. Text = "";

Messagetextbox. Location = new system. Drawing. Point (8,216 );

Messagetextbox. multiline = true;

Messagetextbox. Name = "messagetextbox ";

Messagetextbox. size = new system. Drawing. Size (320,152 );

Messagetextbox. Text = "";

Messagetextbox. tabindex = 6;

Browsebutton. Location = new system. Drawing. Point (280,184 );

Browsebutton. Name = "browsebutton ";

Browsebutton. size = new system. Drawing. Size (56, 24 );

Browsebutton. Text = "Browse Files ";

Browsebutton. tabindex = 7;

Browsebutton. Click + = new system. eventhandler (browsebutton_click );

Sendutton. Location = new system. Drawing. Point (64,380 );

Sendbutton. Name = "sendbutton ";

Sendbutton. size = new system. Drawing. Size (72, 24 );

Sendbutton. Text = "Send email ";

Sendbutton. tabindex = 8;

Sendbutton. Click + = new system. eventhandler (sendbutton_click );

Exitbutton. Location = new system. Drawing. Point (200,380 );

Exitbutton. Name = "exitbutton ";

Exitbutton. size = new system. Drawing. Size (72, 24 );

Exitbutton. Text = "Exit program ";

Exitbutton. tabindex = 9;

Exitbutton. Click + = new system. eventhandler (exitbutton_click );

Label1.font = new system. Drawing. Font ("", 9f );

Label1.location = new system. Drawing. Point (48, 16 );

Label1.name = "label1 ";

Label1.size = new system. Drawing. Size (48, 16 );

Label1.text = "Sender :";

Label2.font = new system. Drawing. Font ("", 9f );

Label2.location = new system. Drawing. Point (48, 53 );

Label2.name = "label2 ";

Label2.size = new system. Drawing. Size (48, 16 );

Label2.text = "Recipient :";

Label3.font = new system. Drawing. Font ("", 9f );

Label3.location = new system. Drawing. Point (48,152 );

Label3.name = "label3 ";

Label3.size = new system. Drawing. Size (48, 16 );

Label3.text = "Subject :";

Cclabel. font = new system. Drawing. Font ("", 9f );

Cclabel. Location = new system. Drawing. Point (48, 88 );

Cclabel. Name = "cclabel ";

Cclabel. size = new system. Drawing. Size (48, 16 );

Cclabel. Text = "cc :";

Label4.font = new system. Drawing. Font ("", 9f );

Label4.location = new system. Drawing. Point (48,120 );

Label4.name = "label4 ";

Label4.size = new system. Drawing. Size (48, 16 );

Label4.text = "BCC :";

Label5.font = new system. Drawing. Font ("", 9f );

Label5.location = new system. Drawing. Point (48,184 );

Label5.name = "label5 ";

Label5.size = new system. Drawing. Size (48, 16 );

Label5.text = "attachment :";

Openfiledialog1.title = "select a file as an attachment to the email :";

This. autoscalebasesize = new system. Drawing. Size (5, 13 );

This. clientsize = new system. Drawing. Size (344,413 );

This. Controls. Add (browsebutton );

This. Controls. Add (attachmenttextbox );

This. Controls. Add (label5 );

This. Controls. Add (label4 );

This. Controls. Add (bcctextbox );

This. Controls. Add (cclabel );

This. Controls. Add (cctextbox );

This. Controls. Add (exitbutton );

This. Controls. Add (sendbutton );

This. Controls. Add (label3 );

This. Controls. Add (label2 );

This. Controls. Add (label1 );

This. Controls. Add (subjecttextbox );

This. Controls. Add (totextbox );

This. Controls. Add (fromtextbox );

This. Controls. Add (messagetextbox );

This. Name = "form1 ";

This. Text = "Use Visual C # As the mail sending software! ";

This. resumelayout (false );

}

Static void main ()

{

Application. Run (New form1 ());

}

Private void sendbutton_click (Object sender, system. eventargs E)

{

Try

{

Mailmessage amessage = new mailmessage ();

// Create a new mailmessage object

Amessage. From = fromtextbox. text;

// Define the sender address. If there are multiple users, separate them with commas (,).

Amessage. To = totextbox. text;

// Define the recipient address. If there are multiple recipients, separate them with commas (,).

Amessage. Cc = cctextbox. text;

// Defines the CC address. If there are multiple recipients, separate them with commas (,).

Amessage. bcc = bcctextbox. text;

// Defines the hidden recipient address. If there are multiple recipients, separate them with commas (,).

Amessage. Subject = subjecttextbox. text;

// Define the subject of the email

Amessage. Body = messagetextbox. text;

// Define the mail content

If (attachmenttextbox. Text. length> 0)

Amessage. attachments. Add (New mailattachment (attachmenttextbox. Text, mailencoding. base64 ));

// Add an attachment to the email

Smtpmail. Send (amessage );

// Send an email

MessageBox. Show ("email has been sent to->" + totextbox. Text );

}

Catch (exception ex)

{

MessageBox. Show (ex. Message. tostring ());

}

}

Private void exitbutton_click (Object sender, system. eventargs E)

{

Application. Exit ();

}

Private void browsebutton_click (Object sender, system. eventargs E)

{

If (openfiledialog1.showdialog () = dialogresult. OK)

{

Attachmenttextbox. Text = openfiledialog1.filename;

}}

}

V. Summary

So far, we have used Visual C # to complete a fully functional email sending software. It can be seen that the. NET Framework SDK plays a key role in the program. It is its rich class library that makes it easy to send emails. In fact, you only have mastered the class libraries in the. NET Framework SDK. Visual C # can play its biggest function.

Related Article

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.