Elena
Elena

Reputation: 839

sending email fails in C# 3.5

While sending email, I get the following error:

The device is not ready at System.Net.Mail.SmtpClient.Send(MailMessage message).

The code is :

MailMessage mailMessage = new MailMessage(senderEmail, cleanRecipients)
{
    Subject = string.empty,
    Body = string.empty,
    IsBodyHtml = false
};
SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(mailMessage);

Upvotes: 1

Views: 2050

Answers (2)

Soner Gönül
Soner Gönül

Reputation: 98740

This code maybe will help!

string from = [email protected]; //Replace this with your own correct Gmail Address

string to = [email protected] //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 

Upvotes: 3

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

In order to send an email you need an SMTP server, so make sure you have specified an SMTP server in the config file.

<system.net>
    <mailSettings>
      <smtp from="[email protected]">
        <network host="mail.mydomain.com" password="secret" port="25" userName="[email protected]" />
      </smtp>
    </mailSettings>
</system.net>

Upvotes: 7

Related Questions