Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send email asp.net c#

Tags:

c#

email

asp.net

Is it possible to send email from my computer(localhost) using asp.net project in C#? Finally I am going to upload my project into webserver but I want to test it before uploading.

I've found ready source codes and tried run them in localhost, but neither of them work succesfully. For example this code:

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

    namespace sendEmail
    {
        public partial class _default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {

            }
            protected void Btn_SendMail_Click(object sender, EventArgs e)
            {
                MailMessage mailObj = new MailMessage(
                    txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
                SmtpClient SMTPServer = new SmtpClient("localhost");
                try
                {
                    SMTPServer.Send(mailObj);
                }
                catch (Exception ex)
                {
                    Label1.Text = ex.ToString();
                }
            }
        }
    }

So how to send email using asp.net C#? Should I setup some server configurations?

like image 547
Nurlan Avatar asked Jul 28 '12 19:07

Nurlan


2 Answers

Sending Email from Asp.Net:

    MailMessage objMail = new MailMessage("Sending From", "Sending To","Email Subject", "Email Body");
    NetworkCredential objNC = new NetworkCredential("Sender Email","Sender Password");
    SmtpClient objsmtp = new SmtpClient("smtp.live.com", 587); // for hotmail
    objsmtp.EnableSsl = true;
    objsmtp.Credentials = objNC;
    objsmtp.Send(objMail);
like image 121
Waqar Janjua Avatar answered Oct 05 '22 08:10

Waqar Janjua


if you have a gmail account you can use google smtp to send an email

smtpClient.UseDefaultCredentials = false;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new NetworkCredential(username,passwordd);
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
like image 45
COLD TOLD Avatar answered Oct 05 '22 07:10

COLD TOLD