Results 1 to 13 of 13

Thread: E-mailing in .NET ...code problem [RESOLVED]

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    8

    Exclamation 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:
    1. Imports System.Web.Mail
    2.  
    3. ' Variable which will send the mail
    4. Dim obj As System.Web.Mail.SmtpMail
    5.  
    6. 'Variable to store the attachments
    7. Dim Attachment As System.Web.Mail.MailAttachment
    8.  
    9. 'Variable to create the message to send
    10. Dim Mailmsg As New System.Web.Mail.MailMessage()
    11. Public Class Form1
    12.  
    13.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    14.  
    15.     End Sub
    16.  
    17.     Private Sub cmdAddAttach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddAttach.Click
    18.         'Show open dialogue box to select the files to attach
    19.         Dim Counter As Integer
    20.         OFD.CheckFileExists = True
    21.         OFD.Title = "Select file(s) to attach."
    22.         OFD.ShowDialog()
    23.  
    24.         For Counter = 0 To UBound(OFD.FileNames)
    25.             lstAttachment.Items.Add(OFD.FileNames(Counter))
    26.         Next
    27.     End Sub
    28.  
    29.     Private Sub cmdRemoveAttach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemoveAttach.Click
    30.         If lstAttachment.SelectedIndex > -1 Then
    31.             lstAttachment.Items.RemoveAt(lstAttachment.SelectedIndex)
    32.         End If
    33.     End Sub
    34.  
    35.     Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
    36.         Dim Counter As Integer
    37.  
    38.         'Validate the data
    39.         If txtSMTPServer.Text = "" Then
    40.             MsgBox("Enter the SMTP server info ...!!!", MsgBoxStyle.Information, "Send Email")
    41.             Exit Sub
    42.         End If
    43.  
    44.         If txtFromEmail.Text = "" Then
    45.             MsgBox("Enter your Email address.", MsgBoxStyle.Information, "Send Email")
    46.             Exit Sub
    47.         End If
    48.  
    49.         If txtTo.Text = "" Then
    50.             MsgBox("Enter the Recipient's Email address.", MsgBoxStyle.Information, "Send Email")
    51.             Exit Sub
    52.         End If
    53.  
    54.         If txtSubject.Text = "" Then
    55.             MsgBox("Please enter an e-mail subject.", MsgBoxStyle.Information, "Send Email")
    56.             Exit Sub
    57.         End If
    58.  
    59.         'Set the properties
    60.         'Assign the SMTP server
    61.         obj.SmtpServer = txtSMTPServer.Text
    62.         'Multiple recepients can be specified using ; as the delimeter
    63.         'Address of the recipient
    64.         Mailmsg.To = txtTo.Text
    65.  
    66.  
    67.         'Your From Address
    68.         'You can also use a custom header Reply-To for a different replyto address
    69.         Mailmsg.From = "\" & txtFromName.Text & "\ <" & txtFromEmail.Text & ">"
    70.  
    71.  
    72.         'Specify the body format
    73.         If chkFormat.Checked = True Then
    74.             Mailmsg.BodyFormat = MailFormat.Html   'Send the mail in HTML Format
    75.         Else
    76.             Mailmsg.BodyFormat = MailFormat.Text
    77.         End If
    78.  
    79.         'If you want you can add a reply to header
    80.         'Mailmsg.Headers.Add("Reply-To", "[email protected]")
    81.         'custom headersare added like this
    82.         'Mailmsg.Headers.Add("Manoj", "TestHeader")
    83.  
    84.         'Mail Subject
    85.         Mailmsg.Subject = txtSubject.Text
    86.  
    87.         'Attach the files one by one
    88.         For Counter = 0 To lstAttachment.Items.Count - 1
    89.             Attachment = New MailAttachment(lstAttachment.Items(Counter))
    90.             'Add it to the mail message
    91.             Mailmsg.Attachments.Add(Attachment)
    92.         Next
    93.  
    94.         'Mail Body
    95.         Mailmsg.Body = txtMessage.Text
    96.         'Call the send method to send the mail
    97.         obj.Send(Mailmsg)
    98.     End Sub
    99. 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:
    1. Imports System.Web.Mail
    2.  
    3. ' Variable which will send the mail
    4. Dim obj As System.Web.Mail.SmtpMail
    5.  
    6. 'Variable to store the attachments
    7. Dim Attachment As System.Web.Mail.MailAttachment
    8.  
    9. 'Variable to create the message to send
    10. 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.

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

    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.

  3. #3
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Edit this code for me...

    1. put it inside your form declaration, ie
    VB Code:
    1. Public Class Form1
    2.  ' put varaible declaraions here, and change those ugly DIM statements to PRIVATE
    3. ...
    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!!

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

    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.

  5. #5
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Edit this code for me...

    Quote 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!!

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Edit this code for me...

    Quote 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.

  7. #7
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Edit this code for me...

    Its good practice to only use Dim within methods. Private means Private and code clarity is important.

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    8

    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:
    VB Code:
    1. Imports System.Web.Mail
    I put it in the Form load, but it says it must precede any declarations. Also, for the following:
    VB Code:
    1. ' Variable which will send the mail
    2.     Private obj As System.Web.Mail.SmtpMail
    3.  
    4.     'Variable to store the attachments
    5.     Private Attachment As System.Web.Mail.MailAttachment
    6.  
    7.     'Variable to create the message to send
    8.     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.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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?

  10. #10

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    8

    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
    VB Code:
    1. Imports System.Web.Mail
    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!

  11. #11
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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.

  12. #12

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    8

    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:
    1. ' Variable which will send the mail
    2.     Private obj As System.Web.Mail.SmtpMail
    3.  
    4.     'Variable to store the attachments
    5.     Private Attachment As System.Web.Mail.MailAttachment
    6.  
    7.     'Variable to create the message to send
    8.     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
    VB Code:
    1. Imports System.Net.Mail
    , 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.

  13. #13

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    8

    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
  •  



Click Here to Expand Forum to Full Width