Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Can anybody tell me what wrong in my code.When I tried to send them emails, I got the following error:
C#
Exception Details: System.Net.Mail.SmtpException: The operation has timed out.

Here is my code.
C#
public  bool SendEmail()
        {            
            List<dbomjobqueue> pendingJobQuoueList = getPendingJobQuoue();
            
                foreach (var list in pendingJobQuoueList)
                {
                    if (list.fromStatus == 0)
                    {
                        ID = list.ID;
                        toEmail = list.toEmail;
                        fromPath = list.fromPath;
                        fromStatus = list.fromStatus;
                        subject = "";
                        //Creating an instance of the mail object
                        MailMessage mailMsg = new MailMessage();
                        MailAddress mailAddress = null;
                        try
                        {
                            //Set the content
                            mailMsg.IsBodyHtml = true;  // Set the format of the mail message body as HTML
                            mailMsg.To.Add(toEmail);    // Set the recepient address of the mail message                     
                            mailAddress = new MailAddress("muna.angul10@gmail.com");
                            mailMsg.From = mailAddress; // Set the sender address of the mail message
                            mailMsg.Subject = subject;  // Set the subject of the mail message
                            mailMsg.Body = "";  // Set the body of the mail message
                            mailMsg.Priority = MailPriority.High;   // Set the priority of the mail message to high
                            mailMsg.Attachments.Add(new Attachment(fromPath));  //create and add the attachment
                            SmtpClient smtpClient = new SmtpClient();   //smtp server address
                            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
                            smtpClient.Credentials = credentials;
                            //smtpClient.Host = "localhost"or"192.168.30.137";
                            smtpClient.EnableSsl = true;
                            //smtpClient.UseDefaultCredentials = false;
                            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                            if (ID != 0)
                            {
                                fromStatus = 1; //Sending In Progress 
                                UpdateDbStatus();
                            }
                            smtpClient.Send(mailMsg);                            
                            eventLogDBOMJobQueue.WriteEntry("Mail Send Successfully");
                            //fromStatus = 2;//Email send successfully(Done)
                            //UpdateDbStatus();
                        }
                        catch (Exception ex)
                        {                           
                            errorDescription = ex.Message;
                            //fromStatus = 3;//Error
                            //UpdateDbStatus();
                            return false;
                        }
                        finally
                        {
                            mailMsg = null;
                            mailAddress = null;
                        }
                 }
            }
            return true; 
}

And here is my configration.
XML
<system.net>
    <mailSettings>
      <smtp>
        <network host="smtpcorp.com" port="465" userName="username" password="password" defaultCredentials="true" />
        <!--<network host="smtp.gmail.com" port="465" />-->
      </smtp>
    </mailSettings>
  </system.net>

Please help me.

Thanks in Advance.
Posted
Updated 6-Jun-12 18:33pm
v4

Hi,

First thing I can see is you've specified credentials in your mailSettings element of your config but you've set use default credentials to true. You need to set this to false otherwise it will use the local security principle in which the service is running.

Your host may also be invalid you can test this using telnet. There's an excellent article which explains how to do this here:

How To Test SMTP Services Manually in Windows Server 2003[^]
 
Share this answer
 
Comments
Saroj Kumar Sahu 7-Jun-12 1:54am    
hello sir,i hav tried already by using default credentials to false and also given my username and password but same result.
Are you sure that the following configuration is correct?
host="smtpcorp.com" port="465"

Make sure that the port is open. Further, do check there is no firewall issues. You can talk to your IT admin guys about it and make sure that the configuration used is ok.

Also, have a look at this blog post: MSDN Blog: System.Net.Mail with SSL to authenticate against port 465[^]
 
Share this answer
 
Comments
amshith 26-Nov-13 6:34am    
Actually i'm trying to send an email using WindowsService using c#, but i'm not able to send the mail through the service even though i tried with the previllege to localsytem, network var client = new SmtpClient("smtp.gmail.com",587)
{
EnableSsl = true,
Credentials = new NetworkCredential("maath@gmail.com", "power")

};

client.Send("mam@gmail.com", "gish@gmail.com","Test", "Test");
there is some problem in host and port.
it worked when i try with

Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,



thank you very much frnds.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900