I have this in a VB.NET contact form:
Protected Sub btnSubmit_Click1(sender As Object, e As EventArgs)
Dim mail As New MailMessage()
mail.To.Add(txtEmail.Text)
mail.From = New MailAddress("info#mySite.net")
mail.Subject = "Contact Form"
'mail.Bcc = New MailAddress
mail.Body = txtName.Text & vbCrLf & txtComments.Text
Dim smtp As New SmtpClient("mail.server")
smtp.Credentials = New NetworkCredential("EMAIL ID", "PWD")
smtp.Send(mail)
Response.Redirect("thankyou.aspx" & mail.To)
lblStatus.Text = "Your data has been submitted successfully"
txtName.Text = ""
txtEmail.Text = ""
txtComments.Text = ""
End Sub
The mail.To in Response.Redirect is giving me an error:
Operator '&' is not defined for types 'String' and 'System.Net.Mail.MailAddressCollection'.
I imagine I need to create a separate personalised aspx 'thank you' page, so I need a way of getting the user's name from the form field data.
Thanks for any advice.
Do you mean something like this:
Response.Redirect("thankyou.aspx" & Request.QueryString["email.Text"];
Mail.To is a collection. Try email.text (which is what you passed to the mail address).
Related
I'm adding an email sender in my app, so i used this:
Try
Dim oMail As New SmtpMail("TryIt")
Dim oSmtp As New SmtpClient()
oMail.From = "app-NHK#hotmail.com" ' From
oMail.To = "NHKomaiha#hotmail.com" ' To
oMail.Subject = Title.Text 'Title
oMail.TextBody = MsgTxt.Text 'Body
Dim oServer As New SmtpServer("smtp.live.com") ' SMTP server address
oServer.User = "app-NHK#hotmail.com" 'here i have written my app's email address made for sending the email from this form
oServer.Password = "thepassword" 'here i have put my app email password
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto ' if SSL connection required
UseWaitCursor = True
Here done setting the main needed info
oSmtp.SendMail(oServer, oMail)
Sending...
UseWaitCursor = False
MessageBox.Show("E-Mail Sent Successfully", "Contact by E-Mail", MessageBoxButtons.OK, MessageBoxIcon.Information)
Main.BringToFront()
Main.Enabled = True
Close()
Error catching...
Catch ep As Exception
UseWaitCursor = False
MessageBox.Show("Error while sending E-Mail." & vbCrLf & vbCrLf & ep.Message, "Contact by E-Mail", MessageBoxButtons.OK, MessageBoxIcon.Error)
Dim closeerror = MessageBox.Show("Do you want to close?", "Contact by E-Mail", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If closeerror = DialogResult.Yes Then
Main.BringToFront()
Main.Enabled = True
Close()
End If
End Try
Is this code wrong? i used a lot of ways to send email but none worked
This time i got error: 550 5.3.4 Requested action not taken; To continue sending messages, please sign in to your account.
Modify and try this working example:
Imports System.Net.Mail
...
Try
Dim Email As New MailMessage()
Email.From = New MailAddress("abcdef#gmail.com")
Email.To.Add("other#provider.com")
Email.Subject = "Subject"
Email.IsBodyHtml = False 'or true if you want html
Email.Body = TextBox1.Text
Dim EmailClient As New SmtpClient("smtp.gmail.com", 587)
EmailClient.EnableSsl = True
EmailClient.Credentials = New Net.NetworkCredential("abcdef#gmail.com", "password")
EmailClient.Timeout = 7000
EmailClient.Send(Email)
Catch ex As SmtpException
MsgBox(ex.StatusCode & vbCrLf & ex.Message, vbCritical, "SMTP Error!")
End Try
Usually you need to specify port and authentication type in order to connect to an smtp server. It seems that smtp.live.com use SSL and port 465 (please verify this data).
So you can use SmtpClient.Port property to sets the port used for SMTP transactions and SmtpClient.EnableSSL to specify that SmtpClient uses Secure Sockets Layer (SSL) to encrypt the connection.
I am trying to send a mail to more than one person at a time.
My code is like this;
Dim SmtpServer As New SmtpClient()
SmtpServer.Credentials = New Net.NetworkCredential("jasibs002#gmail.com", "solutions")
SmtpServer.Port = 25
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.EnableSsl = True
Dim omail As New MailMessage()
omail.From = New MailAddress("jasibs002#gmail.com", "JaseemBinBacker", System.Text.Encoding.UTF8)
omail.Subject = "Test Mail"
Dim str As String
str = "Hai How Are You I am Sendig This Mail for Testing"
str = str + vbNewLine & "Checking"
str = str + vbNewLine & "Sucess"
omail.Body = str
Dim email As String
Dim cmdemail As New SqlCommand("SELECT Emailid FROM dbo.Email_tbl", con.connect)
dr = cmdemail.ExecuteReader
While dr.Read
email = dr("Emailid")
omail.To.Add(email)
SmtpServer.SendAsync(omail, Nothing)
End While
dr.Close()
con.disconnect()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
While executing this, I am getting the following error;
An asynchronous call is already in progress. It must be completed or canceled before you can call this method.
My Email Table has more than 10 email ids.
I want send this email at the same, how I can do this?
I am trying to send a mail to more than one person at a time.
My code is like this;
Dim SmtpServer As New SmtpClient()
SmtpServer.Credentials = New Net.NetworkCredential("jasibs002#gmail.com", "someMadeUpPassword")
SmtpServer.Port = 25
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.EnableSsl = True
Dim omail As New MailMessage()
omail.From = New MailAddress("jasibs002#gmail.com", "JaseemBinBacker", System.Text.Encoding.UTF8)
omail.Subject = "Test Mail"
Dim str As String
str = "Hai How Are You I am Sendig This Mail for Testing"
str = str + vbNewLine & "Checking"
str = str + vbNewLine & "Sucess"
omail.Body = str
Dim email As String
Dim cmdemail As New SqlCommand("SELECT Emailid FROM dbo.Email_tbl", con.connect)
dr = cmdemail.ExecuteReader
While dr.Read
email = dr("Emailid")
omail.To.Add(email)
End While
dr.Close()
con.disconnect()
SmtpServer.SendAsync(omail, Nothing)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
While executing this, I am getting the following error;
An asynchronous call is already in progress. It must be completed or canceled before you can call this method.
My Email Table has more than 10 email ids.
Change your While loop to:
While dr.Read
email = dr("Emailid")
omail.To.Add(email)
End While
SmtpServer.SendAsync(omail, Nothing)
Can any body help me out in VB.Net [Using Microsoft Visual Basic 2010 Express] that how to send DataGridView [Records/Data has been fetched from Database] information via email using VB.Net. I am able to send email with DataGrid information but not in Grid/Table form. Below is sample Code:
Private Sub btnGUIEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGUIEmail.Click
Dim html As String = String.Empty
Dim Mail As New MailMessage
mail.Subject = "test email"
mail.To.Add(TextBox2.Text)
mail.From = New MailAddress("X#gmail.com")
Dim strAccount_No, strClient_Name As String
Dim i As Integer
For i = 0 To DataGridView1.Rows.Count - 2
strAccount_No = DataGridView1.Item(0, i).Value.ToString
strClient_Name = DataGridView1.Item(1, i).Value.ToString
strMailBody += strAccount_No & " " & strClient_Name & vbCrLf
Mail.Body = strMailBody
Next
Dim SMTP As New SmtpClient("smtp.X.com")
SMTP.EnableSsl = True
SMTP.Credentials = New System.Net.NetworkCredential("X#gmail.com", "X")
SMTP.Port = "587"
SMTP.Send(Mail)
MsgBox("Email Sent")
End Sub
Can any body help me out in VB.Net [Using Microsoft Visual Basic 2010 Express] that how to send DataGridView [Records/Data has been fetched from Database] information via email using VB.Net. I am able to send email with DataGrid information but not in Grid/Table form. Below is sample Code:
If you want to send it in table format, you have to create the information in the html format using the table tags.
for eg.
mail.BodyFormat = MailFormat.Html
mail.Body = "this is my test email body.<br><b>this part is in bold</b>"
Just go through below links, it may help you to understand how?
http://www.emailarchitect.net/easendmail/kb/vbnet.aspx?cat=6
http://www.aspsnippets.com/Articles/Create-and-send-HTML-Formatted-Emails-in-ASP.Net-using-C-and-VB.Net.aspx
I hope it will help you.. :)
I can use this code to send an email on my Exchange server
Try
Dim SmtpServer As New SmtpClient
Dim mail As New MailMessage
SmtpServer.Credentials = New Net.NetworkCredential()
SmtpServer.Port = 25
SmtpServer.Host = "email.host.com"
mail = New MailMessage
mail.From = New MailAddress("myemail#email.com")
mail.To.Add("otheremail#email.com")
mail.Subject = "Equipment Request"
mail.Body = "This is for testing SMTP mail from me"
SmtpServer.Send(mail)
catch ex As Exception
MsgBox(ex.ToString)
End Try
But how can I add multiple lines to the body?
Just treat it like a normal text object where you can use Environment.NewLine or vbNewLine between sentences.
StringBuilder is useful here:
Dim sb As New StringBuilder
sb.AppendLine("Line One")
sb.AppendLine("Line Two")
mail.Body = sb.ToString()
I would create a variable for your body and then add that to the mail.Body so it would look something like this.
Try
Dim strBody as string = ""
Dim SmtpServer As New SmtpClient
Dim mail As New MailMessage
SmtpServer.Credentials = New Net.NetworkCredential()
SmtpServer.Port = 25
SmtpServer.Host = "email.host.com"
mail = New MailMessage
mail.From = New MailAddress("myemail#email.com")
mail.To.Add("otheremail#email.com")
mail.Subject = "Equipment Request"
strBody = "This is for testing SMTP mail from me" & vbCrLf
strBody += "line 2" & vbCrLf
mail.Body = strBody
SmtpServer.Send(mail)
catch ex As Exception
MsgBox(ex.ToString)
End Try
That will append the line breaks and you should have each line on it's own in the email.
If the body of your message needs to be in HTML format, add the <br> tags right in your String. vbCrLf and StringBuilder don't work if the body is in HTML format.
Dim mail As New MailMessage
mail.IsBodyHtml = True
mail.Body = "First Line<br>"
mail.Body += "Second Line<br>"
mail.Body += "Third Line"
If it is not in HTML format, the other answers here appear to be good.
Like this?
Dim myMessage as String = "This is for testing SMTP mail from me" + Environment.NewLine
myMessage = myMessage + "Line1" + Environment.NewLine
then
mail.Body = myMessage
try the system.environment.newline in the the string ... should work
What it works for me is using in the string..
strBody = "This is for testing SMTP mail from me<BR> line 2"