Click here to Skip to main content
15,891,868 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
C#
My code is like this its working fine when i run the solution in local system but am not not able to send mails when i published it to server what might be wrong??


 public static bool SendMail(string MailTo, string MailCC, string MailBCC, string MailSubject, string MailBody)
        {
            bool _Success = false;
            try
            {

                SmtpClient smtp = new SmtpClient();
                MailMessage message = new MailMessage();
                message.IsBodyHtml = true;
                message.From = new MailAddress("emailid@gmail.com");
                message.To.Add(MailTo);
                message.CC.Add(new MailAddress(MailCC));
                message.Subject = MailSubject;
                message.Body = MailBody;
                smtp.EnableSsl = true;

                smtp.Send(message);
                _Success = true;
                                
            }
            catch (Exception ex)
            {
                ExceptionLogging("Common", "Common.SendMail()", ex.ToString());
                _Success=false;
            }
            return _Success;
        }


web config file is like this

 <system.net>
    <mailSettings>
      <smtp>
        <network host="smtp.gmail.com" port="587" userName="emailid@gmail.com" password="pwdxyz"/>
      </smtp>
    </mailSettings>    
  </system.net>
Posted

Authorization.

You almost certainly need authorization: Sending an Email in C# with or without attachments: generic routine.[^] includes it.
 
Share this answer
 
C#
i was using smptclient instead of smtpserver final got it with little tweaks.. thanku  guys for giving suggestions :)


const string SERVER = "serverHosting name";
                System.Web.Mail.MailMessage oMail = new System.Web.Mail.MailMessage();
                oMail.From = "emailname@yourdomain";
                oMail.To = MailTo;
                oMail.Cc = MailCC;
                oMail.Subject = MailSubject;
                oMail.BodyFormat = MailFormat.Html; // enumeration
                oMail.Priority = MailPriority.High; // enumeration

                oMail.Body = MailBody;
                SmtpMail.SmtpServer = SERVER;
                SmtpMail.Send(oMail);
                oMail = null; // free up resources
 
Share this answer
 

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