Sending email using C#

//Let us look at how to send email with attachment using C#.

//To try this, you should include the following code into your application.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Mail;

public partial class Views_WebPages_Default : System.Web.UI.Page
{
MailMessage objEmail = new MailMessage();

public string strName, strEmail, strMsg, Mailcontent = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{

strName = txtName.Text;
strEmail = txtEmail.Text;
strMsg = txtMsg.Text;


Mailcontent = "";
Mailcontent = Mailcontent + "";
Mailcontent = Mailcontent + "";
Mailcontent = Mailcontent + "";
Mailcontent = Mailcontent + "

Mailcontent = Mailcontent + "";
Mailcontent = Mailcontent + "";
Mailcontent = Mailcontent + "";
Mailcontent = Mailcontent + "";
Mailcontent = Mailcontent + "

Contact Us Form

First Name : " + strName + "

Email : " + strEmail + "

Message : " + strMsg + "

";
Mailcontent = Mailcontent + "
";

objEmail.From = strEmail;
objEmail.To = "To email id";
objEmail.Bcc = "Another To email id(optional)";
objEmail.Subject = "Subject Name";
objEmail.Body = Mailcontent;
objEmail.BodyFormat = System.Web.Mail.MailFormat.Html;
objEmail.Priority = System.Web.Mail.MailPriority.High;

objEmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication

objEmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "Your To email id"); //set your username here

objEmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "To email id's pwd"); //set your password here

SmtpMail.SmtpServer = "Here your SMTP Server";
System.Web.Mail.SmtpMail.Send(objEmail);

Lbl_Status.Text = "Your Message has been Sent Successfully";
}
catch (Exception ex)
{
Lbl_Status.Text = ex.ToString();
}
}


Comments

Author: Santhi11 Nov 2008 Member Level: Gold   Points : 2

using System;
using System.IO;
using System.Text;
using System.Threading;
using Jscape.Email;

namespace Smtpsample {

public class Smtpsample {

public Smtp mySmtp = null;

public SmtpSample(string hostname){
mySmtp = new Smtp(hostname);
mySmtp.Debug = false;

mySmtp.ConnectedEvent += new Smtp.ConnectedEventHandler(OnConnected);
mySmtp.DisconnectedEvent += new Smtp.DisconnectedEventHandler(OnDisconnected);
mySmtp.DataReceivedEvent += new Smtp.DataReceivedEventHandler(OnDataReceived);
mySmtp.CommandSentEvent += new Smtp.CommandSentEventHandler(OnCommandSent);

// create email message
EmailMessage message = new EmailMessage();
message.To = "xyz@mail.com";
message.Cc = "abc@mail.com";
message.From = "Sample@mail.com";
message.SetSubject("Creating mail message", "Hi5");

message.SetBody("Create mail.");

// add attachment
Attachment att = new Attachment(@"D:\mail\message\xyz.doc");
message.AddAttachment(att);
// Connect to smtp server
mySmtp.Connect();
// Send email message;
mySmtp.Send(message);
// your server may require a slight delay in order to respond
Thread.Sleep(100);
// Disconnect from smtp server
mySmtp.Disconnect();
}


public static void Main() {

// default mail server
string hostname = "mail.ourserver.com";

// prompt for mail server
Console.WriteLine("Smtp server: "+hostname);
if ((hostname = Console.ReadLine()) != "") {
SmtpSample smtpexample = new SmtpSample(hostname);
}
}


public void OnConnected(object sender, SmtpConnectedEventArgs e) {
Console.WriteLine("Connected to {0}", e.Host);
}
public void OnDisconnected(object sender, SmtpDisconnectedEventArgs e) {
if (mySmtp.IsConnected()) {
mySmtp.Disconnect();
}
Console.WriteLine("Disconnected.");
}
public void OnDataReceived(object sender, SmtpDataReceivedEventArgs e) {
Console.WriteLine("Response: "+e.Response);
}
public void OnCommandSent(object sender, SmtpCommandSentEventArgs e) {
Console.WriteLine("Command: "+e.Command);
}
}
}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: