Smtp.NET 3 Documentation

SmtpServer.AddAttachment Method (String, String)

Adds an attachment to the current message

[Visual Basic]
Overloads Public Function AddAttachment( _
   ByVal pathname As String, _
   ByVal contentType As String _
) As Boolean
[C#]
public bool AddAttachment(
   string pathname,
   string contentType
);

Parameters

pathname
Fully qualified pathname to the file to be attached.
contentType
Overridable content-type.

Return Value

false if file does not exist, or true if file exists.

Example

[C#]
            public void Send()
            {
                 SmtpServer oSmtpDotNet = new SmtpServer("c:\\temp\\log.txt");
             
                 // Set the remote server name.  If left blank, "localhost" is used.
                 oSmtpDotNet.ServerAddress = "mail.domain.com"
                 
                 // Message Addressing.  Instead of using the ToAddress property...
                 oSmtpDotNet.AddRecipient("to@domain.com", "Bob Barker", "TO");
                 oSmtpDotNet.FromAddress = "from@domain.com";
             
                 // Set the message subject and body
                 oSmtpDotNet.Body = "This is the message";
                 oSmtpDotNet.Subject = "Routine email";
                 
                 // Add an attachment
                 oSmtpDotNet.AddAttachment("C:\temp\files.zip", "application/zip");
                  
                 ReturnCodes nRC = oSmtpDotNet.Send();
                 if ( nRC != ReturnCodes.SUCCESS )
                 {
                      Console.WriteLine("Error #"+nRC+" occurred.");
                      Console.WriteLine(oSmtpDotNet.LastError);
                      Console.Write(oSmtpDotNet.LogFileToString(false));
                      return;
                 }
             
                 // Success!
                 Console.WriteLine("Success!");
            }
            
[VB]
            Public Function Send() As Integer
                 Dim oSmtpDotNet As New SmtpServer("c:\temp\log.txt")
                 Dim nRC As ReturnCodes
            
                 ' Set the remote server name.  If left blank, "localhost" is used.
                 oSmtpDotNet.ServerAddress = "mail.domain.com"
                 
                 ' Message Addressing.  Instead of using the ToAddress property...
                 oSmtpDotNet.AddRecipient("to@domain.com", "Bob Barker", "TO")
                 oSmtpDotNet.FromAddress = "from@domain.com"
            
                 ' Set the message subject and body
                 oSmtpDotNet.Subject = "Routine email"
                 oSmtpDotNet.Body = "This is a briefing of the financials"
            
                 ' Add an attachment
                 oSmtpDotNet.AddAttachment("C:\temp\files.zip", "application/zip")
                  
                 ' Send the message.  Get the return code and store it in the variable
                 ' nRC.  Then check nRC for success or failure
                 nRC = oSmtpDotNet.Send()
                 If (nRC <> ReturnCodes.SUCCESS) Then
                     ' A problem occurred.
                     ' Write out the last error encountered
                     Console.WriteLine(oSmtpDotNet.LastError)
                     ' Write out the log file contents
                     Console.WriteLine(oSmtpDotNet.LogFileToString(False))
                     Exit Function
                 End If
            
                 ' Success!
                 Console.WriteLine("Message Sent!")
            End Function
            

See Also

SmtpServer Class | SmtpServer Members | SmtpDotNet Namespace | SmtpServer.AddAttachment Overload List