使用代码通过电子邮件发送附件

Hui Liu-MSFT 48,571 信誉分 Microsoft 供应商
2024-06-13T07:42:25.6433333+00:00

尝试使用代码通过电子邮件发送附件。作为新手,我需要一些帮助。下面的代码是在 MS 网站上找到的,它似乎可以满足我的需要。但是,代码需要服务器参数,我不知道它是什么或如何获取它。这是代码。

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(  
        "jane@contoso.com",  
        "ben@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  

Note:此问题总结整理于:Using Code to Email an attachment

VB
VB
Microsoft 开发的一种面向对象的编程语言,其在 .NET Framework 上实现。 以前称为 Visual Basic .NET。
73 个问题
0 个注释 无注释
{count} 票

1 个答案

排序依据: 非常有帮助
  1. Jiale Xue - MSFT 46,466 信誉分 Microsoft 供应商
    2024-06-13T08:06:33.1533333+00:00

    它是一个字符串,其中包含用于SMTP事务的主机的名称或IP地址。 SmtpClient(String)


    如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。

    注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。

    0 个注释 无注释

你的答案

问题作者可以将答案标记为“接受的答案”,这有助于用户了解已解决作者问题的答案。