Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. Problem Sending an Email from Desktop App

Problem Sending an Email from Desktop App

Scheduled Pinned Locked Moved Windows Forms
helptutorialquestioncsharpjava
6 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    All Time Programming
    wrote on last edited by
    #1

    Hi, In my desktop app developed in VB.NET, I want to send an email.

    Private Sub CreateMessage()
        Dim from As New MailAddress(fromTxt.Text.Trim, nameTxt.Text.Trim)
        Dim toAdd As New MailAddress(toTxt.Text, totitle)
        Dim msg As New MailMessage(from, toAdd)
        msg.Subject = subjectTxt.Text.Trim
        msg.Body = messageTxt.Text.Trim
        Dim cc1 As New MailAddress(cc1.Text)
        Dim cc2 As New MailAddress(cc2.Text)
        msg.CC.Add(cc1)
        msg.CC.Add(cc2)
    
        Dim client As New SmtpClient()
    
        Try
            client.Send(msg)
            ToolStripStatusLabel1.Text = "Message Sent Successfully"
        Catch ex As Exception
            ToolStripStatusLabel1.Text = "Error Sending Mail"
            MessageBox.Show("Error sending mail : " & ex.ToString, "ERROR ::  Try Again", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            cc1 = Nothing
            cc2 = Nothing
            from = Nothing
            toAdd = Nothing
            msg = Nothing
            client = Nothing
        End Try
        Return
    End Sub
    

    Above is the code I use to send email. I have imported System.Net.Mail & set MailMessage object properly also created SmtpClient. But I don't know what and how to set the Smtp host. I mean, the app will be on differnet PC's & all will be using different means of Internet access. Then how can I know and set the host before sending message. I tried adding "localhost" but that didn't worked. What to do and how to achieve the goal ? This should be possible and in a simpler manner - when I can do the same in my Java apps then why not in .NET apps. Searched a lot on net also, but every example showed "", but how can I know the server name of each PC ??? Any help guidance is highly appreciated.

    Thanks & Regards,

    D 1 Reply Last reply
    0
    • A All Time Programming

      Hi, In my desktop app developed in VB.NET, I want to send an email.

      Private Sub CreateMessage()
          Dim from As New MailAddress(fromTxt.Text.Trim, nameTxt.Text.Trim)
          Dim toAdd As New MailAddress(toTxt.Text, totitle)
          Dim msg As New MailMessage(from, toAdd)
          msg.Subject = subjectTxt.Text.Trim
          msg.Body = messageTxt.Text.Trim
          Dim cc1 As New MailAddress(cc1.Text)
          Dim cc2 As New MailAddress(cc2.Text)
          msg.CC.Add(cc1)
          msg.CC.Add(cc2)
      
          Dim client As New SmtpClient()
      
          Try
              client.Send(msg)
              ToolStripStatusLabel1.Text = "Message Sent Successfully"
          Catch ex As Exception
              ToolStripStatusLabel1.Text = "Error Sending Mail"
              MessageBox.Show("Error sending mail : " & ex.ToString, "ERROR ::  Try Again", MessageBoxButtons.OK, MessageBoxIcon.Error)
          Finally
              cc1 = Nothing
              cc2 = Nothing
              from = Nothing
              toAdd = Nothing
              msg = Nothing
              client = Nothing
          End Try
          Return
      End Sub
      

      Above is the code I use to send email. I have imported System.Net.Mail & set MailMessage object properly also created SmtpClient. But I don't know what and how to set the Smtp host. I mean, the app will be on differnet PC's & all will be using different means of Internet access. Then how can I know and set the host before sending message. I tried adding "localhost" but that didn't worked. What to do and how to achieve the goal ? This should be possible and in a simpler manner - when I can do the same in my Java apps then why not in .NET apps. Searched a lot on net also, but every example showed "", but how can I know the server name of each PC ??? Any help guidance is highly appreciated.

      Thanks & Regards,

      D Offline
      D Offline
      DaveAuld
      wrote on last edited by
      #2

      Here is an article Sending Email with Gmail, Yahoo, AOL, and Live Mail Via SMTP[^] that might help, its in C#, but you will get the idea what to do in vb.net as the classes are the same. just change any of the smtp address/ports to whatever server you are using.

      Dave Don't forget to rate messages!
      Find Me On: Web|Facebook|Twitter|LinkedIn

      A 2 Replies Last reply
      0
      • D DaveAuld

        Here is an article Sending Email with Gmail, Yahoo, AOL, and Live Mail Via SMTP[^] that might help, its in C#, but you will get the idea what to do in vb.net as the classes are the same. just change any of the smtp address/ports to whatever server you are using.

        Dave Don't forget to rate messages!
        Find Me On: Web|Facebook|Twitter|LinkedIn

        A Offline
        A Offline
        All Time Programming
        wrote on last edited by
        #3

        Oh Thanks Dave for this. Just wanted to confirm a point and make myself understand properly. So a mail will have From & To. From will be someone who is writing & To will be my mail address (I will receive it). If my mail address i.e. To is of hotmail, then I got to implement of Live Mail, if of Yahoo then of Yahoo & if of gmail then of gmail. Then regardless of what the From address belong. If To is "...@hotmail.com" & From is "...@xyz.com" ; If I implement of Live Mail, then I will receive all mails send from anyone. Am I correct or wrong at any angle. Kindly let me know.

        Thanks & Regards,

        D 1 Reply Last reply
        0
        • D DaveAuld

          Here is an article Sending Email with Gmail, Yahoo, AOL, and Live Mail Via SMTP[^] that might help, its in C#, but you will get the idea what to do in vb.net as the classes are the same. just change any of the smtp address/ports to whatever server you are using.

          Dave Don't forget to rate messages!
          Find Me On: Web|Facebook|Twitter|LinkedIn

          A Offline
          A Offline
          All Time Programming
          wrote on last edited by
          #4

          Thanks Dave, I found my way and also solved the confussion. But am just wondering 1 thing. In my form I already have ToolStripProgressBar. When the Send(mail) is being executed, for a short while I see 2 progress bars. Then 1 disappears. Can't find what is going on. Does Send() shows a progress bar by default - didn't find any such info in MSDN. Can you tell what must be the reason of seeing 2 progress bar for a short while when Smtp.Send(mailMessage) function is being executed. In MSDN I also found that when send is eecutes, it blocks so its safe to set timeout. Can you tell what value should be set in timeout - 100000 (100 secs) is good enough or should be more ! Thanks a lot.

          Thanks & Regards,

          D 1 Reply Last reply
          0
          • A All Time Programming

            Thanks Dave, I found my way and also solved the confussion. But am just wondering 1 thing. In my form I already have ToolStripProgressBar. When the Send(mail) is being executed, for a short while I see 2 progress bars. Then 1 disappears. Can't find what is going on. Does Send() shows a progress bar by default - didn't find any such info in MSDN. Can you tell what must be the reason of seeing 2 progress bar for a short while when Smtp.Send(mailMessage) function is being executed. In MSDN I also found that when send is eecutes, it blocks so its safe to set timeout. Can you tell what value should be set in timeout - 100000 (100 secs) is good enough or should be more ! Thanks a lot.

            Thanks & Regards,

            D Offline
            D Offline
            DaveAuld
            wrote on last edited by
            #5

            Thats good to here. your welcome.

            Dave Don't forget to rate messages!
            Find Me On: Web|Facebook|Twitter|LinkedIn

            1 Reply Last reply
            0
            • A All Time Programming

              Oh Thanks Dave for this. Just wanted to confirm a point and make myself understand properly. So a mail will have From & To. From will be someone who is writing & To will be my mail address (I will receive it). If my mail address i.e. To is of hotmail, then I got to implement of Live Mail, if of Yahoo then of Yahoo & if of gmail then of gmail. Then regardless of what the From address belong. If To is "...@hotmail.com" & From is "...@xyz.com" ; If I implement of Live Mail, then I will receive all mails send from anyone. Am I correct or wrong at any angle. Kindly let me know.

              Thanks & Regards,

              D Offline
              D Offline
              DaveAuld
              wrote on last edited by
              #6

              From Address is the person sending, To address is the receiver, in this case you. The SMTP server needs to be any server for which the user who is sending the email is authorised to use. It doesn't matter who the target (TO) address is, it is dependant on who the sender is. SMTP servers generally need to be authorised, as there are very few open relays now due to the abundance of spam and abuse they receive.

              Dave Don't forget to rate messages!
              Find Me On: Web|Facebook|Twitter|LinkedIn

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups