C# .NET - To send Email using asp.net - Asked By abinav shankar on 17-Jan-12 03:13 AM

Hi

I want to send mail using asp.net and when i use the host as smtp.gmail.com with the g mail user credentials it works fine but whrn i try to use the host as smtp.bizmail.yahoo.com with port as 465 i am getting the error Failed snding mail
since it is secured should i make any registry with the IIS and pl help me about the same

dipa ahuja replied to abinav shankar on 17-Jan-12 03:16 AM
Step 1 : Add this in Web.config
    
<system.net>
<mailSettings>
<smtp>
<network host="smtp.gmail.com"
  port="587"
  userName=""
  password=""
</smtp>
</mailSettings>
</system.net>
  
Step 2 : Mail Sending Code
 
using System.Net.Mail;
using System.Net;
 
protected void btnSent_Click(object sender, EventArgs e)
{
  string toEmailAddress = "xx@gmail.com";
  string GmailId = "abc@gmail.com";
  string bodyMsg = "helo its testing mail";
  string subject = "testing mail";
  MailMessage mail = new MailMessage();
  mail.To.Add(toEmailAddress);
 
  mail.From = new MailAddress(GmailId);
  mail.Subject = subject;
  mail.Body = bodyMsg;
  mail.IsBodyHtml = true;
 
    SmtpClient smtp = new SmtpClient();
  smtp.EnableSsl = true;
  smtp.Send(mail);
}
 
 
kalpana aparnathi replied to abinav shankar on 17-Jan-12 03:18 AM
hi,

First of all add below mentioned namespace in code behind of aspx page from which you want to send the mail.
using System.Net.Mail;

Now write this code in click event of button

protected void Button1_Click(object sender, EventArgs e)
{
  MailMessage mail = new MailMessage();
  mail.To.Add("jainamit.agra@gmail.com");
  mail.To.Add("amit_jain_online@yahoo.com");
  mail.From = new MailAddress("jainamit.agra@gmail.com");
  mail.Subject = "Email using Gmail";
 
  string Body = "Hi, this mail is to test sending mail"+
        "using Gmail in ASP.NET";
  mail.Body = Body;
 
  mail.IsBodyHtml = true;
  SmtpClient smtp = new SmtpClient();
  smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
  smtp.Credentials = new System.Net.NetworkCredential
     ("YourUserName@gmail.com","YourGmailPassword");
//Or your Smtp Email ID and Password
  smtp.EnableSsl = true;
  smtp.Send(mail);
}

You also need to enable POP by going to settings > Forwarding and POP in your gmail account

Change YourUserName@gmail.com to your gmail ID and YourGmailPassword to Your password for Gmail account and test the code.

If your are getting error mentioned below
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required."

than you need to check your Gmail username and password.
<system.net>
<defaultProxy>
<proxy proxyaddress="YourProxyIpAddress"/>
</defaultProxy>
</system.net>
 
 
If you are still having problems them try changing port number to 587
smtp.Host = "smtp.gmail.com,587";
 
If you still having problems then try changing code as mentioned below
 
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = False;
smtp.Credentials = new System.Net.NetworkCredential
("YourUserName@gmail.com","YourGmailPassword");
smtp.EnableSsl = true;
smtp.Send(mail);

hope will works,
smr replied to abinav shankar on 17-Jan-12 03:22 AM
hi

look at this

  smr replied to abinav shankar on 16-Jan-12 09:05 AM

hi

To configure SMTP configuration data for ASP.NET, you would add the following tags to your web.config file.
 
 <system.net>
    <mailSettings>
    <smtp from="abc@somedomain.com">
    <network host="somesmtpserverport="25userName="namepassword="passdefaultCredentials="true" />
    </smtp>
    </mailSettings>
 </system.net>


protected void btnSendmail_Click(object sender, EventArgs e)
  {
  // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
 
  // System.Net.Mail.SmtpClient is the alternate class for this in 2.0
 
  SmtpClient smtpClient = new SmtpClient();
  MailMessage message = new MailMessage();
 
  try
  {
    MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);
 
    // You can specify the host name or ipaddress of your server
 
    // Default in IIS will be localhost
 
    smtpClient.Host = "localhost";
 
    //Default port will be 25
 
    smtpClient.Port = 25;
 
    //From address will be given as a MailAddress Object
 
    message.From = fromAddress;
 
    // To address collection of MailAddress
 
    message.To.Add("admin1@yoursite.com");
    message.Subject = "Feedback";
 
    // CC and BCC optional
 
    // MailAddressCollection class is used to send the email to various users
 
    // You can specify Address as new MailAddress("admin1@yoursite.com")
 
    message.CC.Add("admin1@yoursite.com");
    message.CC.Add("admin2@yoursite.com");
 
    // You can specify Address directly as string
 
    message.Bcc.Add(new MailAddress("admin3@yoursite.com"));
    message.Bcc.Add(new MailAddress("admin4@yoursite.com"));
 
    //Body can be Html or text format
 
    //Specify true if it  is html message
 
    message.IsBodyHtml = false;
 
    // Message body content
 
    message.Body = txtMessage.Text;
    
    // Send SMTP mail
 
    smtpClient.Send(message);
 
    lblStatus.Text = "Email successfully sent.";
  }
  catch (Exception ex)
  {
    lblStatus.Text = "Send Email Failed." + ex.Message;
  }
  }


follow
http://www.dotnetcurry.com/ShowArticle.aspx?ID=65

follow http://www.eggheadcafe.com/community/csharp/2/10409425/how-to-send-email-using-aspnet.aspx
Jitendra Faye replied to abinav shankar on 17-Jan-12 03:25 AM

You can send email by using System.Net.Mail Namspace.

Use this code-
protected void SendEmail(object sender, EventArgs e)

{

SmtpClient


smtpClient = new SmtpClient();

MailMessage


message = new MailMessage();

try

{

// Prepare two email addresses

MailAddress


fromAddress = new MailAddress("FromAddress", "DisplayName");

MailAddress


toAddress = new MailAddress("ToAddress");

// Prepare the mail message

message.From = fromAddress;

message.To.Add(toAddress);

message.Subject =

"Testing!";

message.Body =

"This is the body of a sample message";

smtpClient.EnableSsl =

true;

smtpClient.Port = 587;

smtpClient.Host =

"smtp.gmail.com";

smtpClient.Credentials =

new System.Net.NetworkCredential("UserName", "Password");

smtpClient.Send(message);

}

catch


(Exception ex)

{

statusLabel.Text =

"Coudn't send the message!<br />" + ex.Message;

}

}


Try this and let me know.

Jitendra Faye replied to abinav shankar on 17-Jan-12 03:26 AM

You can send email by using System.Net.Mail Namspace.

Use this code-
protected void SendEmail(object sender, EventArgs e)

{

SmtpClient


smtpClient = new SmtpClient();

MailMessage


message = new MailMessage();

try

{

// Prepare two email addresses

MailAddress


fromAddress = new MailAddress("FromAddress", "DisplayName");

MailAddress


toAddress = new MailAddress("ToAddress");

// Prepare the mail message

message.From = fromAddress;

message.To.Add(toAddress);

message.Subject =

"Testing!";

message.Body =

"This is the body of a sample message";

smtpClient.EnableSsl =

true;

smtpClient.Port = 587;

smtpClient.Host =

"smtp.gmail.com";

smtpClient.Credentials =

new System.Net.NetworkCredential("UserName", "Password");

smtpClient.Send(message);

}

catch


(Exception ex)

{

statusLabel.Text =

"Coudn't send the message!<br />" + ex.Message;

}

}


Try this and let me know.

Jitendra Faye replied to abinav shankar on 17-Jan-12 03:26 AM

You can send email by using System.Net.Mail Namspace.

Use this code-
protected void SendEmail(object sender, EventArgs e)

{

SmtpClient


smtpClient = new SmtpClient();

MailMessage


message = new MailMessage();

try

{

// Prepare two email addresses

MailAddress


fromAddress = new MailAddress("FromAddress", "DisplayName");

MailAddress


toAddress = new MailAddress("ToAddress");

// Prepare the mail message

message.From = fromAddress;

message.To.Add(toAddress);

message.Subject =

"Testing!";

message.Body =

"This is the body of a sample message";

smtpClient.EnableSsl =

true;

smtpClient.Port = 587;

smtpClient.Host =

"smtp.gmail.com";

smtpClient.Credentials =

new System.Net.NetworkCredential("UserName", "Password");

smtpClient.Send(message);

}

catch


(Exception ex)

{

statusLabel.Text =

"Coudn't send the message!<br />" + ex.Message;

}

}


Try this and let me know.

Suchit shah replied to abinav shankar on 17-Jan-12 04:50 AM
If you want to send email with Attachement then do it like below code

using System.Net.Mail;

using System.Net;

 

protected void btnSent_Click(object sender, EventArgs e)

{

   string toEmailAddress="xx@gmail.com";

   string GmailId="abc@gmail.com";

   string password="";

   string bodyMsg="helo its testing mail";

 

   MailMessage mail = new MailMessage();

   mail.To.Add(toEmailAddress);

   mail.From = new MailAddress(GmailId);

   mail.Subject = txtSubject.Text;

   mail.Body = bodyMsg;

   mail.IsBodyHtml = true;

 

   //Attach file using FileUpload Control
and put the file in memory stream

 

   if (FileUpload1.HasFile)

   {

   mail.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream,
FileUpload1.FileName));

   }

 

   SmtpClient smtp = new SmtpClient("smtp.gmail.com");

   smtp.EnableSsl = true;

   smtp.UseDefaultCredentials = false;

   smtp.Credentials = new System.Net.NetworkCredential(GmailId, password);

   smtp.Send(mail);

}



Hope it will helps
Sri K replied to abinav shankar on 17-Jan-12 04:59 AM
string from = me@gmail.com; //Replace this with your own correct Gmail Address
  
string to = you@gmail.com //Replace this with the Email Address to whom you want to send the mail
  
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
 mail.To.Add(to);
 mail.From = new MailAddress(from, "One Ghost" , System.Text.Encoding.UTF8);
mail.Subject = "This is a test mail" ;
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true ;
mail.Priority = MailPriority.High;
  
SmtpClient client = new SmtpClient();
//Add the Creddentials- use your own email id and password
  
 client.Credentials = new System.Net.NetworkCredential(from, "Password");
  
client.Port = 587; // Gmail works on this port
client.Host = "smtp.gmail.com";
client.EnableSsl = true; //Gmail works on Server Secured Layer
   try
  {
    client.Send(mail);
  }
  catch (Exception ex)
  {
    Exception ex2 = ex;
    string errorMessage = string.Empty;
    while (ex2 != null)
    {
    errorMessage += ex2.ToString();
    ex2 = ex2.InnerException;
    }
   HttpContext.Current.Response.Write(errorMessage );
  } // end try