Popular Posts

Monday, March 7, 2011

Code to send email through C# .net using Gmail server(complete detailed description)


PROCEDURE
           How to obtain details of the server u want to send an email through
on command prompt follow the following steps

1.c:\nslookup
2.>set type=mx
3.>enter the server name (example : google.com)
4.Press enter

COMPLETE PROCEDURE:

(Follow these steps step-wise in your command prompt)

c:\>nslookup
Default Server:  UnKnown
Address:  218.248.241.4
> set type=mx
> google.com
Server:  UnKnown
Address:  218.248.241.4

Non-authoritative answer:
google.com      MX preference = 300, mail exchanger = google.com.s9b1.psmtp.com
google.com      MX preference = 400, mail exchanger = google.com.s9b2.psmtp.com
google.com      MX preference = 100, mail exchanger = google.com.s9a1.psmtp.com
google.com      MX preference = 200, mail exchanger = google.com.s9a2.psmtp.com

google.com      nameserver = ns2.google.com
google.com      nameserver = ns3.google.com
google.com      nameserver = ns4.google.com
google.com      nameserver = ns1.google.com
google.com.s9a1.psmtp.com       internet address = 74.125.148.10
google.com.s9a2.psmtp.com       internet address = 74.125.148.11
google.com.s9b1.psmtp.com       internet address = 74.125.148.13
google.com.s9b2.psmtp.com       internet address = 74.125.148.14
ns1.google.com  internet address = 216.239.32.10
ns2.google.com  internet address = 216.239.34.10
ns3.google.com  internet address = 216.239.36.10
ns4.google.com  internet address = 216.239.38.10
>



UR C# .NET CODE:
DESIGN


FORM :










CODE:
(copy this code in the forms code)
(please verify the namespace)


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

namespace Mailing
{
    public partial class Mail : Form
    {
        public Mail()
        {
            InitializeComponent();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage message = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text,    txtBody.Text);
                message.Priority = MailPriority.High;
                message.IsBodyHtml = true;
                SmtpClient emailclient = new SmtpClient();
emailclient.Credentials = new    System.Net.NetworkCredential("your email     address","youremailpassword");
                emailclient.Port = 587;                                   //port number for gmail
                emailclient.Host = "smtp.gmail.com";              //gmail host SMTP server
                emailclient.EnableSsl = true;                          //for security purpose
              
                try
                {
                    emailclient.Send(message);
                    MessageBox.Show("Message Sent");
                }
                catch (Exception ae)
                {
                    MessageBox.Show(ae.ToString());
                }


            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}

No comments:

Post a Comment