Sunday, June 29, 2008

Examples of Sending Email in C#

In order to send email in C# code, do the following:

// create mail message object
MailMessage mail = new MailMessage();
mail.From = ""; // put the from address here
mail.To = ""; // put to address here
mail.Subject = ""; // put subject here
mail.Body = ""; // put body of email here
SmtpMail.SmtpServer = ""; // put smtp server you will use here
// and then send the mail
SmtpMail.Send(mail);


Here is an another example

// Send a quick e-mail message
SendEmail.SendMessage("This is a Test", "This is a test message...", "noboday@nowhere.com", "somebody@somewhere.com", "ccme@somewhere.com");


Here is a full code of sending email

//This sample demonstrates how to send email in CSharp
using System;
using AOSMTPLib;

namespace CSharpSample
{
class SendEmail
{
public static void Main( string[] args )
{
AOSMTPLib.MailClass SMTP = new AOSMTPLib.MailClass(); //create component instance
//If you don't specify the value of ServerAddr property,
//ANSMTP would simulate a smtp server to send email automatically.
SMTP.ServerAddr = "smtp.component.com"; //email server
SMTP.FromAddr = "Asp@component.com"; //sender' email address
SMTP.AddRecipient( "Dennis", "Dennis@component.com", 0 ); //normal recipient
SMTP.BodyFormat = 1; //html format
SMTP.Subject = "send email asp asp.net, smtp, control, dotnet"; //email subject
SMTP.BodyText =".... ocx, library, activex, component, NET assemble";//body
if( SMTP.SendMail() == 0 )//send email
Console.WriteLine( "Email delivered");
else
Console.WriteLine( SMTP.GetLastErrDescription() );
}
}
}

2 comments:

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

Anonymous said...

This code is fine!
Could anyone tell me how to send an email without using Smtp server in asp.net?