【发布时间】: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 电子邮件。我想你必须问问自己,我更改电子邮件的最简单方法是什么。