Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating window application. When I am sending a mail, at that time error occur
Cannot sent Message : Failure sending mail.
Can you tell me what is the problem in my code.
C#
try
{
                MailMessage mail = new MailMessage(txtfrom.Text, txtto.Text);
                mail.From = new MailAddress(txtfrom.Text);
                mail.To.Add(txtto.Text);
                mail.Subject = txtsubject.Text;
                mail.Body = txtmess.Text;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "mail.mycompanyid.net";
                smtp.Port = 25;
                NetworkCredential NetCrd = new NetworkCredential();
                NetCrd.Domain = "ip";
                NetCrd.UserName = "myid";
                NetCrd.Password = "mypass";
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = NetCrd;
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.Send(mail);
                MessageBox.Show("Your Message Sent Successfully");
            }
            catch (Exception ee)
            {
                MessageBox.Show("Cannot send message: " + ee.Message);
            }
Posted
Updated 1-Apr-11 0:39am
v2

1 solution

[Content related to ASP.NET removed]

Have a look at this Tip for winforms:
Sending an Email in C# with or without attachments: generic routine.[^]
 
Share this answer
 
v2
Comments
ChiragBMakwana 1-Apr-11 6:42am    
I am creating Window Application in C#.net.. not in asp.net
So i need code for window application
Sandeep Mewara 1-Apr-11 6:56am    
Answer updated.
ChiragBMakwana 1-Apr-11 7:02am    
sorry open but nothing on this page... only show the clear page/
white page.
ChiragBMakwana 1-Apr-11 7:03am    
nothing display in this link only open white page.
Henry Minute 1-Apr-11 7:04am    
The link works fine for me.

Try it again.

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