用户登录  |  用户注册
首 页商业源码原创产品编程论坛
当前位置:PB创新网文章中心.Net

SmtpMail and MailMessage : Send mails in .NET

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2009-03-16 15:55:06
SmtpMail and MailMessage : Send mails in .NET
Tools UsedVisual C# .NET
NamespaceSystem.Web.Util
AssemblySystem.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);
rametersDescription
fromEmail address of sender
toEmail address of recipient
subjectEmail 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 Description
BccSpecifies 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 recipient
UrlContentBaseIndicates 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. 

Tags:

作者:佚名

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
PB创新网ourmis.com】Copyright © 2000-2009 . All Rights Reserved .
页面执行时间:7,687.50000 毫秒
Email:ourmis@126.com QQ:2322888 蜀ICP备05006790号