Hey Guys, I've created a Contact form for my page and I'm having trouble with button click event, which is supposed to take the information from the text boxes and email it. I'm using the System.Net.Mail function. Here's the code
Private Sub SendMail(ByVal from As String, ByVal name As String, ByVal body As String)
Dim mailServerName As String = "SMTP.gmail.com"
Dim message As MailMessage
message.From = New MailAddress(from)
message.To.Add = (New MailAddress(name))
message.CC.Add = New MailAddress("jp@infinitymst.com")
message.Subject = "Thanks For Contacting Us."
message.Body = (body)
Everything is fine except I get an error on (message.to.add) and (message.cc.add) In both cases the error I get is
"overload resolution failed because no accessible add accepts this number of arguments"
I'm having trouble with button click event, which is supposed to take the information from the text boxes and email it
jpleasant
However, is there a way to have a CC or BCc in this function.
According to your description ,I would like to suggest you to check the complete the sample below which shows how to send an email by using System.Net.Mail class.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim content As String = Me.TextBox2.Text
'the content of email.
Dim address As String = Me.TextBox1.Text
'"target_email@hotmail.com"
'the address which you would e-mail to.
Dim ccaddress As String = Me.TextBox3.Text
'the address which you would cc to.
Dim hostid As String = "host_email@hotmail.com"
' the address used to send e-mail
Dim hostname As String = "hostname"
'the name of host
Dim hostpwd As String = "password"
'the password for hostid
Dim hostsmtp As String = "smtp.host.com"
'the SMTP address
Dim hosttitle As String = "HostTitle"
'the title of the e-mail
SendMail(address, ccaddress, hostid, hostname, hostpwd, content, hostsmtp, hosttitle)
'send e-mail;
End Sub
Public Function SendMail(ByVal address As String, ByVal ccaddress As String, ByVal HostID As String, ByVal HostName As String, ByVal HostPwd As String, ByVal Content As String, ByVal HostSmtp As String, ByVal HostTitle As String) As Boolean
Dim msg As New System.Net.Mail.MailMessage()
msg.From = New MailAddress(HostID, HostName, System.Text.Encoding.UTF8)
msg.Subject = HostTitle
'title
msg.SubjectEncoding = System.Text.Encoding.UTF8
'Encoding
msg.Body = Content
'the content of email
msg.BodyEncoding = System.Text.Encoding.UTF8
msg.IsBodyHtml = True
'send the e-mail which format as html
msg.Priority = MailPriority.High
Dim client As New SmtpClient()
client.Credentials = New System.Net.NetworkCredential(HostID, HostPwd)
client.Host = HostSmtp
Try
msg.[To].Add(address)
msg.CC.Add(ccaddress)
client.Send(msg)
msg.[To].Clear()
Catch ex As System.Net.Mail.SmtpException
Return False
End Try
Return True
End Function
Hope it can help you.
Please mark the replies as answers if they help or unmark if not.
Feedback to us
Thanks, this has worked. However, is there a way to have a CC or BCc in this function.
You need to understand the basics here -
You define the mailmessage and the smtpclient. The mailMessage contains all the parts you need for the message (to, from,cc,bcc, subject, body - all properties of the mailmessage object) but you need to add them in somewhere.
If, as above, you create your own subroutine to do it, then you need to add your CC and/or BCC to the arguments, but again, you need to assign them somewhere else, when you call the SendEmail subroutine.
Once you have your mailmessage and SMTP Client defined:
Dim msg as new MailMessage
Dim client as new SMTPClient
Then, you just add in all the pieces you need (to/from/cc/bcc/subject/body)
Once you have your mailmessage pieces all put together, you use the client to send it:
client.Send(msg)
So, you can do it wherever you want - you just have to define and create the parts, and then use them to actually send your message.
Thanks, this has worked. However, is there a way to have a CC or BCc in this function.
You need to understand the basics here -
You define the mailmessage and the smtpclient. The mailMessage contains all the parts you need for the message (to, from,cc,bcc, subject, body - all properties of the mailmessage object) but you need to add them in somewhere.
If, as above, you create your own subroutine to do it, then you need to add your CC and/or BCC to the arguments, but again, you need to assign them somewhere else, when you call the SendEmail subroutine.
Once you have your mailmessage and SMTP Client defined:
Dim msg as new MailMessage
Dim client as new SMTPClient
Then, you just add in all the pieces you need (to/from/cc/bcc/subject/body)
Once you have your mailmessage pieces all put together, you use the client to send it:
client.Send(msg)
So, you can do it wherever you want - you just have to define and create the parts, and then use them to actually send your message.
I didn't realize that I could add those arguments in the Sub or how. I was hoping that I could, which was the basis of my question, but you've explained in great detail. Thanks.
jpleasant
Member
21 Points
37 Posts
Having Trouble with Send Email Code
Dec 29, 2010 03:06 AM|LINK
Hey Guys, I've created a Contact form for my page and I'm having trouble with button click event, which is supposed to take the information from the text boxes and email it. I'm using the System.Net.Mail function. Here's the code
Private Sub SendMail(ByVal from As String, ByVal name As String, ByVal body As String)
Dim mailServerName As String = "SMTP.gmail.com"
Dim message As MailMessage
message.From = New MailAddress(from)
message.To.Add = (New MailAddress(name))
message.CC.Add = New MailAddress("jp@infinitymst.com")
message.Subject = "Thanks For Contacting Us."
message.Body = (body)
Everything is fine except I get an error on (message.to.add) and (message.cc.add) In both cases the error I get is
"overload resolution failed because no accessible add accepts this number of arguments"
Any help is much appreciated.
augustwind
All-Star
35860 Points
4900 Posts
ASPInsiders
Moderator
Re: Having Trouble with Send Email Code
Dec 29, 2010 04:02 AM|LINK
try this format:
message.To.Add(new MailAddress(you@There.com))
All Things Dot Net
Stored Procs and Code in a Flash!
ASP.Net Sitemap Creator
jpleasant
Member
21 Points
37 Posts
Re: Having Trouble with Send Email Code
Dec 29, 2010 04:15 AM|LINK
Thanks. I gave that a shot, and I still got the same error.
Ram Reddy Me...
Star
9604 Points
1314 Posts
Re: Having Trouble with Send Email Code
Dec 29, 2010 04:50 AM|LINK
Not sure, but try changing message.From = New MailAddress(from) also to the above said format.
Abhiram Reddy Mekha
mohsince
Member
132 Points
28 Posts
Re: Having Trouble with Send Email Code
Dec 29, 2010 05:08 AM|LINK
Please use below function it will solve your problem
jpleasant
Member
21 Points
37 Posts
Re: Having Trouble with Send Email Code
Dec 29, 2010 04:28 PM|LINK
Thanks, this has worked. However, is there a way to have a CC or BCc in this function.
Ming Xu - MS...
All-Star
25269 Points
2235 Posts
Microsoft
Re: Having Trouble with Send Email Code
Jan 03, 2011 02:43 AM|LINK
Hi,
According to your description ,I would like to suggest you to check the complete the sample below which shows how to send an email by using System.Net.Mail class.
1.Code in page(.aspx):
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> <script language="javascript" type="text/javascript"> function fnValidate() { //your method for validating the fields return true; } </script> </head> <body> <form id="form1" runat="server"> <div> E-mail:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> Message:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /> CC:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br /> <asp:Button ID="Button1" runat="server" Text="Send" OnClick="Button1_Click" OnClientClick="return fnValidate();" /> </div> </form> </body> </html>2.Code in page(.vb):
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Dim content As String = Me.TextBox2.Text 'the content of email. Dim address As String = Me.TextBox1.Text '"target_email@hotmail.com" 'the address which you would e-mail to. Dim ccaddress As String = Me.TextBox3.Text 'the address which you would cc to. Dim hostid As String = "host_email@hotmail.com" ' the address used to send e-mail Dim hostname As String = "hostname" 'the name of host Dim hostpwd As String = "password" 'the password for hostid Dim hostsmtp As String = "smtp.host.com" 'the SMTP address Dim hosttitle As String = "HostTitle" 'the title of the e-mail SendMail(address, ccaddress, hostid, hostname, hostpwd, content, hostsmtp, hosttitle) 'send e-mail; End Sub Public Function SendMail(ByVal address As String, ByVal ccaddress As String, ByVal HostID As String, ByVal HostName As String, ByVal HostPwd As String, ByVal Content As String, ByVal HostSmtp As String, ByVal HostTitle As String) As Boolean Dim msg As New System.Net.Mail.MailMessage() msg.From = New MailAddress(HostID, HostName, System.Text.Encoding.UTF8) msg.Subject = HostTitle 'title msg.SubjectEncoding = System.Text.Encoding.UTF8 'Encoding msg.Body = Content 'the content of email msg.BodyEncoding = System.Text.Encoding.UTF8 msg.IsBodyHtml = True 'send the e-mail which format as html msg.Priority = MailPriority.High Dim client As New SmtpClient() client.Credentials = New System.Net.NetworkCredential(HostID, HostPwd) client.Host = HostSmtp Try msg.[To].Add(address) msg.CC.Add(ccaddress) client.Send(msg) msg.[To].Clear() Catch ex As System.Net.Mail.SmtpException Return False End Try Return True End FunctionHope it can help you.
Feedback to us
Develop and promote your apps in Windows Store
augustwind
All-Star
35860 Points
4900 Posts
ASPInsiders
Moderator
Re: Having Trouble with Send Email Code
Jan 03, 2011 03:31 AM|LINK
You need to understand the basics here -
You define the mailmessage and the smtpclient. The mailMessage contains all the parts you need for the message (to, from,cc,bcc, subject, body - all properties of the mailmessage object) but you need to add them in somewhere.
If, as above, you create your own subroutine to do it, then you need to add your CC and/or BCC to the arguments, but again, you need to assign them somewhere else, when you call the SendEmail subroutine.
Once you have your mailmessage and SMTP Client defined:
Dim msg as new MailMessage
Dim client as new SMTPClient
Then, you just add in all the pieces you need (to/from/cc/bcc/subject/body)
Once you have your mailmessage pieces all put together, you use the client to send it:
client.Send(msg)
So, you can do it wherever you want - you just have to define and create the parts, and then use them to actually send your message.
You can read this in more details on my site:
http://allthingsdotnet.net/?p=637
All Things Dot Net
Stored Procs and Code in a Flash!
ASP.Net Sitemap Creator
jpleasant
Member
21 Points
37 Posts
Re: Having Trouble with Send Email Code
Jan 05, 2011 02:55 AM|LINK
Hi,
Thanks for the example. I went through this and was still getting the same error. I'll go through it again, maybe I'm missing something thanks. again.
jpleasant
Member
21 Points
37 Posts
Re: Having Trouble with Send Email Code
Jan 05, 2011 02:57 AM|LINK
I didn't realize that I could add those arguments in the Sub or how. I was hoping that I could, which was the basis of my question, but you've explained in great detail. Thanks.