开发者

c# SMTP email not delivered if attachments exist

开发者 https://www.devze.com 2022-12-17 20:18 出处:网络
I am creating and sending emails using the System.Net.Mail namespace.The smtp.Send(mailMessage) call is being made within a using System.Transactions.TransactionScope block.If 开发者_StackOverflow中文

I am creating and sending emails using the System.Net.Mail namespace. The smtp.Send(mailMessage) call is being made within a using System.Transactions.TransactionScope block. If 开发者_StackOverflow中文版I do not create any email attachments then everything works fine. If I add any attachments (using mailMessage.Attachments.Add(attachment)) then no errors are thrown but the email is not delivered to the recipient - it just seems to disappear into the ether. Can anyone think of a reason why the addition of an attachment would result in the total failure of the email to be delivered (but no exception is raised!)

Thanks very much.


The issue is with the vbscript within \mailroot on the server to catch all emails (C:\inetpub\mailroot\Script\catchall.vbs). This is not part of my app, but I believe is installed as part of the default SMTP server that comes with Windows Server.

This script is checking emails and attachments and is blocking emails based on the attachment. The following code in the file on line 62 has been commented out.

'if part.ContentMediaType = "application/octet-stream" then 'Executable attachment ' ProcessMessage = true ' Exit Function 'end if

Everything now working. Thanks for your help folks.


The smpt server can be rejecting email with attachments. What type of file are you attaching? The server might confuse it with a virus or spam.


The mail server will be more suspicious of mails which have an attachment. You may need to set up a Sender Policy to convince the mail servers that you are who you claim to be.


Enable logging for System.Net.Mail, and view the log. This will at least verify that the mail has been sent. If it's been sent, and accepted, then you know the problem is on the mail server end, and you can quit chasing ghosts.

Here is a link on how to enable logging: http://systemnetmail.com/faq/4.10.aspx

Basically, add the following to your .config file:

<configuration>
  <system.diagnostics>
    <trace autoflush="true" />

    <sources>

      <source name="System.Net" >
        <listeners>
          <add name="MyTraceFile"/>
        </listeners>
      </source>

      <source name="System.Net.Sockets">
        <listeners>
          <add name="MyTraceFile"/>
        </listeners>
      </source>

    </sources>


    <sharedListeners>
      <add
        name="MyTraceFile"
        type="System.Diagnostics.TextWriterTraceListener"
        initializeData="System.Net.trace.log"                />
    </sharedListeners>

    <switches>
      <add name="System.Net" value="Verbose" />
      <add name="System.Net.Sockets" value="Verbose" />
    </switches>
 </configuration>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号