How to: Use System.Web.mail and Visual C # .NET to send email (from MSDN)

xiaoxiao2021-03-06  59

Summary This article demonstrates how to use

System.Web.mail Sends an email in Visual C # .NET.

More information

Start Microsoft Visual Studio .NET. In the File menu, click New, and then click Project. Click Visual C # items, console application templates, and then click OK. Create Class1.cs by default. Add a reference to System.Web.dll. To do this, do the following steps:

In the project menu, click Add Reference. In the .NET option page, find System.Web.dll, and then click Select. In the Add Reference dialog box, click OK to accept the selected selection. If you prompt you to generate a package for the selected library, click Yes. In the code window, replace the entire code with the following: USING SYSTEM;

Using system.web.mail;

Namespace Webmail

{

Class class1

{

Static void main (string [] args)

{

Try

{

MailMessage OMSG = New MailMessage ();

// Todo: Replace with sender e-mail address.

omsg.from = "sender@somewhere.com";

// Todo: Replace with Recipient E-mail address.

OMSG.to = "Recipient@somewhere.com";

OMSG.SUBJECT = "Send useing web mail";

// send in Html Format (Comment this Line to send Plain Text).

OMSG.BODYFORMAT = mailformat.html;

// HTML Body (Remove HTML Tags for Plain Text).

OMSG.BODY = " Hello World! ";

// add an attachment.

// Todo: Replace with path to attachment.

String sfile = @ "c: /TEMP/Hello.txt";

Mailattachment oattch = new mailattachment (sfile, mailencoding.base64);

OMSG.Attachments.Add (oattch);

// Todo: Replace with the name of your remote smtp server.

SMTPMAIL.SMTPSERVER = "MySMTPServer";

SMTPMAIL.SEND (OMSG);

OMSG = NULL;

oattch = null;

}

Catch (Exception E)

{

Console.writeline ("{0} exception caught.", E);

}

}

}

}

Modify the code at "Todo". Press F5 to compile and run the program. Verify that the email has been sent and received.

The information in this article applies to:

Microsoft Visual C # .NET (2002)

Recent Updated: 2004-3-25 (1.2) Keywords: kbhowto KB310273

转载请注明原文地址:https://www.9cbs.com/read-109061.html

New Post(0)