繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> Asp.Net开发 >> SmtpMail and MailMessage : Send mails in .NET

SmtpMail and MailMessage : Send mails in .NET

2007-07-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介:SmtpMail and MailMessage : Send mails in .NET Tools UsedVisual C# .NETNamespaceSystem.Web.UtilAssemblySystem.Web.dll The SmtpMail class can be used to send from your C# application. Mail is by def...

SmtpMail and MailMessage : Send mails in .NET

Tools UsedVisual C# .NETNamespaceSystem.Web.UtilAssemblySystem.Web.dll

The SmtpMail class can be used to send from your C# application. Mail is by default queued on the system, ensuring that the calling program does not block network traffic. The SmtpMail class is defined in the namespace System.Web.Util. You need to call

using System.Web.Util

before you use SmtpMail. This class has only one member function Send. Send sends a mail message. The Send method is overloaded. Either a MailMessage class or four arguments can be passed to the Send message. You can call the Send method in two manners:

SmtpMail.Send(txtFrom.Text, txtTo.Text, txtSubject.Text, txtMessage.Text);

Or, if you don't want to call System.Web.Util.

System.Web.Util.Smptmail.Send(txtFrom.Text, txtTo.Text, txtSubject.Text, txtMessage.Text);

You can call the Send method in two ways.

1. By passing MailMessage as a parameter

public static void Send(MailMessage);

Here MailMessage is a class.

MailMessage mailMsg = new MailMessage();

mailMsg .From = "from@fromServer.com";

mailMsg .To = "to@toServer.com";

mailMsg .Cc = "cc@ccServer.com"";

mailMsg .Bcc = "bcc@bccServer.com";

mailMsg .Subject = "SubjectOfTheMailString";

mailMsg .Body = "BodyOfTheMailString";

SmtpMail.Send(mailMsg );

2. Direct method.

public static void Send(string from , string to, string subject, string messageText);

rametersDescriptionfromEmail address of sendertoEmail address of recipientsubjectEmail subject line.messageTextBody of Email message

Example:

SmtpMail.Send("mcb@mindcracker.com", "webmaster@mindcracker.com", "Subject", "Message body");

The MailMessage Class

The MailMessage class can be used to send mails with cc, bcc or attachments. The MailMessage constructor initializes a new instance of the MailMessage class. It doesn't take any parameters.

MailMessage mail = new MailMessage();

Members DescriptionBccSpecifies a semicolon-separated list of email addresses that will receive a private copy of the email message.BodyIndicates the body content of the email message.BodyEncodingIndicates the encoding type of the email message.BodyFormatIndicates the type of the email message body (text or HTML).CcSpecifies a semicolon-separated list of email addresses that will receive a copy of the email message.FromIndicates the email address of sender.PriorityIndicates the priority of the email message.SubjectIndicates the subject line of the email.ToIndicates the email address of the recipientUrlContentBaseIndicates the base of all relative URLs used within an HTML-encoded message.AttachmentsList of MailAttachments to be transmitted with the message. This property is read-only.HeadersIndicates the dictionary of custom headers to be transmitted with the message. This property is read-only.

Each member of this class are pretty easy to understand. Say you want to send an attachment with the mail. You use Attachment member to attach the mail. Say I want to attach mcb.jpg with the mail.

mail.Attachments.Add(new MailAttachment(@"C:\mcb.jpg"));

Each member of this class are pretty easy to understand. Say you want to send an attachment with the mail. You use Attachment member to attach the mail.

Other MailMessage members have been used in the above example.

About the Author:

Mahesh is Admin and the founder of this site. He has been programming in C++, MFC, Visual Basic, COM, ATL over 5 years.

责任编辑:admin
相关文章