Using Code to Email an attachment

Jeff Stiegler 466 Reputation points
2022-05-24T18:09:01.957+00:00

Trying to use code to email an attachment. Being new to this I need some help. The code below was found on the MS website and it seems to do what I need. However, the code requires a server parameter and I don't know what it is or how to get it. Here is the code.

Public Shared Sub CreateMessageWithAttachment(ByVal server As String)  
    ' Specify the file to be attached And sent.  
    ' This example assumes that a file named Data.xls exists in the  
    ' current working directory.  
    Dim file As String = "data.xls"  
    ' Create a message and set up the recipients.  
    Dim message As MailMessage = New MailMessage(  
        "******@contoso.com",  
        "******@contoso.com",  
        "Quarterly data report.",  
        "See the attached spreadsheet.")  
  
    ' Create  the file attachment for this email message.  
    Dim data As Attachment = New Attachment(file, MediaTypeNames.Application.Octet)  
    ' Add time stamp information for the file.  
    Dim disposition As ContentDisposition = data.ContentDisposition  
    disposition.CreationDate = System.IO.File.GetCreationTime(file)  
    disposition.ModificationDate = System.IO.File.GetLastWriteTime(file)  
    disposition.ReadDate = System.IO.File.GetLastAccessTime(file)  
    ' Add the file attachment to this email message.  
    message.Attachments.Add(data)  
  
    ' Send the message  
    Dim client As SmtpClient = New SmtpClient(server)  
    ' Add credentials if the SMTP server requires them.  
    client.Credentials = CredentialCache.DefaultNetworkCredentials  
  
    Try  
        client.Send(message)  
    Catch ex As Exception  
        Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", ex.ToString())  
    End Try  
  
    ' Display the values in the ContentDisposition for the attachment.  
    Dim cd As ContentDisposition = data.ContentDisposition  
    Console.WriteLine("Content disposition")  
    Console.WriteLine(cd.ToString())  
    Console.WriteLine("File {0}", cd.FileName)  
    Console.WriteLine("Size {0}", cd.Size)  
    Console.WriteLine("Creation {0}", cd.CreationDate)  
    Console.WriteLine("Modification {0}", cd.ModificationDate)  
    Console.WriteLine("Read {0}", cd.ReadDate)  
    Console.WriteLine("Inline {0}", cd.Inline)  
    Console.WriteLine("Parameters: {0}", cd.Parameters.Count)  
  
    For Each d As DictionaryEntry In cd.Parameters  
        Console.WriteLine("{0} = {1}", d.Key, d.Value)  
    Next  
  
    data.Dispose()  
End Sub  
 
Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2022-05-25T05:37:38.57+00:00

    Hi @Jeff Stiegler ,
    It is a String that contains the name or IP address of the host computer used for SMTP transactions.
    SmtpClient(String)
    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.