-2

我正在寻找一种方法来打开安装了计算机的默认电子邮件客户端(Outlook 或 groupwise)并附加文件。用户将在电子邮件客户端中输入其他信息。

试过:

Dim SendFrom As MailAddress = New MailAddress("test@email.com")
Dim SendTo As MailAddress = New MailAddress("test@email.com") 
Dim MyMessage As MailMessage = New MailMessage(SendFrom, SendTo) 
MyMessage.Subject = "Hola" 
MyMessage.Body = "Body:" 
'Dim attachFile As New Attachment("C:\test.txt") 
'MyMessage.Attachments.Add(attachFile) 
Dim emailClient As New SmtpClient("yahoo.com") 
emailClient.Timeout = Int32.MaxValue 
emailClient.Send(MyMessage) 
TextBox1.Text = "Message Sent"
4

2 回答 2

1

正确的方法是使用 MAPI

这是 VB6 的一些代码:

Public Function MailtoWithAttachment(ByVal Recipient As String, ByVal Subject As String, ByVal Body As String, ByVal Attachment As String) As Boolean
Dim Message As MAPIMessage
Dim RecipientA() As Byte
Dim Recipients(0) As MapiRecip
Dim AttachmentA() As Byte
Dim Attachments(0) As MapiFile
Dim SubjectA() As Byte
Dim BodyA() As Byte

Dim Result As Long

  'Set the recipient
  RecipientA = StrConv(Recipient & vbNullChar, vbFromUnicode)
  Recipients(0).lpName = VarPtr(RecipientA(0))
  Recipients(0).RecipClass = MAPI_TO
  Message.RecipCount = 1
  Message.lpRecips = VarPtr(Recipients(0))

  'Add the attachment
  AttachmentA = StrConv(Attachment & vbNullChar, vbFromUnicode)
  Attachments(0).lpPathName = VarPtr(AttachmentA(0))
  Attachments(0).Position = -1
  Message.FileCount = 1
  Message.lpFiles = VarPtr(Attachments(0))

  'Subject
  SubjectA = StrConv(Subject & vbNullChar, vbFromUnicode)
  Message.lpSubject = VarPtr(SubjectA(0))

  'And body
  BodyA = StrConv(Body & vbNullChar, vbFromUnicode)
  Message.lpNoteText = VarPtr(BodyA(0))

  'Try and send the email
  Result = MAPISendMail(0, 0, ByVal VarPtr(Message), MAPI_DIALOG, 0&)
  'Return false if there was a problem (ignoring canel)
  MailtoWithAttachment = Result = 0 Or Result = 1
End Function

这使用来自MAPI32.bas的声明,并大量使用 unicode 到 ANSI 的转换和结构中的指针。

请注意,并非所有邮件客户端都支持这一点,唯一的解决方案是为每个客户端使用自定义界面。

于 2013-03-07T17:28:20.977 回答
-1

取决于您使用的开发环境。看看你是否可以构建一个 .EML 文件。并启动一个新进程来加载文件。只要您的电子邮件客户端与 .EML 扩展名相关联,这就会起作用。

在这里,您可以找到 .NET 环境的示例。

打开默认邮件客户端以及附件

于 2014-09-01T16:23:25.347 回答