Friday 6 April 2012

Simple Mail Function in C# using SMTP

Open a new Windows Application .... named it mailtest

Design Form1like below,

Double click on button one "Send"...

Type the Below Code...



System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
            mm.To.Add(new              System.Net.Mail.MailAddress("r.shanmugam89@gmail.com","shanmugam"));
           
            mm.From = new System.Net.Mail.MailAddress("Simple@mailfunction.com");
            mm.Sender = new System.Net.Mail.MailAddress("username""password");
            mm.Subject = "This is Test Email";
            mm.Body = "<h3>This is Testing SMTP Mail Send By Me</h3>";
            mm.IsBodyHtml = true;
            mm.Priority = System.Net.Mail.MailPriority.High; // Set Priority to sending mail
            System.Net.Mail.SmtpClient smtClient = new System.Net.Mail.SmtpClient("hostname",port number);            
            smtCliend.Credentials = new NetworkCredential( "username""password" );
            smtClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
 smtClient.Send(mm);


Here, Username must be your email id, password must be that email password...
          Host Name is from which server you are sending mail,
              eg, gmail, yahoo... or from any of mail servers...
Gmail SMTP Host name is smtp.gmail.com...
          Default port number 25..


Double Click on button2 "Exit" and the code below...


Application.Exit();


Source Code:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;

namespace mailtest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
            mm.To.Add(new              System.Net.Mail.MailAddress("shanmugamXXXX@gmail.com","shanmugam"));
           
            mm.From = new System.Net.Mail.MailAddress("Simple@mailfunction.com");
            mm.Sender = new System.Net.Mail.MailAddress("username", "password");
            mm.Subject = "This is Test Email";
            mm.Body = "<h3>This is Testing SMTP Mail Send By Me</h3>";
            mm.IsBodyHtml = true;
            mm.Priority = System.Net.Mail.MailPriority.High; // Set Priority to sending mail
            System.Net.Mail.SmtpClient smtClient = new System.Net.Mail.SmtpClient("hostname",port number);
            //smtCliend.Host = "mail.shanmugam.com";
            //smtCliend.Port = 25;    // smtp port no 
            //smtCliend.Credentials = CredentialCache.DefaultNetworkCredentials;
            smtCliend.Credentials = new NetworkCredential( "username""password" );
            smtClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            try
            {
                smtClient.Send(mm);
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                MessageBox.Show(ex.ToString());
            }
           
        }
    }
}





4 comments:

  1. it is very nice and helpful......

    ReplyDelete
  2. Thanks for your reply...
    if u have any Doubt in C# Dot .net Kindly Contact me...
    if i know that Surely i will help U...

    ReplyDelete
  3. public static string SendEmail(string toAddress, string subject, string body)
    {

    string result = "message sent seccesfully";
    string senderID = "akyuzz.burak@gmail.com";
    const string senderPassword = "727652burak";


    try
    {
    MailMessage mail = new MailMessage();
    mail.Subject = subject; ;
    mail.Body = body;


    mail.From = new System.Net.Mail.MailAddress(senderID);
    mail.IsBodyHtml = false;
    mail.BodyEncoding = System.Text.Encoding.Unicode;
    mail.SubjectEncoding = System.Text.Encoding.Unicode;
    mail.To.Add(toAddress);

    NetworkCredential cred = new NetworkCredential(senderID, senderPassword);
    SmtpClient smtp = new SmtpClient("smtp@gmail.com");
    smtp.UseDefaultCredentials = false;
    smtp.EnableSsl = true;
    smtp.Credentials = cred;
    smtp.Port = 587;

    smtp.Send(mail);


    }
    catch (Exception ex)
    {

    Console.WriteLine("{0}", ex.Message);


    }
    return result;
    }

    when i try to run this method it gives me this message pls help
    "failure sending mail"
    thanks

    ReplyDelete
  4. hi.
    smtp for gmail is smtp.gmail.com. i made a mistake.
    sorry.

    ReplyDelete