VB.Net:用to,cc,subject,body,attachment打开outlook

时间:2011-03-08 08:19:22

标签: vb.net outlook attachment

我想从我的vb.net应用程序中打开outlook。我想通过我的应用程序填写邮件的“收件人”,“主题”,“正文”和“附件”部分。在这里我不需要发送邮件。我想用mail参数打开outlook。

请建议我如何才能完成这项任务

4 个答案:

答案 0 :(得分:5)

一般程序如下:

  • 使用所需信息创建mailto:链接字符串
  • 将该字符串传递给Process.Start。这将打开默认邮件客户端,而不是必需的Outlook。

例如,字符串可能如下所示:mailto:mail@example.com?subject=Hello&body=test。必须正确转义各个字段(URL编码)。有关语法的更多信息,请参阅RFC 2368

可以使用attachment字符串中的mailto参数添加附件。根据{{​​3}},这必须加倍引用。那就是:

mailto:mail@example.com?subject=Hello&body=Test&attachment=""C:\file.txt""

答案 1 :(得分:2)

使用office interop可以实现此任务,只需将我们的projecto引用添加到“Microsoft.Office.Interop.Outlook”即可。然后在您的应用程序中可以打开邮件,然后按如下方式附加文件:

Imports Microsoft.Office.Interop

...

Dim Outlook As Outlook.Application
Dim Mail As Outlook.MailItem
Dim Acc As Outlook.Account

Outlook = New Outlook.Application()
Mail = Outlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
Mail.To = "test@test.com"
Mail.Subject = "Hello World!"

'If you have multiple accounts you could change it in sender:
For Each Acc In Outlook.Session.Accounts
    'Select first pop3 for instance.
    If Acc.AccountType = Microsoft.Office.Interop.Outlook.OlAccountType.olPop3 Then
        Mail.Sender = Acc
    End If
Next

'Take default account if no sender...
If Not Sender Is Nothing Then Mail.Sender = Sender.CurrentUser.AddressEntry

'Attach files
Mail.Attachments.Add("C:\Path\To\File.pdf")
Mail.Attachments.Add("C:\Path\To\File1.pdf")

'Append some text:
Mail.HTMLBody &= "Hello World!"

Mail.Display()

这是一个老问题,但我想这对其他人有帮助。

答案 2 :(得分:0)

如果有人想在不使用Outlook的情况下这样做...

Imports System.Net.Mail

Public Function SendEmail(EmailBody As String, EmailSubject As String, EmailTo As String, AttachmentPath As String, EmailAsHTML As Boolean)

    Dim Mail As New MailMessage

    Try
        Dim SMTP As New SmtpClient("smtp.gmail.com")
        SMTP.EnableSsl = True
        SMTP.Credentials = New System.Net.NetworkCredential("[your gmail address@gmail.com]", "[the associated password]")
        SMTP.Port = 587

        Mail.From = New MailAddress("""[Friendly Name]"" <[your gmail address@gmail.com>")

        'Split Multiple Addresses
        If EmailTo.Contains(";") Then
            For Each EmailAddress In EmailTo.Split(";")
                Mail.To.Add(Trim(EmailAddress))
            Next
        Else
            Mail.To.Add(Trim(EmailTo))
        End If

        Mail.Subject = EmailSubject
        Mail.Body = EmailBody
        If AttachmentPath <> "" Then Mail.Attachments.Add(New Mail.Attachment(AttachmentPath))
        Mail.IsBodyHtml = EmailAsHTML

        SMTP.Send(Mail)

        'Clear Mail Object
        Mail.Dispose()

        'Function Return
        Return True

    Catch ex As Exception

        'Function Return
        Return False

    End Try
End Function

答案 3 :(得分:-1)

找到这篇精彩的帖子。

Programmatically adding attachments to emails in C# and VB.NET

链接文章介绍了如何使用MAPI32.dll提供的MAPISendMail函数。

    <DllImport("MAPI32.DLL")> _
     Private Shared Function MAPISendMail(ByVal sess As IntPtr, 
         ByVal hwnd As IntPtr, ByVal message As MapiMessage, 
         ByVal flg As Integer, ByVal rsv As Integer) As Integer
    End Function

    Private Function SendMail(ByVal strSubject As String, 
        ByVal strBody As String, ByVal how As Integer) As Integer
        Dim msg As MapiMessage = New MapiMessage()
        msg.subject = strSubject
        msg.noteText = strBody

        msg.recips = GetRecipients(msg.recipCount)
        msg.files = GetAttachments(msg.fileCount)

        m_lastError = MAPISendMail(New IntPtr(0), New IntPtr(0), msg, how, 
            0)
        If m_lastError > 1 Then
            MessageBox.Show("MAPISendMail failed! " + GetLastError(), 
                "MAPISendMail")
        End If

        Cleanup(msg)
        Return m_lastError
    End Function

希望它会帮助那些可能像我一样找到自己的人。