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

前端 未结 4 1902
抹茶落季
抹茶落季 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条回答
  •  猫巷女王i
    2020-12-01 00:19

    Since not all of my clients use authenticated SMTP accounts, I resorted to using the SMTP account only if app key values are supplied in web.config file.

    Here is the VB code:

    sSMTPUser = ConfigurationManager.AppSettings("SMTPUser")
    sSMTPPassword = ConfigurationManager.AppSettings("SMTPPassword")
    
    If sSMTPUser.Trim.Length > 0 AndAlso sSMTPPassword.Trim.Length > 0 Then
        NetClient.Credentials = New System.Net.NetworkCredential(sSMTPUser, sSMTPPassword)
    
        sUsingCredentialMesg = "(Using Authenticated Account) " 'used for logging purposes
    End If
    
    NetClient.Send(Message)
    

提交回复
热议问题