Hi all
in my appliction i want to send the document through mail i.e: outlook
here is my code

Dim myWS As Object
Dim RegKey As String
Dim Key As String
Key = "HKEY_LOCAL_MACHINE\SOFTWARE\Clients\Mail\"
'access Windows scripting
myWS = CreateObject("WScript.Shell")
'read key from registry
RegKey = myWS.RegRead(Key)
If RegKey = "Microsoft Outlook" Then



Dim mailString As String = "mailto:" & "?subject= &body="
Process.Start(mailString)
Dim aFile As String = attachfile
Dim Ret As IntPtr
While Ret = 0
Application.DoEvents()
Ret = FindWindow(vbNullString, " ")
End While
SendKeys.Send("%if" & attachfile & "{ENTER}")

but it is not attaching the file just it opens the outlook
can anybady tell me where im my code goes wrong?

Recommended Answers

All 9 Replies

hey the code which u have posted works for all ? i mean to ask if i copy this code in my application (VB.NET 2005) will this work ? ie..atleast ms outlook window will open ?
and one more thing, RegKey...from where u got it ? or is it common to all ? am a raw fresher so need help kindly reply....thanks a lot in advance

Hello friend try this in vb.net;

Imports System.Web.mail

Public Class Form1
    Inherits System.Windows.Forms.Form
    ' Variable which will send the mail
    Dim obj As System.Web.Mail.SmtpMail

    'Variable to store the attachments 
    Dim Attachment As System.Web.Mail.MailAttachment

    'Variable to create the message to send
    Dim Mailmsg As New System.Web.Mail.MailMessage

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        'Show open dialogue box to select the files to attach
        Dim Counter As Integer
        OFD.CheckFileExists = True
        OFD.Title = "Select file(s) to attach"
        OFD.ShowDialog()

        For Counter = 0 To UBound(OFD.FileNames)
            lstAttachment.Items.Add(OFD.FileNames(Counter))
        Next
    End Sub

    Private Sub BtnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRemove.Click
        'Remove the attachments
        If lstAttachment.SelectedIndex > -1 Then
            lstAttachment.Items.RemoveAt(lstAttachment.SelectedIndex)
        End If
    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Dim Counter As Integer

        Try
            'Validate the data
            If txtSMTPServer.Text = "" Then
                MsgBox("Enter the SMTP server info ...!!!", MsgBoxStyle.Information, "Send Email")
                Exit Sub
            End If

            If txtFrom.Text = "" Then
                MsgBox("Enter the From email address ...!!!", MsgBoxStyle.Information, "Send Email")
                Exit Sub
            End If

            If txtTo.Text = "" Then
                MsgBox("Enter the Recipient email address ...!!!", MsgBoxStyle.Information, "Send Email")
                Exit Sub
            End If

            If txtSubject.Text = "" Then
                MsgBox("Enter the Email subject ...!!!", MsgBoxStyle.Information, "Send Email")
                Exit Sub
            End If

            'Set the properties
            'Assign the SMTP server
            obj.SmtpServer = txtSMTPServer.Text
            'Multiple recepients can be specified using ; as the delimeter
            'Address of the recipient
            Mailmsg.To = txtTo.Text


            'Your From Address
            'You can also use a custom header Reply-To for a different replyto address
            Mailmsg.From = "\" & txtFromDisplayName.Text & "\ <" & txtFrom.Text & ">"


            'Specify the body format
            If chkFormat.Checked = True Then
                Mailmsg.BodyFormat = MailFormat.Html   'Send the mail in HTML Format
            Else
                Mailmsg.BodyFormat = MailFormat.Text
            End If

            'If you want you can add a reply to header 
            'Mailmsg.Headers.Add("Reply-To", "Manoj@geinetech.net")
            'custom headersare added like this
            'Mailmsg.Headers.Add("Manoj", "TestHeader")

            'Mail Subject
            Mailmsg.Subject = txtSubject.Text

            'Attach the files one by one
            For Counter = 0 To lstAttachment.Items.Count - 1
                Attachment = New MailAttachment(lstAttachment.Items(Counter))
                'Add it to the mail message
                Mailmsg.Attachments.Add(Attachment)
            Next

            'Mail Body
            Mailmsg.Body = txtMessage.Text
            'Call the send method to send the mail
            obj.Send(Mailmsg)
            MsgBox("Send Complete!")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

End Class

thanks... hope it works....

cya,
Rohan

Thanks
ur code may work but im interfacing outlook in my appliction. i just want to attach the file with outlook.

Hi laghaterohan
sory for late reply.
yes it works for all u need to have outlook istalled iin ur machine
basically im checking for the windows registry key, that method vill give u the default mail client once u get it vill open the outlook and attach the file.

Hi laghaterohan
sory for late reply.
yes it works for all u need to have outlook istalled iin ur machine
basically im checking for the windows registry key, that method vill give u the default mail client once u get it vill open the outlook and attach the file.

okie....so from where can i get windows registy key ? btw, the above code in vb.net is also working properly or not ??? i am aksing u this because so that i can directly use it...'coz i dont want to create mess as my application already has too much of coding.......
clarify

Following Coding Run successfully. Any Body want send a mail Via VB.net. You can use this code.


Send Email with Gmail

This sample base on vb.net 2005.

There are two step to create this.

1. You must insert mailSettings tag in app.config for window application or insert in web.config for web application.

2. Use System.Net.Mail namespace.

Sample Code for window application.
1. Insert this tag in app.config (Config Google SMTP).

<system.net >
<mailSettings >
<smtp from ="Test@Test">
<network host ="smtp.gmail.com" port ="587" password ="your Password" userName ="Your Mail Id"/>
</smtp>
</mailSettings>
</system.net>


2. Use System.Net.Mail namespace.


Imports System.Net.Mail


Dim client As New SmtpClient()

Dim sendTo As New MailAddress("sendToAccount@gmail.com")
Dim from As MailAddress = New MailAddress("from@address.com")
Dim message As New MailMessage(from, sendTo)


message.IsBodyHtml = True
message.Subject = "HI"
message.Body = "Type Your Msg.!!"
'multipleattach()
' Use the same account in app.config to authenticate.
System.Net.NetworkCredential("yourAccount@gmail.com", "YourPassword")


client.Host = "smtp.gmail.com"
client.UseDefaultCredentials = False
client.Credentials = basicAuthenticationInfo
client.EnableSsl = True

Try

client.Send(message)
MsgBox("SUCCESS")

Catch ex As Exception

MsgBox("SEND FAIL")
End Try

All The Best

Yes it wil work
just copy and paste the code and run..
if not works plz let me know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.