Linklabel -- how to populate subject & body of email?

BugMan

Well-known member
Joined
Dec 27, 2010
Messages
52
Location
Tallahassee FL
Programming Experience
10+
In my app I want the user to be able to click a linklabel on the form to send me an email. Easy enough using this:

VB.NET:
    Private Sub linklabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles linklabel1.LinkClicked
        System.Diagnostics.Process.Start("mailto:support@software.com?subject=Hello BugMan")
    End Sub
This code adds a subject line also, but I'd like to go a step further and add something to the body of the text... how is this done?

thanks in advance.
 

BugMan

Well-known member
Joined
Dec 27, 2010
Messages
52
Location
Tallahassee FL
Programming Experience
10+
Thanks JohnH, as always your quick help is appreciated. I see that if put "?body" after the 'To' address, then it puts the following text into the body of the message. But in this case, the subject doesn't work, all text after "body = " ends up in the body. I'm having a hard time finding examples of this, and I've looked pretty hard... can you tell me what the format is for adding both the Subject and body text? Should there be a delimiter between the two in the line of code below?

VB.NET:
System.Diagnostics.Process.Start("mailto:support@bugware.com?body=this is body text? subject=this is subject test")
thanks again.
 

BugMan

Well-known member
Joined
Dec 27, 2010
Messages
52
Location
Tallahassee FL
Programming Experience
10+
D'oh! I went to the other two links and didn't notice the 1st, needed link... gotta use the ampersand between the subject and body:

VB.NET:
mailto:user@example.com?subject=Message Title[COLOR="#FF0000"]&[/COLOR]body=Message Content
Next question.... in my app, I have a code in a textbox that the user previously copy/pasted into an email himself. I'd like my program to put that text into the body of the email to save him from the copy/paste. Is there a way to use a variable that contains the contents of the textbox into this line of code, where I have "Contents of textbox" in blue?


VB.NET:
mailto:user@example.com?subject=Message Title & body= [COLOR="#0000CD"]Contents of textbox[/COLOR]
 

BugMan

Well-known member
Joined
Dec 27, 2010
Messages
52
Location
Tallahassee FL
Programming Experience
10+
The code below seems like it would lend itself well to using a variable to put text into the body and subject, but it requires a "From". I won't have that information at run time. I assume there is a way to retrieve the users default email address, though, but is this the best way to go?

Also, in the line "SmtpClient("127.0.0.1")" , is that IP address a standard or does it need to be acquired? I get an error on the last line of code.

thanks! Fun stuff, this emailing from an app!



VB.NET:
 Imports System.Net.Mail
      
  Dim mail As New MailMessage()

        'set the addresses
        mail.From = New MailAddress("me@mycompany.com")
        mail.To.Add("support@bugware.com")

        'set the content
        mail.Subject = "This is an email"
        mail.Body = "this is a sample body"

        'send the message
        Dim smtp As New SmtpClient("127.0.0.1")
        smtp.Send(mail)
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,430
Location
Norway
Programming Experience
10+
I have a code in a textbox that the user previously copy/pasted into an email himself. I'd like my program to put that text into the body of the email to save him from the copy/paste. Is there a way to use a variable that contains the contents of the textbox into this line of code, where I have "Contents of textbox" in blue?


VB.NET:
mailto:user@example.com?subject=Message Title & body= [COLOR="#0000CD"]Contents of textbox[/COLOR]
Concatenation Operators in Visual Basic
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,430
Location
Norway
Programming Experience
10+
SmtpClient works like a standalone email client, all necessary information must be provided to be able to send the email, just like you fill in various information when you set up any other email client. "127.0.0.1" is the address for the SMTP server, which here is the address of localhost and can only be used if the machine sending from hosts a SMTP server. Normally the SMTP server address is provided by the ISP. I guess in these days of Hotmail and Gmail it is possible you have never actually set up a real email program before, but that's how it goes.
 

BugMan

Well-known member
Joined
Dec 27, 2010
Messages
52
Location
Tallahassee FL
Programming Experience
10+
Hmmm, I'm missing the connection between using a variable inside of the MailTo function and concatenation. Could you elaborate just a wee little bit? :)

I'd like to do something like this:


VB.NET:
   Private Sub linklabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles linklabel1.LinkClicked
        ContentOfTextbox = txtUserTextBox.text

        System.Diagnostics.Process.Start("mailto:support@software.com?subject=Hello from BugMan & body = ContentOfTextbox")
    End Sub
Of course, this would just write "ContentOfTextbox" as the body of the email...

thanks much!
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,430
Location
Norway
Programming Experience
10+
Hmmm, I'm missing the connection between using a variable inside of the MailTo function and concatenation. Could you elaborate just a wee little bit?
Didn't you see the code examples where they put strings and variables together to form new concatenated strings?
 

BugMan

Well-known member
Joined
Dec 27, 2010
Messages
52
Location
Tallahassee FL
Programming Experience
10+
Didn't you see the code examples where they put strings and variables together to form new concatenated strings?
Yes, and I understand concatenation... but the code there gave little insight on using concatenation with the MailTo thing. Finally figured it out with a some experimentation... the key is realizing that you can put stuff outside of the quotes and it still will show up in the body... not that intuitive to a hack like me.

Here's the solution if anybody else wants to do this:

VB.NET:
   Private Sub linklblBugWare_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles linklblBugWare.LinkClicked
        Dim CustCode$
        CustCode = txtCustCode.Text
        System.Diagnostics.Process.Start("mailto:support@bugware.com?subject=Hello from BugMan & Body= Customer code: " & CustCode)
    End Sub
Thanks.
 
Top Bottom