用附件打开电子邮件客户端

时间:2013-03-07 14:21:53

标签: vb.net email

我正在寻找打开安装了计算机的默认电子邮件客户端(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"

2 个答案:

答案 0 :(得分: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转换和结构中的指针。

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

答案 1 :(得分:-1)

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

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

Open default mail client along with a attachment