VB
Microsoft 开发的一种面向对象的编程语言,其在 .NET Framework 上实现。 以前称为 Visual Basic .NET。
76 个问题
尝试使用代码通过电子邮件发送附件。作为新手,我需要一些帮助。下面的代码是在 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
它是一个字符串,其中包含用于SMTP事务的主机的名称或IP地址。 SmtpClient(String)
如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。
注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。