【问题标题】:How to send smtp email without entering credentials (which often change)?如何在不输入凭据(经常更改)的情况下发送 smtp 电子邮件?
【发布时间】:2020-02-20 23:38:08
【问题描述】:

我编写了下面的代码,效果很好。该代码将按计划(每天和/或每周)运行以处理文件并在处理结束时发送一封电子邮件,并附上结果。 但是,此代码需要网络凭据(用户和密码)。 但是密码经常更改。那么如何编写代码来发送电子邮件,而无需在密码更改时进入并更改 mmy 代码?

  Dim SmtpServer As New SmtpClient()
  SmtpServer.EnableSsl = True
  Dim mail As New MailMessage()
  SmtpServer.Credentials = New Net.NetworkCredential("dw@my-org.org", "mypassword")
  SmtpServer.Port = 587
  SmtpServer.Host = "smtp.office365.com"
  mail = New MailMessage()
  mail.From = New MailAddress("dw@my-org.org")
  mail.To.Add("dw@my-org.org")
  mail.Subject = "Results of automatic Import of Time Card Punches to Attendance Application"
  mail.Body = "Attached are the CSV logs with the results of the automatic Import of Time Card Punches to Attendance Application. The import file contains logs of any new employee records createds or any changes to any existing employees. The import issues file will not contain any results unless there were errors during import."
  mail.Attachments.Add(New Attachment(LogFilename.ToString))
  mail.Attachments.Add(New Attachment(LogimportIssues.ToString))
  SmtpServer.Send(mail)

【问题讨论】:

  • 您必须将用户名和密码放入配置文件或数据库中。
  • 但是每次密码更改时我仍然需要不断更新它,对吧?
  • 是的,尤其是如果它是 office365 电子邮件。我想你必须问问自己,我更改电子邮件的最简单方法是什么。

标签: vb.net email smtp


【解决方案1】:

一种技术可能是; 您可以在路径中创建一个文本文件,您可以从此文本文件(或任何扩展名)中读取第一行女巫是您的密码 每次您想更改密码时,您都需要更新此文件,从代码中您始终读取此文件中的密码。

   ' You need to update only your textfile
     Dim pwd As String = Trim(IO.File.ReadAllText("C:\WhateverFolder\YourFile.txt"))


    ' And in code doesn't need to change anything 
    SmtpServer.Credentials = New Net.NetworkCredential("dw@my-org.org", pwd) 

【讨论】:

    【解决方案2】:

    发现我的服务员做了一件很聪明的事。 如果电子邮件要发送给内部用户,那么我可以使用他设置的 SMTP 服务器并在没有密码的情况下发送它。里面的任何东西都可以通过那个 SMTP 框发送。 所以我只是更改了以下 3 行代码,它就开始工作了。 更改自:

    SmtpServer.Credentials = New Net.NetworkCredential("dw@my-org.org", "mypassword")
    

    SmtpServer.Port = 587 SmtpServer.Host = "smtp.office365.com"

    到(下面显然不是真实的主机名):

    SmtpServer.Credentials = New Net.NetworkCredential("dw@my-org.org", "")
    

    SmtpServer.Port = 25 SmtpServer.Host = "192.001.1.1"

    我还删除了下面的行:

    SmtpServer.EnableSsl = True

    现在我可以在内部发送 SMTP 电子邮件而无需密码!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-12
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 2013-06-23
      • 2014-08-11
      • 2022-01-10
      相关资源
      最近更新 更多