How to set username and password for SmtpClient object in .NET?

前端 未结 4 1889
抹茶落季
抹茶落季 2020-12-01 00:07

I see different versions of the constructor, one uses info from web.config, one specifies the host, and one the host and port. But how do I set the username and password to

4条回答
  •  [愿得一人]
    2020-12-01 00:23

    SmtpClient MyMail = new SmtpClient();
    MailMessage MyMsg = new MailMessage();
    MyMail.Host = "mail.eraygan.com";
    MyMsg.Priority = MailPriority.High;
    MyMsg.To.Add(new MailAddress(Mail));
    MyMsg.Subject = Subject;
    MyMsg.SubjectEncoding = Encoding.UTF8;
    MyMsg.IsBodyHtml = true;
    MyMsg.From = new MailAddress("username", "displayname");
    MyMsg.BodyEncoding = Encoding.UTF8;
    MyMsg.Body = Body;
    MyMail.UseDefaultCredentials = false;
    NetworkCredential MyCredentials = new NetworkCredential("username", "password");
    MyMail.Credentials = MyCredentials;
    MyMail.Send(MyMsg);
    

提交回复
热议问题