VB.Net: an open worldview with, cc, subject, body, attachment

I want to open Outlook from my vb.net application. I want to fill out a part of the message "To", "Subject", "Body" and "Attachment" through my application. I don’t need to send mail here. I want to just open Outlook with the mail option.

Please suggest how I can achieve this.

+3
source share
4 answers

The general procedure is as follows:

  • Create a link bar mailto:with the necessary information
  • Pass this line to Process.Start. This will open the default email client, not Outlook.

, : mailto:mail@example.com?subject=Hello&body=test. ( URL). RFC 2368.

attachment mailto. MSDN, . :

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

, "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()

, , - .

+1

- 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
0

.

# VB.NET

A related article describes how to use the MAPISendMail function provided by MAPI32.dll.

    <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 this helps someone who might be here like me.

-1
source

All Articles