Emailing to address work...just not how I want it to...

mouse51180

Member
Joined
Jan 25, 2013
Messages
9
Programming Experience
Beginner
So I have a program and it emails fine if you hard code the email To: address in.

Example:

VB.NET:
oMsg.To = "Joe.Blow@Widgets.com"

I would like to pull the username in from a text box on another form and add the domain info manually

So I tried this, but it doesnt seem to work.
VB.NET:
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]
oMsg.To = Form2.txtEmail.Text & [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]@Widgets.com"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]

 
Are you missing a quotation mark (") before the @ symbol?

No...im sorry...I just left that out in my forum posting when I was taking out my company info and adding WIDGETS in.

The actual code does have the "...the text color is red like it should be for items in quotes.


oMsg.To = Form2.txtEmail.Text & "@Widgets.com"
 
Hi,

There is nothing that you have posted to suggest that what you have would not work since as long as Form2.txtEmail.Text has a valid string to represent a valid email then this would work.

VB.NET:
oMsg.To = Form2.txtEmail.Text & "@Widgets.com"

Therefore, please explain in detail what error message you are getting and where you are getting this error since saying "but it doesnt seem to work" does not exactly give anyone much to go on.

Cheers,

Ian
 
Hi,

There is nothing that you have posted to suggest that what you have would not work since as long as Form2.txtEmail.Text has a valid string to represent a valid email then this would work.

VB.NET:
oMsg.To = Form2.txtEmail.Text & "@Widgets.com"

Therefore, please explain in detail what error message you are getting and where you are getting this error since saying "but it doesnt seem to work" does not exactly give anyone much to go on.

Cheers,

Ian

Sorry for lack of error info. Here is the error it spits out....

COMException was unhabdled
Outlook does not recognize one or more names.

System.Runtime.InteropServices.COMException was unhandled
ErrorCode=-2147467259
Message=Outlook does not recognize one or more names.
Source=Microsoft Outlook
StackTrace:
at Microsoft.Office.Interop.Outlook._MailItem.Send()
at WindowsApplication1.ITRequestForm.btnSubmit_Click(Object sender, EventArgs e) in C:\Users\mmaus\documents\visual studio 2010\Projects\ITRequestForm\ITRequestForm\ITRequestForm.vb:line 75
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
 
Hi,

Not sure what to advise here. As you can see for yourself, this is not an exception in your coding from VS, but an exception returned from MS Outlook.

The best that I can suggest at the moment is to manually take each email addresses you have build in your VS project and test them individually with MS Outlook until you can narrow down what your issue is.

Not much to go on I know, but that is the best I can offer at this stage.

Cheers,

Ian
 
Hi,

Not sure what to advise here. As you can see for yourself, this is not an exception in your coding from VS, but an exception returned from MS Outlook.

The best that I can suggest at the moment is to manually take each email addresses you have build in your VS project and test them individually with MS Outlook until you can narrow down what your issue is.

Not much to go on I know, but that is the best I can offer at this stage.

Cheers,

Ian

Yea the error description is pretty weak. Is there a way in the debug procedure to see the address it is trying to send to?

I only have 1 email address the program should email and I dont hard code it in just incase the email need to change in the futre. If I hard code the address in, it works...
 
What is the value of Form2.txtEmail.Text? If you have hard-coded this and it works, then as IanRyder stated, there doesn't appear to be anything that wouldn't make this work if the same value is created when you concatenate the passed in Text property with "@Widgets.com". Can you set a breakpoint on the line just after oMsg.To = .... and see what the value of oMsg.To is.
 
The value of Form2.txtEmail.Text is the user name...in this case it is "Support" This is read from a saved file and I am wondering if that is where my problem occurs. If I hard code "Support@Widgets.com"..works, but with the code above I think its not pulling in the "Support" part.

I dont know how to set a breakpoint to check the To box...is this a debug option or is this something I would have to do like spit a msg box out before the send message actually goes?
 
To set a breakpoint in Visual Studio 2008/2010 (probably the same for other versions), right click on the line where you want to set the breakpoint and choose the Breakpoint > Insert Breakpoint command. When you debug via Visual Studio (starting program by pressing F5) it will stop at the breakpoint and you can hover your mouse over the oMsg.To text and it should show you the value.
 
To set a breakpoint in Visual Studio 2008/2010 (probably the same for other versions), right click on the line where you want to set the breakpoint and choose the Breakpoint > Insert Breakpoint command. When you debug via Visual Studio (starting program by pressing F5) it will stop at the breakpoint and you can hover your mouse over the oMsg.To text and it should show you the value.

Ok...making progress I think.

Got the breakpoint to work. Shows the the To: field is only pulling up the "@Widgets.com" and nothing before it. So I am guessing that the code I have to fill the Form2.txtBox.text is not doing so before the item is run..

This is probably just my own lack of coding skills.

I have a stream reader to fill the txtbox on form2, but I guess after that form closes the text box clears. Here is the code:
VB.NET:
[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Private
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] Form2_Load(sender [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] System.[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]Object[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2], e [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] System.[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]EventArgs[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]) [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2].Load
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] R [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] System.IO.[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]StreamReader[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2](FileName)[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]
        txtbox.Text = R.ReadToEnd
        R.Close()  
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]

So im guessing I have two options... try to code something to pass the txtbox.text variable over to Form 1 as a different variable...or write another stream reader code to re-read the file during the send processes...

or is there a better way of doing this?

Thanks for all the help...I have had no formal code training and learning this on my own.
I really appreciate the help.
 
It sounds like you've identified your issue. A textbox on a form will no longer be accessible once you close the form. There are several ways to do this. One crude way of doing this is to define a variable/property in your application that is not directly tied to the forms and just before you close Form2, store the value in the textbox to this variable. Now wherever you are generating the email address, you should be able to access the data that you stored from the textbox.

The reason that I say that this method is crude is that the use of global/shared variables is dangerous since they can be modified from anywhere and over the life of a project different people working on it may make changes that use global/shared variables without realizing that their use is breaking something else that already exists.
 
It sounds like you've identified your issue. A textbox on a form will no longer be accessible once you close the form. There are several ways to do this. One crude way of doing this is to define a variable/property in your application that is not directly tied to the forms and just before you close Form2, store the value in the textbox to this variable. Now wherever you are generating the email address, you should be able to access the data that you stored from the textbox.

The reason that I say that this method is crude is that the use of global/shared variables is dangerous since they can be modified from anywhere and over the life of a project different people working on it may make changes that use global/shared variables without realizing that their use is breaking something else that already exists.

What would be the prefered way of accomplishing what I am trying to do?
 
A better way to do this without global/shared variables depends on the overall situation that you have. As an example, let's assume that you have two forms Form1 and Form2. Form1 is your main form and is where you will send the email from, while Form2 collects the email address. In this case you would define a property on Form1 that will hold the email address collected on Form2...for example

Friend Property EmailAddress As String

Now after you instantiate an instance of Form2 in your Form1 code you can set the Owner property of Form2 to Form1, then show Form2 by doing the following.

Dim f2 as New Form2()
f2.Owner = Me
f2.ShowDialog()

Now in Form2, when you are ready to save the input email address (you can do this as a result of a button click, as Form2 is closing, etc) you can save it in the property that is defined in Form1 by doing the following in your Form2 code.

TryCast(Me.Owner, Form1).EmailAddress = Me.TextBox1.Text

Now the value has been stored in Form1 and closing Form2 will not lose the value. This now ensures that the email address field is unique to this instance of Form1 and less likely to be changed by other code in your application. If you are not sending the e-mail from within Form1, then once you close Form1 you are again going to lose the data.
 
Back
Top