ASP.Net Email Sending

This is very common usage that whenever we are working with webform or winform sometimes , instead most of the times we need to send email
To send email there is simple logic you just need Two very crucial things one the email server somtimes called host , and its port throuth which you want to send email
Here is the detail of some Hosts and there ports setting
For sending Email
Gmail : 
Incoming Mail Server (POP3) – pop.gmail.com (SSL enabled, port 995)
Outgoing Mail Server (SMTP) – smtp.gmail.com (TLS enabled, port 587)
MSN :
Incoming Mail Server (POP3) – pop3.email.msn.com (port 110, using Secure Password Authentication – SPA)
MSN Outgoing Mail Server – smtp.email.msn.com
Yahoo:
Yahoo Incoming Mail Server (POP3) – pop.mail.yahoo.com (SSL enabled, port 465)
Yahoo Outgoing Mail Server (SMTP) – smtp.mail.yahoo.com (SSL enabled, port 995)


And the credential we have to provide the sender email and sender password .
Below is the working sample , Make a function in you app_code Class and call it where u need it


METHOD 1 :

public static bool SendEmail(string ToAddress, string subject, string body,string emailaddress)
    {
        string setupemail = “”;
        string setuppassword = “”;
        string setupport = “”;


        SqlDataReader dr = DataLayer.DBOperation.ExecuteReader(“select * from emailsetup”);
        if (dr.Read())
        {
            setupemail = dr[“emailaddress”].ToString();
            setuppassword = dr[“password”].ToString();
            setupport = dr[“port”].ToString();
        }
        dr.Close();
        System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
        mail.From = “\”Innovation in Diabetes\” < “+emailaddress+”>”;
        mail.To = ToAddress;
        mail.Headers.Add(“Reply-To”, emailaddress);
        mail.Body = body;
        mail.Subject = subject;
        mail.Bcc = setupemail;
        mail.BodyFormat = MailFormat.Html;
        SmtpMail.SmtpServer = “smtp.gmail.com”;
        // – smtp.gmail.com use smtp authentication
        mail.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”, “1”);
        mail.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/sendusername”, “”+setupemail+””);
        mail.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/sendpassword”, “”+setuppassword+””);
        // – smtp.gmail.com use port 465 or 587
        mail.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/smtpserverport”, “”+setupport+””);
        // – smtp.gmail.com use STARTTLS (some call this SSL)
        mail.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/smtpusessl”, “true”);
        // try to send Mail
        try
        {
            SmtpMail.Send(mail);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }


METHOD 2:


Public Sub SendEmail()

‘Hotmail/Live Host & Port smtp.live.com (Port 25)
        ‘Hotmail/Live Host & Port pop3.live.com (Port 995)


        ‘Yahoo pop.mail.yahoo.com (port 110)
        ‘Yahoo smtp.mail.yahoo.com (port 25)
        ‘Gmail smtp.gmail.com(port 587)
        Try


            ‘    Dim client As New SmtpClient
            ‘    client.Credentials = New System.Net.NetworkCredential(“ur_own_ftp_email account”, “password of that account”)
            ‘    client.EnableSsl = True
            ‘    client.Host = “smtp.gmail.com”


            ‘    client.Port = 485


            ‘    client.UseDefaultCredentials = False
            ‘    client.Send(mailMsg)
            ‘    ‘Attachments.Items.Clear()
            ‘    ‘Attachments.Items.Add(“(No Attachments)”)


 Dim mailClient As New SmtpClient(“smtp.gmail.com”, 587)


            mailClient.EnableSsl = True
‘Dim cred As New NetworkCredential(“your email”, “ur pwd goes here”)
Dim cred As New NetworkCredential(From.Text, txtPassword.Text)
            mailClient.Credentials = cred
            mailClient.Send(From.Text, ToAddress.Text, Subject.Text, Body.Text)


            MessageBox.Show(“Your email has been successfully sent!”, “Email Send Status”, MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch exp As Exception
            MessageBox.Show(“The following problem occurred when attempting to ” + exp.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *