C# .NET - Failed sending mail - Asked By abinav shankar on 17-Jan-12 02:04 AM

Hi

I am trying to send mail using asp.net and I used the following code and getting the Exception, Failed sending mail pl correct where i went wrong
Thanks

<system.net>
  <mailSettings>
    <smtp from="abinav@ymail.com">
    <network host="smtp.bizmail.yahoo.com" port="465" userName="abinav@ymail.com" password="Mypwd" defaultCredentials="true" />
    </smtp>
  </mailSettings>
  </system.net>

public void sendMail()
  {
    //SmtpClient smtpClient = new SmtpClient();
    //MailMessage message = new MailMessage();
    try
    {
      string toEmailAddress = "mahesh.krishnan@gmail.com";
      string ymailId = "abinav@Ymail.com";
      string bodyMsg = "hello its testing mail";
      string subject = "testing mail";
      MailMessage mail = new MailMessage();
      mail.To.Add(toEmailAddress);
      mail.From = new MailAddress(ymailId);
      mail.Subject = subject;
      mail.Body = bodyMsg;
      mail.IsBodyHtml = true;
      SmtpClient smtp = new SmtpClient();
      //smtp.Timeout = 3000;
      smtp.EnableSsl = true;
      smtp.Send(mail);
  
    }
    catch (Exception ex)
    {
      throw(ex);
    }
}

Jitendra Faye replied to abinav shankar on 17-Jan-12 02:06 AM

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

Note -  Check your SMTP setting first, is it correct or not.


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 na dlet me know.

Suchit shah replied to abinav shankar on 17-Jan-12 02:09 AM
To send an email follow below steps for that

Step 1 : Add Below Namespace
 
using System.Net;

using System.Net.Mail;

 
Step 2 :
Write code on your button click event
 
protected void btnSend_Click(object sender, EventArgs e)
{
   SmtpClient client = new SmtpClient();
   MailMessage message = new MailMessage();
   client.Port = 587;
   client.Host = "smtp.gmail.com";
   client.UseDefaultCredentials = true;
   client.Credentials = new System.Net.NetworkCredential(txtemail.Text ,txtpass.Text );
   client.EnableSsl = true;
   try
   {
   MailAddress SendTo = new MailAddress(txtTo.Text);
   MailAddress SendFrom = new MailAddress(txtFrom.Text);
   message = new MailMessage(SendFrom, SendTo);
   message.Body = txtMessage.Text;
   message.Subject = txtSub.Text;
   client.Send(message);

    txtStatus.Text = "Your email has been sent";
    
  }
   catch (Exception ex)
   {
   txtStatus.Text = ex.ToString();
   }

}

Reena Jain replied to abinav shankar on 17-Jan-12 02:23 AM

hi,


here is the code to sent mail

MailMessage message = new MailMessage();
 
message.From = new MailAddress("sender@yahoo.com");
 
message.To.Add(new MailAddress("recipient1@yahoo.com"));
 
message.To.Add(new MailAddress("recipient2@yahoo.com"));
 
message.CC.Add(new MailAddress("carboncopy@foo.bar.com"));
 
message.Subject = "This is my subject";
 
message.Body = "This is the content";
 
SmtpClient client = new SmtpClient();
 
client.Send(message);

System.Net.Mail reads SMTP configuration data out of the standard .NET configuration system (so for ASP.NET applications you’d configure this in your application’s web.config file).  Here is an example of how to configure it:
<system.net>
 
  <mailSettings>
 
  <smtp from="test@foo.com">
 
  <network host="smtpserver1" port="25" userName="username" password="secret" defaultCredentials="true" />
 
  </smtp>
 
  </mailSettings>
 
  </system.net>
dipa ahuja replied to abinav shankar on 17-Jan-12 02:41 AM
void email()
{
  string toEmailAddress = "xxx@gmail.com";
  string YahooId = "xx@yahoo.com";
  string password = "xxxx";
  string bodyMsg = "Helo its testing mail!";
 
  MailMessage mail = new MailMessage();
  mail.To.Add(toEmailAddress);
 
  mail.From = new MailAddress(YahooId);
  mail.Subject = "testing";
  mail.Body = bodyMsg;
  mail.IsBodyHtml = true;
 
  SmtpClient smtp = new SmtpClient("smtp.mail.yahoo.com");
  smtp.Port = 25;
  smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
  smtp.UseDefaultCredentials = false;
  smtp.Credentials = new System.Net.NetworkCredential(YahooId, password);
  smtp.Send(mail);
}