Results 1 to 37 of 37

Thread: Sending Email through outlook to email in cell in datagrdiview

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Sending Email through outlook to email in cell in datagrdiview

    Hi,

    please can you help please to send Email through outlook to email in cell in datagrdiview,
    I have a datagridview from Ms Access db and emails in one of the columns,
    I would like to send Email like a reminder to those emails in the cells in the datagrdiview if the status of the item is open,

    I wish you could help me with is, I am struggling with it.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Sending Email through outlook to email in cell in datagrdiview

    Do you know how to send an email using Outlook? Does this email even have to be sent using Outlook, or would the user's default email client (whatever that may be) be a more appropriate option?

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Sending Email through outlook to email in cell in datagrdiview

    Quote Originally Posted by jmcilhinney View Post
    Do you know how to send an email using Outlook? Does this email even have to be sent using Outlook, or would the user's default email client (whatever that may be) be a more appropriate option?
    Yes the default email is outlook
    I know how to send email through outlook

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Sending Email through outlook to email in cell in datagrdiview

    Quote Originally Posted by vbuser99 View Post
    Yes the default email is outlook
    That doesn't really answer my question. You've got three main choices when sending an email from a .NET app. They are, in increasing order of complexity:

    1. Call Process.Start and pass a mailto URL. That will open a new mail message in the default email client regardless of what that is. The user would then click Send in that client.

    2. Use the MailMessage and SmtpClient classes to send an email via a specific SMTP server. That will bypass an email client installed on the system.

    3. Use Office Automation to actually remote control an instance of the Outlook application.
    Quote Originally Posted by vbuser99 View Post
    I know how to send email through outlook
    If you already know how to send the email then your question is simply how to get the email addresses out of the DataGridView. If I hadn't asked then we may have wasted our time explaining how to do something that you already know how to do. Please provide a FULL and CLEAR explanation of the actual problem so that we know what question we're supposed to be answering.

    Getting data from a specific column of a DataGridView is a simple matter of using a For or For Each loop. I'm sure that a web search would turn up a number of examples but it shouldn't be too hard to work out for yourself. You can use a loop to get access to each row and you can access a cell value inside the loop. I would suggest that you give it a go and then, if it doesn't work, post back and show us what you've done and we can help you fix it.

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Sending Email through outlook to email in cell in datagrdiview

    thank you for your efforts,
    I know how to send the email to specific email not to an email in a cell of datagridview,
    I am new with Vb.net

    please help me with the coding

  6. #6
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,040

    Re: Sending Email through outlook to email in cell in datagrdiview

    Hi,

    picking up on JMC's..
    1. Call Process.Start and pass a mailto URL. That will open a new mail message in the default email client regardless of what that is. The user would then click Send in that client.
    depending on the Datagridview Cell
    Code:
    'in my Datagridview I have a column with the name "Email"
    
    Private Sub DataGridView1_DataBindingComplete(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
            For Each r As DataGridViewRow In DataGridView1.Rows
                r.Cells("Email") = New DataGridViewLinkCell()
                Dim c As DataGridViewLinkCell = TryCast(r.Cells("Email"), DataGridViewLinkCell)
            Next
        End Sub
    
        Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            If TypeOf DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex) Is DataGridViewLinkCell Then
                Process.Start("mailto:" + DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
            End If
        End Sub
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Sending Email through outlook to email in cell in datagrdiview

    Quote Originally Posted by ChrisE View Post
    Hi,

    picking up on JMC's..


    depending on the Datagridview Cell
    Code:
    'in my Datagridview I have a column with the name "Email"
    
    Private Sub DataGridView1_DataBindingComplete(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
            For Each r As DataGridViewRow In DataGridView1.Rows
                r.Cells("Email") = New DataGridViewLinkCell()
                Dim c As DataGridViewLinkCell = TryCast(r.Cells("Email"), DataGridViewLinkCell)
            Next
        End Sub
    
        Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            If TypeOf DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex) Is DataGridViewLinkCell Then
                Process.Start("mailto:" + DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
            End If
        End Sub
    regards
    Chris
    appreceat that chrise, is that will email with click on the cell ?
    because I need it to send with a click of a button

  8. #8
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,040

    Re: Sending Email through outlook to email in cell in datagrdiview

    Hi,

    you click on the Cell
    the Email address looks like ...XYZ@t-online.de

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Sending Email through outlook to email in cell in datagrdiview

    Quote Originally Posted by ChrisE View Post
    Hi,

    you click on the Cell
    the Email address looks like ...XYZ@t-online.de

    regards
    Chris
    I will try that and let you know

    thanks

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Sending Email through outlook to email in cell in datagrdiview

    Quote Originally Posted by vbuser99 View Post
    I know how to send the email to specific email not to an email in a cell of datagridview
    You're trying to create a problem where there isn't one. An email address is just a String. Where that String comes from is completely irrelevant when it comes to using it. If you know how to send an email then you know how to send an email. You can write a method that takes a String as an argument and sends an email to the address in that String. That's exactly what you should do. That then encapsulates the sending of the email completely and you can call it and pass any String from any source. All you then have to do is get the email address from the grid and call the method.

  11. #11

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Sending Email through outlook to email in cell in datagrdiview

    I am not trying to make problems guys,

    to be clear with you,

    I have a datagridview with Name, Email, Status, and Send Button
    what I want is to send an email with this button to all the emails that their status is Not completed.

    I know it kind of confusing but I am new to Vb.net and trying to learn please please

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Sending Email through outlook to email in cell in datagrdiview

    Firstly, doing something when the user clicks a button in a row in a DataGridView is the same no matter what it is that you want to do. You can search for that independently of anything else.

    Secondly, getting a value from a cell in row of a DataGridView is the same regardless of what that data represents and what you want to do with it. You can search for that independently of anything else.

    Thirdly, you already know how to send an email so you don't even need to search for that but, if you did, you could do so independently of anything else.

    It is a classic beginners' mistake to insist on looking at a problem as a whole instead of looking at it as a series of parts that can be addressed independently and then their solutions combined. When someone wants to build a new cell phone, do they start from scratch and invent every component? Of course not. They use all that they already know, come up with solutions to the parts they don't and then put all the pieces together, massaging around the edges to make them fit, into a cohesive whole. That's exactly how you need to approach programming problems. As I have already said - and you have chosen to ignore - if you already know how to send an email then you can simply put that into a method and that part's done, e.g.
    vb.net Code:
    1. Private Sub SendEmail(address As String)
    2.     'Code here to send email to specified address.
    3. End Sub
    You say that you already know how to send an email so you already have the code to write in that method. That's the email part of your problem done and dusted so email is now irrelevant to the problem. You now have a method that has a parameter of type String and you need to get a String from a particular cell in a DataGridViewRow and pass it to that method, e.g.
    vb.net Code:
    1. Private Sub SendEmailToAddressInRow(row As DataGridViewRow)
    2.     Const addressColumnIndex As Integer = 1
    3.     Dim addressCell = row.Cell(addressColumnIndex)
    4.     Dim address = CStr(addressCell.Value)
    5.  
    6.     SendEmail(address)
    7. End Sub
    As you can see, that second method has nothing specifically to do with email. It's simply getting a value from a grid row and passing it to another method. You could have found out how to do that quite easily, although I would hope that you would already know.

    Now that you have sending the email and getting the required cell value covered, all that remains is how to react to the click of a button in a row and to pass that row to that second method. I'll leave that part - the part that didn't even rate a mention before post #11 - to you.

    By the way, you also mention that you only want to send the email if another cell contains a particular value. You already know how to get a value from a cell from my example code above. Testing the condition is simply an If statement. I'm assuming that you know how to write an If statement but, if you don't, that's also an easy one to learn. For that and other fundamentals, you might try here if you need to.

  13. #13
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: Sending Email through outlook to email in cell in datagrdiview

    Imports System.Net.Mail

    Public Class Form1
    ' This will do the sending for us.
    Private Client As SmtpClient

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    ' Initialize the client, host would be something like hotmail.com or gmail.com and the port would be something like 587
    Client = New SmtpClient("Host", 587)
    End Sub

    Private Sub cmdSend_Click(sender As Object, e As EventArgs) Handles cmdSend.Click
    ' Get all rows where the status is 'Not completed'
    Dim TheList As Generic.List(Of DataGridViewRow) = (From R As DataGridViewRow In Me.DataGridView1.Rows Where R.Cells(2).Value = "Not completed" Select R).ToList
    ' Empty email.
    Dim Msg As MailMessage

    ' Check if we have succeeded in getting any rows.
    If TheList IsNot Nothing Then
    ' Loop Through All The Rows in The List
    For Each Row As DataGridViewRow In TheList
    ' Create a new email
    Msg = New MailMessage()
    ' Add the sender to the email
    Msg.From = New MailAddress("Sender@hotmail.com")
    ' Extract the email address from the row and add it to the email.
    Msg.To.Add(New MailAddress(Row.Cells(2).Value))
    ' Add a titel
    Msg.Subject = $"Dear {Row.Cells(0).Value}"
    ' Add the body
    Msg.Body = "Dman man... Do you work and get your status to completed"
    ' Send
    Client.Send(Msg)
    Next
    End If
    End Sub
    End Class

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Sending Email through outlook to email in cell in datagrdiview

    @Goggy, once more with formatting:
    vb.net Code:
    1. Imports System.Net.Mail
    2.  
    3. Public Class Form1
    4.     ' This will do the sending for us.
    5.     Private Client As SmtpClient
    6.  
    7.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    8.         ' Initialize the client, host would be something like hotmail.com or gmail.com and the port would be something like 587
    9.         Client = New SmtpClient("Host", 587)
    10.     End Sub
    11.  
    12.     Private Sub cmdSend_Click(sender As Object, e As EventArgs) Handles cmdSend.Click
    13.         ' Get all rows where the status is 'Not completed'
    14.         Dim TheList As Generic.List(Of DataGridViewRow) = (From R As DataGridViewRow In Me.DataGridView1.Rows Where R.Cells(2).Value = "Not completed" Select R).ToList
    15.         ' Empty email.
    16.         Dim Msg As MailMessage
    17.  
    18.         ' Check if we have succeeded in getting any rows.
    19.         If TheList IsNot Nothing Then
    20.             ' Loop Through All The Rows in The List
    21.             For Each Row As DataGridViewRow In TheList
    22.                 ' Create a new email
    23.                 Msg = New MailMessage()
    24.                 ' Add the sender to the email
    25.                 Msg.From = New MailAddress("Sender@hotmail.com")
    26.                 ' Extract the email address from the row and add it to the email.
    27.                 Msg.To.Add(New MailAddress(Row.Cells(2).Value))
    28.                 ' Add a titel
    29.                 Msg.Subject = $"Dear {Row.Cells(0).Value}"
    30.                 ' Add the body
    31.                 Msg.Body = "Dman man... Do you work and get your status to completed"
    32.                 ' Send
    33.                 Client.Send(Msg)
    34.             Next
    35.         End If
    36.     End Sub
    37. End Class

  15. #15
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: Sending Email through outlook to email in cell in datagrdiview

    Sorry, for the not formating... *Blush

  16. #16

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Sending Email through outlook to email in cell in datagrdiview

    Quote Originally Posted by Goggy View Post
    Imports System.Net.Mail

    Public Class Form1
    ' This will do the sending for us.
    Private Client As SmtpClient

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    ' Initialize the client, host would be something like hotmail.com or gmail.com and the port would be something like 587
    Client = New SmtpClient("Host", 587)
    End Sub

    Private Sub cmdSend_Click(sender As Object, e As EventArgs) Handles cmdSend.Click
    ' Get all rows where the status is 'Not completed'
    Dim TheList As Generic.List(Of DataGridViewRow) = (From R As DataGridViewRow In Me.DataGridView1.Rows Where R.Cells(2).Value = "Not completed" Select R).ToList
    ' Empty email.
    Dim Msg As MailMessage

    ' Check if we have succeeded in getting any rows.
    If TheList IsNot Nothing Then
    ' Loop Through All The Rows in The List
    For Each Row As DataGridViewRow In TheList
    ' Create a new email
    Msg = New MailMessage()
    ' Add the sender to the email
    Msg.From = New MailAddress("Sender@hotmail.com")
    ' Extract the email address from the row and add it to the email.
    Msg.To.Add(New MailAddress(Row.Cells(2).Value))
    ' Add a titel
    Msg.Subject = $"Dear {Row.Cells(0).Value}"
    ' Add the body
    Msg.Body = "Dman man... Do you work and get your status to completed"
    ' Send
    Client.Send(Msg)
    Next
    End If
    End Sub
    End Class
    thank you so much Goggy,

    appreciate that, what if I will use Outlook to send the email?

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Sending Email through outlook to email in cell in datagrdiview

    Quote Originally Posted by vbuser99 View Post
    what if I will use Outlook to send the email?
    You've already stated that you know how to do that.

  18. #18

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Sending Email through outlook to email in cell in datagrdiview

    Quote Originally Posted by jmcilhinney View Post
    You've already stated that you know how to do that.
    yes, but in his code he implemented the sending with getting the specific rows,
    I tried but it did work,
    please guy's I am learning here

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Sending Email through outlook to email in cell in datagrdiview

    Quote Originally Posted by vbuser99 View Post
    I tried but it did work,
    please guy's I am learning here
    If you want to learn then show us what you tried and tell us what happened, then we can tell you what's wrong with it and help you fix it. Learning is not about getting other people to write your code for you, particularly if you may well have most of it already done.

  20. #20

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Sending Email through outlook to email in cell in datagrdiview

    Quote Originally Posted by jmcilhinney View Post
    If you want to learn then show us what you tried and tell us what happened, then we can tell you what's wrong with it and help you fix it. Learning is not about getting other people to write your code for you, particularly if you may well have most of it already done.
    this is what I have tried,

    Code:
    Imports outlook = Microsoft.Office.Interop.Outlook
    
    
    Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click
            ' Get all rows where the status is 'Not completed'
            Dim TheList As Generic.List(Of DataGridViewRow) = (From R As DataGridViewRow In Me.DataGridView1.Rows Where R.Cells(2).Value = "Open" Select R).ToList
            ' Empty email.
            Dim OutlookMessage As outlook.MailItem
    
    
            ' Check if we have succeeded in getting any rows.
            If TheList IsNot Nothing Then
                ' Loop Through All The Rows in The List
                For Each Row As DataGridViewRow In TheList
                    ' Create a new email
                    OutlookMessage = New MailMessage()
                    ' Add the sender to the email
                    'OutlookMessage.From = New MailAddress("Sender@hotmail.com") NO NEED FOR FROM ADDRESS
                    ' Extract the email address from the row and add it to the email.
                    OutlookMessage.Recipients.Add(Row.Cells(9).Value)
                    ' Add a titel 
     OutlookMessage.Subject = $"Dear {Row.Cells(0).Value}"
                    ' Add the body
                    OutlookMessage.Body = "Dman man... Do you work and get your status to completed"
                    ' Send 
                    Client.Send(Msg)
                Next
            End If
        End Sub

  21. #21
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Sending Email through outlook to email in cell in datagrdiview

    So when I said show us what you've done and tell us what happened, you decided that just showing us what you've done would be enough? You say that you want to learn but it doesn't necessarily seem like it.

    For a start, you said that you knew how to send a message using Outlook and I said that you should put that code that you already know works into a method and I showed you how to do that. You have chosen to ignore that. Why is that? I also showed you how write a method that takes a grid row and uses that existing method to send an email. All you would then have to do is call that second method from with the loop that you were given in post #13. You already have everything you need. You simply have to put the parts together but you're basically refusing to do that. You've got a loop that gets you the rows, you've got a method that gets the email address from a row and you've got (or should have) a method that sends an email. That is the whole solution right there.

  22. #22

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Sending Email through outlook to email in cell in datagrdiview

    Quote Originally Posted by jmcilhinney View Post
    So when I said show us what you've done and tell us what happened, you decided that just showing us what you've done would be enough? You say that you want to learn but it doesn't necessarily seem like it.

    For a start, you said that you knew how to send a message using Outlook and I said that you should put that code that you already know works into a method and I showed you how to do that. You have chosen to ignore that. Why is that? I also showed you how write a method that takes a grid row and uses that existing method to send an email. All you would then have to do is call that second method from with the loop that you were given in post #13. You already have everything you need. You simply have to put the parts together but you're basically refusing to do that. You've got a loop that gets you the rows, you've got a method that gets the email address from a row and you've got (or should have) a method that sends an email. That is the whole solution right there.
    thank you for your help and your efforts my friend,
    IF you could help Then
    HELP
    Else
    HELP ALSO
    End IF

  23. #23
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Sending Email through outlook to email in cell in datagrdiview

    I already have helped. You just refuse to use the help provided and expect someone to write the code for you to copy and paste. I won't be doing that. You say you want to learn. You have that opportunity. I'll not be contributing any further.

  24. #24
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,040

    Re: Sending Email through outlook to email in cell in datagrdiview

    Quote Originally Posted by vbuser99 View Post
    Yes the default email is outlook
    I know how to send email through outlook
    Hi,
    please Post your Code how you send an Email

    that means how do you fill the Datagridview etc...

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  25. #25

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Sending Email through outlook to email in cell in datagrdiview

    Quote Originally Posted by jmcilhinney View Post
    I already have helped. You just refuse to use the help provided and expect someone to write the code for you to copy and paste. I won't be doing that. You say you want to learn. You have that opportunity. I'll not be contributing any further.
    your contribution is really matters, we are here to help each other
    I am 11 years old and trying to read the codes and learn more

  26. #26
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Sending Email through outlook to email in cell in datagrdiview

    Quote Originally Posted by vbuser99 View Post
    I am 11 years old and trying to read the codes and learn more
    That's great and I applaud you. I'm more than happy to help further if you try to get it done but can't. I need to see people try though. The best way to learn is to do and I want people to learn, so I expect them to do. Failing is part of learning too though, so I don't hold failure to succeed against people, just failure to try. Of course, I'm not the king of programming or this forum, so you're free to ignore me if you want to.

  27. #27

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Sending Email through outlook to email in cell in datagrdiview

    Quote Originally Posted by ChrisE View Post
    Hi,
    please Post your Code how you send an Email

    that means how do you fill the Datagridview etc...

    regards
    Chris
    here is my code to send email
    Code:
    Dim OutlookMessage As outlook.MailItem
            Dim AppOutlook As New outlook.Application
            Try
                OutlookMessage = AppOutlook.CreateItem(outlook.OlItemType.olMailItem)
                Dim Recipents As outlook.Recipients = OutlookMessage.Recipients
                Recipents.Add("my email here")
                OutlookMessage.Subject = "my subject"
                OutlookMessage.Body = "the body of email here"
                OutlookMessage.BodyFormat = outlook.OlBodyFormat.olFormatHTML
                OutlookMessage.Send()
            Catch ex As Exception
                MessageBox.Show("Mail could not be sent") 'if you dont want this message, simply delete this line  
            Finally
                OutlookMessage = Nothing
                AppOutlook = Nothing
            End Try

  28. #28
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Sending Email through outlook to email in cell in datagrdiview

    So now go back to post #12 and do as suggested there.

  29. #29

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Sending Email through outlook to email in cell in datagrdiview

    Quote Originally Posted by jmcilhinney View Post
    So now go back to post #12 and do as suggested there.
    I tried and got confused by mixing the codes together

  30. #30

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Sending Email through outlook to email in cell in datagrdiview

    I tried this but I failed

    Code:
    
    Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click
            SendEmailToAddressInRow(row)
    
        End Sub
    
        Private Sub SendEmail(address As String)
    
            Dim OutlookMessage As outlook.MailItem
            Dim AppOutlook As New outlook.Application
            Try
                OutlookMessage = AppOutlook.CreateItem(outlook.OlItemType.olMailItem)
                Dim Recipents As outlook.Recipients = OutlookMessage.Recipients
                Recipents.Add("vbuser99@test.com")
                OutlookMessage.Subject = "Subject"
                OutlookMessage.Body = "email body"
                OutlookMessage.BodyFormat = outlook.OlBodyFormat.olFormatHTML
                OutlookMessage.Send()
            Catch ex As Exception
                MessageBox.Show("Mail could not be sent") 'if you dont want this message, simply delete this line  
            Finally
                OutlookMessage = Nothing
                AppOutlook = Nothing
            End Try
        End Sub
    
        Private Sub SendEmailToAddressInRow(row As DataGridViewRow)
    
            Const addressColumnIndex As Integer = 1
    
            Dim addressCell = row.Cell(addressColumnIndex)
    
            Dim address = CStr(addressCell.Value)
            SendEmail(address)
    
        End Sub
    Last edited by vbuser99; Dec 3rd, 2017 at 05:58 AM.

  31. #31
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: Sending Email through outlook to email in cell in datagrdiview

    Hello vbuser99,

    What is the fault your getting with your code?
    I must admit, i have a hard time believing your just 11...


    Not sure if its you, but found vbuser99 at www.vbulletin.org who joined back in 2004, that would make it so that you started programming when you were -2

    seems you also joint xtremevbtalk back in 2008....

    Must admit you started waaaaaaaaaaaaaaaaaaaaaaaaaaaaaaay young!!!!
    Last edited by Goggy; Nov 28th, 2017 at 10:11 AM.
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  32. #32
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,040

    Re: Sending Email through outlook to email in cell in datagrdiview

    Quote Originally Posted by vbuser99 View Post
    your contribution is really matters, we are here to help each other
    I am 11 years old and trying to read the codes and learn more
    well this just uses the Datagridview with the Email addresses in Cell(1)
    but be careful, if you have 200 Adresses with NC in Cell(2) = Not Complete
    it will create these 200 new Emails

    Code:
     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim oMail = New System.Diagnostics.Process()
            With oMail.StartInfo
    
                'get the Emailaddresses out of the Datagridview 
                'in this case my Emailaddresses are in Cell(1)
    
                'in Cell(2) I put a Text = NC which means 'Not Complete'
    
                Dim MailList As Generic.List(Of DataGridViewRow) _
                = (From R As DataGridViewRow In Me.DataGridView1.Rows _
                   Where R.Cells(2).Value = "NC" Select R).ToList
    
                If MailList IsNot Nothing Then
                    For Each Row As DataGridViewRow In MailList
    
                        .FileName = "mailto:" & (Row.Cells(1).Value) & _
                        "?subject=Get the Job done&body=here your Emailbody text"
    
                        .UseShellExecute = True
                        .RedirectStandardOutput = False
                        oMail.Start()
                    Next
    
                End If
            End With
    
        End Sub

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  33. #33
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Sending Email through outlook to email in cell in datagrdiview

    This is my approach

    Code:
        Private Sub Btn_SendToEmail_Click(sender As Object, e As EventArgs) Handles Btn_SendToEmail.Click
            Try
                Dim MailToString As String = Nothing
                For Each DGVRow As DataGridViewRow In DGV_People.Rows
                    If Convert.ToBoolean(DGVRow.Cells("Select").Value) = True Then 'this would be your "Completed" check
                        If Not String.IsNullOrEmpty(DGVRow.Cells("Email").Value.ToString) Then 'make sure the email not NULL
                            MailToString &= DGVRow.Cells("Email").Value.ToString & ";"
                        End If
                    End If
                Next
                Dim Outlook As Outlook.Application
                Dim Mail As Outlook.MailItem
                Outlook = New Outlook.Application()
                Mail = CType(Outlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem), Outlook.MailItem)
                Mail.To = MailToString.Substring(0, MailToString.Length - 1)
                Mail.Display()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    Easily modified to suite your needs.

  34. #34
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Sending Email through outlook to email in cell in datagrdiview

    So you're now doing about half of what I suggested two days ago but ignoring several things that I've already told you several times. For one thing, I specifically said that that, in that SendEmail method, you should send the email to the address that gets passed in. You seem to have completely ignored that.

    Code removed as may contain sensitive information.

    See the problem there? You pass in an address to which you're supposed to be sending an email and then you just don't use it and use a hard-coded address instead.

    Secondly, I specifically said that you should use the loop provided in post #13 to get the rows and then, in that loop, call the SendEmailToAddressInRow method for the current row. Where are you doing that? You're not.

    If you have a conversation with someone then it's easy to forget some of what's said. There's no such excuse when it's all written down. You can read everything that has been written as often as you need to. Before doing anything else, I strongly suggest that you go back and read everything several times, then make an attempt to do what has been suggested.

    Having said all that, it's also worth noting that you are going to be sending a separate email to each address, rather than one email to all of them. I was going to mention that sooner but I noticed that you were using the person's name in the body, so that would require separate emails for each person. You should think a bit on which way you want to go. If you want one email to everyone then you'll need to change things slightly, but only slightly. Either way, I'd suggest getting this way working first. Just be sure not to send loads of emails to other people in the process, rather than using your own test addresses.
    Last edited by jmcilhinney; Dec 3rd, 2017 at 06:18 AM. Reason: Quote removed on request of OP.

  35. #35

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Sending Email through outlook to email in cell in datagrdiview

    Quote Originally Posted by ChrisE View Post
    well this just uses the Datagridview with the Email addresses in Cell(1)
    but be careful, if you have 200 Adresses with NC in Cell(2) = Not Complete
    it will create these 200 new Emails

    Code:
     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim oMail = New System.Diagnostics.Process()
            With oMail.StartInfo
    
                'get the Emailaddresses out of the Datagridview 
                'in this case my Emailaddresses are in Cell(1)
    
                'in Cell(2) I put a Text = NC which means 'Not Complete'
    
                Dim MailList As Generic.List(Of DataGridViewRow) _
                = (From R As DataGridViewRow In Me.DataGridView1.Rows _
                   Where R.Cells(2).Value = "NC" Select R).ToList
    
                If MailList IsNot Nothing Then
                    For Each Row As DataGridViewRow In MailList
    
                        .FileName = "mailto:" & (Row.Cells(1).Value) & _
                        "?subject=Get the Job done&body=here your Emailbody text"
    
                        .UseShellExecute = True
                        .RedirectStandardOutput = False
                        oMail.Start()
                    Next
    
                End If
            End With
    
        End Sub

    regards
    Chris
    this code worked with me,
    but it will open outlook windows as many as I have of Open status,
    my code is sending emails directly,

    can I have it sends email directly?

  36. #36
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    196

    Re: Sending Email through outlook to email in cell in datagrdiview

    Look @ post #14, It doesn't use outlook, Just pure .Net
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  37. #37

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    32

    Re: Sending Email through outlook to email in cell in datagrdiview

    Quote Originally Posted by Goggy View Post
    Look @ post #14, It doesn't use outlook, Just pure .Net
    I prefer using outlook, I am trying to figure it out with outlook

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width