Отправка электронной почты

protected string sendMail(string from, string to, string cc, string bcc, string subject, string body)
{
//Отсылка через System.Web.Mail
// Mail initialization
System.Web.Mail.MailMessage mailMsg = new System.Web.Mail.MailMessage();
mailMsg.From = from;
mailMsg.To = to;
mailMsg.Cc = cc;
mailMsg.Bcc = bcc;
mailMsg.Subject = subject;
mailMsg.BodyFormat = System.Web.Mail.MailFormat.Text;
mailMsg.Body = body;
mailMsg.Priority = System.Web.Mail.MailPriority.High;
// Smtp configuration
System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com";//smtp is :smtp.gmail.com
// - smtp.gmail.com use smtp authentication
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "myemail@gmail.com");
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "mypassword");
// - smtp.gmail.com use port 465 or 587
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");//port is: 465, 25 default
// - smtp.gmail.com use STARTTLS (some call this SSL)
mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
// try to send Mail
try
{
System.Web.Mail.SmtpMail.Send(mailMsg);
return "";
}
catch (Exception ex)
{
return ex.Message;
}
}

Вариант № 2
protected void ExecuteHtmlSendMail(string FromAddress, string ToAddress, string BodyText, string Subject)
{
//System.Net.Mail can only send email.
System.Net.Mail.MailMessage mailMsg = new System.Net.Mail.MailMessage();
mailMsg.From = new System.Net.Mail.MailAddress(FromAddress);
mailMsg.To.Add(new System.Net.Mail.MailAddress(ToAddress));
mailMsg.Subject = Subject;
mailMsg.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
System.Net.Mail.AlternateView plainView = System.Net.Mail.AlternateView.CreateAlternateViewFromString
(System.Text.RegularExpressions.Regex.Replace(BodyText, @"", string.Empty), null, "text/plain");
System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(BodyText, null, "text/html");
mailMsg.AlternateViews.Add(plainView);
mailMsg.AlternateViews.Add(htmlView);
// Smtp configuration
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.Host = "mail.bistriy.com";
smtp.Credentials = new System.Net.NetworkCredential("email", "password");
smtp.EnableSsl = true;

smtp.Send(mailMsg);
}


Вариант № 3
protected void SendMail_Web_Mail()
{
try
{
System.Web.Mail.MailMessage oMsg = new System.Web.Mail.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 Using Web Mail";
// SEND IN HTML FORMAT (comment this line to send plain text).
oMsg.BodyFormat = System.Web.Mail.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";
System.Web.Mail.MailAttachment oAttch = new System.Web.Mail.MailAttachment(sFile, System.Web.Mail.MailEncoding.Base64);
oMsg.Attachments.Add(oAttch);
// TODO: Replace with the name of your remote SMTP server.
System.Web.Mail.SmtpMail.SmtpServer = "MySMTPServer";
System.Web.Mail.SmtpMail.Send(oMsg);
oMsg = null;
oAttch = null;
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
}