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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5229973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:19:43  来源:igfitidea点击:

VB.Net: open outlook with to,cc,subject, body , attachment

vb.netoutlookattachment

提问by Rupesh

i want to open the outlook from my vb.net application. I want to fill out the To,Subject, Body and Attachment part of mail through my application. here i don't need to send mail . I want to just open the outlook with mail parameter.

我想从我的 vb.net 应用程序打开 Outlook。我想通过我的申请填写邮件的收件人、主题、正文和附件部分。在这里我不需要发送邮件。我只想用邮件参数打开 Outlook。

Please suggest how can i achieve this task

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

回答by Konrad Rudolph

The general procedure is as follows:

一般程序如下:

  • Create a mailto:link string with the required information
  • Pass that string to Process.Start. This will open the defaultmail client, not necessary Outlook.
  • 创建包含mailto:所需信息的链接字符串
  • 将该字符串传递给Process.Start. 这将打开默认邮件客户端,而不是 Outlook。

For example, the string might look like this: mailto:[email protected]?subject=Hello&body=test. The individual fields have to be properly escaped (URL encoded). More information about the syntax can be found in RFC 2368.

例如,字符串可能是这样的:mailto:[email protected]?subject=Hello&body=test。各个字段必须正确转义(URL 编码)。有关语法的更多信息可以在RFC 2368 中找到。

An attachment can be added by using the attachmentargument in the mailtostring. According to a comment on MSDNthis has to be doubly quoted, though. That is:

可以使用字符串中的attachment参数添加附件mailto。不过,根据MSDN的评论,这必须被双重引用。那是:

mailto:[email protected]?subject=Hello&body=Test&attachment=""C:\file.txt""

回答by ZeroWorks

This task can be achieved using office interop, just add a reference to our projecto to `Microsoft.Office.Interop.Outlook'. Then in your applicattion can créate a mail and then attach files as follows:

这个任务可以使用 office interop 来实现,只需将我们的项目引用添加到“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 = "[email protected]"
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()

It's an old question, but I guess it could help to somebody else.

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

回答by Tim

In case someone wants to do this without using outlook...

如果有人想在不使用 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 [email protected]]", "[the associated password]")
        SMTP.Port = 587

        Mail.From = New MailAddress("""[Friendly Name]"" <[your gmail [email protected]>")

        '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

回答by Michael Rushizha

Found this great post.

找到了这篇很棒的帖子。

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

在 C# 和 VB.NET 中以编程方式向电子邮件添加附件

The linked article documents how to use the MAPISendMail function provided by MAPI32.dll.

链接的文章记录了如何使用 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

Hope it will help somebody who might find themselves way here as I did.

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