Click here to Skip to main content
16,012,061 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

I am using SMTP to send a mail on button click Event .

MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(Convert.ToString(ConfigurationSettings.AppSettings["UpdateMailFrm"]));
            mailMessage.To.Add(Convert.ToString(ConfigurationSettings.AppSettings["UpdateMailTo"]));
            
            mailMessage.IsBodyHtml = true;
            mailMessage.Subject = " HP Stacking Profile Update ";
            mailMessage.Body = mailtxt;
            string host = Convert.ToString(ConfigurationSettings.AppSettings["BytechSmtpServer"]);
            SmtpClient SmtpMail = new SmtpClient(host);                  
            SmtpMail.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
            SmtpMail.Send(mailMessage);

            string nowdate = System.DateTime.Now.Date.ToString("dd_MM_yyyy");
            string path = Server.MapPath("~/") + "LogFile/" + nowdate + ".txt";
            string text1 = " Page :Mail Date and Time :" + System.DateTime.Now.ToString("dd-MM-yyyy hh:ss:mm");
            text1 = text1 + " Message :" + "Message successfully Sent\r\n";
            File.AppendAllText(path, text1);


The Above Code is which i have used in My project . Its not throwing any Error but its not sending the Mail also. I have declared the From & To mail's and Host(BytechSmtpServer) in my Web.Config File .

So can any one suggest me to fix this issues .
Posted

1 solution

use this code

MailMessage oMsg = new MailMessage();
               oMsg.From = new MailAddress("xxx@gmail.com", "xxx");
               oMsg.To.Add(new MailAddress("yyy@gmail.com", "xxx"));
               oMsg.Subject = "Packet Parsing Problem";
               oMsg.Body = " Problem Occuread test mail";
               oMsg.SubjectEncoding = System.Text.Encoding.UTF8;
               oMsg.BodyEncoding = System.Text.Encoding.UTF8;
               oMsg.IsBodyHtml = false;
               oMsg.Priority = MailPriority.High;

               SmtpClient oSmtp = new SmtpClient("smtp.gmail.com", 587);
               oSmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
               oSmtp.EnableSsl = true;

               NetworkCredential oCredential = new NetworkCredential("xxx@gmail.com", "123456aA");
               oSmtp.UseDefaultCredentials = false;
               oSmtp.Credentials = oCredential;
               oSmtp.Send(oMsg);



Thanks
 
Share this answer
 
Comments
sahabiswarup 19-Jan-12 5:27am    
Good Job Rajesh
Rajesh Anuhya 19-Jan-12 5:27am    
Thank you
SARAVANAKUMAR.M 19-Jan-12 5:46am    
Hi Rajesh,

Thanks for your Information , If i use the Host as Gmail its working fine but i am trying to use my local Host as "secure.emailsrvr.com" at that time its not sending the mail.
Rajesh Anuhya 19-Jan-12 5:48am    
may be your sender mail does't have the outgoing permission, contact your Administrator for SMTP out permission for your sender mail ID.

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