-2

I`m trying to get send confirmation email with link (code) inside for new registered users to confirm their email, this is a code im using, does anyone see whats wrong with it ? I use credentials from hosting, they are working normal on other methods.

public class Cls_SendMail
{
    public Cls_SendMail(string mailid, string message, string subject)
    {
        try
        {
            MailMessage objmail = new MailMessage();
            objmail.To.Add(mailid);
            objmail.From = new MailAddress(ConfigurationManager.AppSettings["FROMEMAIL"].ToString());
            objmail.Subject = subject;
            string Body;
            Body = message;
            objmail.Body = Body;
            objmail.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient();
            smtp.UseDefaultCredentials = false;
            smtp.EnableSsl = false;
            smtp.Host = "mail.host.com";
            smtp.Port = 587; // 465; //Gmail works on this port
            smtp.Timeout = 100000;
            smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]);
            smtp.Send(objmail);
        }
        catch (Exception)
        {

        }
    }
}


 var Token = (from p in context.webpages_Memberships where p.UserId == model.Id select p.ConfirmationToken).FirstOrDefault();
            var resetLink = "<a href='" + Url.Action("ConfirmAccount", "SignUp", new { un = model.Id, rt = Token }, "http") + "' style='color: #28BDF2;'>"+confirm+"</a>";

            string csspath = "http://localhost:50467/EmailHTML/stylesheets/email.css";
            string readFile = reader.ReadToEnd();
            string myString = "";
            UsersContext dbcontext = new UsersContext();
            var username = string.Empty;
            var user = dbcontext.UserProfiles.Find(model.Id);
            if (user!=null)
            {
                username = user.UserName;
            }
            myString = readFile;
            myString = myString.Replace("%{#{CssPath}#}%", csspath);
            myString = myString.Replace("%{#{Name}#}%", model.FirstName);
            myString = myString.Replace("%{#{UserName}#}%", username);
            myString = myString.Replace("%{#{ConfirmLink}#}%", resetLink);
            string subject = acountconfirm;
            string body = myString;
            try
            {
                //call for send mail
                Cls_SendMail mail = new Cls_SendMail(model.Email, body, subject);
                TempData["Message"] = Resource.mailhassent;
            }
            catch (Exception )
            {
                TempData["Message"] = Resource.Erroroccurredwhilesendingemail;
            }
Faisal
  • 23
  • 3

1 Answers1

0

So after hours and hours of thinking, ive realized i havent setup credentials in web.config

Solution: Check webconfig.