|
-
Jun 13th, 2005, 08:56 PM
#1
Thread Starter
New Member
E-mailing in .NET ...code problem [RESOLVED]
Hi There.
I'm developing this program that lets the user easily send e-mail to a recipient. All of the concept is there, but my code needs a little bit of editing. I'll outline the problems for you once you can take a look at the code. By the way, sorry if there's obvious mistakes...It's late at night and I don't feel well, so cut me some slack:P. And I know this might take a lot of reading through to fix some errors, but I appreciate your time.
Screenshot:
VB Code:
Imports System.Web.Mail
' 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()
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub cmdAddAttach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddAttach.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 cmdRemoveAttach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemoveAttach.Click
If lstAttachment.SelectedIndex > -1 Then
lstAttachment.Items.RemoveAt(lstAttachment.SelectedIndex)
End If
End Sub
Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
Dim Counter As Integer
'Validate the data
If txtSMTPServer.Text = "" Then
MsgBox("Enter the SMTP server info ...!!!", MsgBoxStyle.Information, "Send Email")
Exit Sub
End If
If txtFromEmail.Text = "" Then
MsgBox("Enter your Email address.", MsgBoxStyle.Information, "Send Email")
Exit Sub
End If
If txtTo.Text = "" Then
MsgBox("Enter the Recipient's Email address.", MsgBoxStyle.Information, "Send Email")
Exit Sub
End If
If txtSubject.Text = "" Then
MsgBox("Please enter an e-mail 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 = "\" & txtFromName.Text & "\ <" & txtFromEmail.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
'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)
End Sub
End Class
Ok. Here's a list of the errors I'm getting:
1. It is saying that the following is not in a valid namespace:
VB Code:
Imports System.Web.Mail
' 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()
2. It says that the Namespace or type specified in the Imports "System.Web.Mail" cannot be found.
3. The name "obj" is not declared. What would I declare it as, and where?
4. The name "Mailmsg" is not declared. What would I declare it as, and where?
5. The name "MailFormat is not declared. What would I declare is as, and where?
6. The name "Attachment" is not declared. What would I declare it as, and where?
7. Finally, the name "MailAttachment" is not declared. What would I declare it as, and where?
----------
Can anyone help me? Thanks!
-742617000027
Last edited by 742617000027; Jun 15th, 2005 at 02:57 PM.
-
Jun 13th, 2005, 10:47 PM
#2
Re: Edit this code for me...
Add a reference to System.Web to your project in the Solution Explorer. This will fix most, or possibly all, of your errors.
-
Jun 13th, 2005, 10:47 PM
#3
Re: Edit this code for me...
1. put it inside your form declaration, ie
VB Code:
Public Class Form1
' put varaible declaraions here, and change those ugly DIM statements to PRIVATE
...
2.You might need to add a reference, I dunno cuz I havent used that namespace. Right click on the References node in Solution explorer and click on add reference and then look through that list and find the appropriate dll to add
3-4 should be fixed once 1 is fixed
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Jun 13th, 2005, 10:55 PM
#4
Re: Edit this code for me...
I was editing my post when MrPolite replied, and he pointed out what I was changing: All variables must be declared inside a class or module. As he said, only use "Dim" when declaring a local, or procedure-level, variable.
-
Jun 14th, 2005, 04:00 AM
#5
Re: Edit this code for me...
 Originally Posted by jmcilhinney
I was editing my post when MrPolite replied, and he pointed out what I was changing: All variables must be declared inside a class or module. As he said, only use "Dim" when declaring a local, or procedure-level, variable.
actually, DONT use dim when you're declaring class level variables. It's better to define the scope (ie, use Private count As Integer if you're declaring it at the top of your form, and not Dim count...)
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Jun 14th, 2005, 04:26 AM
#6
Re: Edit this code for me...
 Originally Posted by MrPolite
actually, DONT use dim when you're declaring class level variables. It's better to define the scope (ie, use Private count As Integer if you're declaring it at the top of your form, and not Dim count...)
Two things:
1. Read my post and you'll notice that I wrote ONLY use "Dim" to decalare LOCAL variables.
2. Using "Dim" to declare a class-level variable has the same effect as declaring it Private anyway.
-
Jun 14th, 2005, 04:45 AM
#7
Re: Edit this code for me...
Its good practice to only use Dim within methods. Private means Private and code clarity is important.
-
Jun 14th, 2005, 07:48 PM
#8
Thread Starter
New Member
Re: Edit this code for me...
Thanks for all your help folks. I'm still having a couple of problems, though.
First of all, where I do I put the:
I put it in the Form load, but it says it must precede any declarations. Also, for the following:
VB Code:
' Variable which will send the mail
Private obj As System.Web.Mail.SmtpMail
'Variable to store the attachments
Private Attachment As System.Web.Mail.MailAttachment
'Variable to create the message to send
Private Mailmsg As New System.Web.Mail.MailMessage()
the System.Web.Mail.SmtpMail, System.Web.Mail.MailAttachment, and New System.Web.Mail.MailMessage are not defined. How do I fix this?
Finally, it says that "MailFormat" is not declared. What and where would I declare it as?
Thanks again for the help.
-
Jun 14th, 2005, 08:11 PM
#9
Re: E-mailing in .NET ...code problem
All "Imports" statements go at the very top of the class file, before any other lines of code. Once you fix that I think you'll find that all the other issues will disappear.
Edit:
You have added a reference to System.Web to your project in the Solution Explorer, right?
-
Jun 15th, 2005, 12:44 PM
#10
Thread Starter
New Member
Re: E-mailing in .NET ...code problem
jmcilhinney -
First of all, thank you (and everyone else who posted) for all your help. I've added the to the top of the class file. However, I'm not sure how to add a reference to System.Web.Mail to my Solution Explorer (I know what the Explorer is, but I'm unsure how to add the reference). Perhaps you could help me with this? Thanks!
-
Jun 15th, 2005, 01:11 PM
#11
Re: E-mailing in .NET ...code problem
There is a "References" folder, righ click and select add reference, find System.Web in the list, and hit ok.
-
Jun 15th, 2005, 02:51 PM
#12
Thread Starter
New Member
Re: E-mailing in .NET ...code problem
Thanks for the help..One more problem, and then it should be working fine.
In the following code:
VB Code:
' Variable which will send the mail
Private obj As System.Web.Mail.SmtpMail
'Variable to store the attachments
Private Attachment As System.Web.Mail.MailAttachment
'Variable to create the message to send
Private Mailmsg As New System.Web.Mail.MailMessage()
it says that System.Web.Mail.SmtpMail, MailAttachment, and MailMessage are all obsolete because the class is depreciated, and to use System.Net.Mail.
I tried using , and renamed everything to System.Net.Mail.Stmp, MailAttachment, MailMessage. When I did that, it said each type was not defined, even after using the new Imports statement.
Once this problem is fixed, my program should work fine. Can anyone help?
Thanks again to everyone, I very much appreciate it.
-
Jun 15th, 2005, 02:56 PM
#13
Thread Starter
New Member
Re: E-mailing in .NET ...code problem
Nevermind, fixed the problem.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|