【发布时间】:2023-03-08 00:06:02
【问题描述】:
我有一个 Windows 服务,它通过从数据库中检索记录来发送电子邮件。运行 Windows 服务的服务帐户是该计算机上的本地管理员。我遇到的问题是 Windows 服务以某种方式设法发送一些带有 pdf 附件的电子邮件,这些电子邮件存在于 Windows 服务所在的本地服务器上,并且对于其中大多数我收到一条错误消息:
无效的邮件附件并且遇到真正的困难可能是什么原因造成的?请问我应该从哪里开始寻找任何想法?
谢谢
Try
' Initialize new mail message class
Dim objEmailMessage As New System.Net.Mail.MailMessage(EmailOutboxEntity.EmailFrom, EmailOutboxEntity.EmailTo)
' Set the email parameters
objEmailMessage.Subject = EmailOutboxEntity.EmailSubject
objEmailMessage.Body = EmailOutboxEntity.EmailBody
' Check if attachments have been specified
If Not EmailOutboxEntity.TestOriginalFieldValueForNull(EmailOutboxFieldIndex.EmailAttachements) Then
' Get an array of attachment file names to send
Dim arrAttachment As String() = Split(EmailOutboxEntity.EmailAttachements, CONST_AttachmentSeparator)
' Step through each filename and attach it to the email
For Each strAttachment As String In arrAttachment
' Attach the current file to the email
objEmailMessage.Attachments.Add(New System.Net.Mail.Attachment(strAttachment))
Next
End If
' Set the SMTP server
Dim mailServer As New System.Net.Mail.SmtpClient(pstrSMTPServer)
' Send the message
mailServer.Send(objEmailMessage)
Catch errException As Exception
' Throw an exception indicating the email failed to send
Throw New EmailOutboxManagerException(errException.Message, "Test Failed to send email, got the following error instead:")
End Try
【问题讨论】:
-
服务代码将是一个很好的开始。
-
已按照您的要求提供了上述代码。
-
在循环和添加附件的地方,尝试取出整个:New System.Net.Mail.Attachment,然后放入您的 strAttachment。
-
我可以尝试这样做,但是我想问一下是否存在无效附件是否会引发异常?
-
是的,它会,但它可能源于很多事情......你可以发布问题的堆栈跟踪吗?
标签: vb.net email windows-services