Problem Sending an Email from Desktop App
-
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,
-
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,
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 -
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|LinkedInOh 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,
-
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|LinkedInThanks 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,
-
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,
-
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,
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