Adding multiple lines to body of SMTP email VB.NET - vb.net

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"

Related

What is the best way to send emails via VB using O365 domain?

I'm trying to send emails automatically, the configuration below worked perfectly with Gmail, but now with office365. I can't send the emails, I need help.
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.UseDefaultCredentials = False
SmtpServer.Credentials = New Net.NetworkCredential("xxx#xx.com", "xxxx")
SmtpServer.Port = 587
SmtpServer.Host = "smtp.office365.com"
SmtpServer.EnableSsl = True
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
mail = New MailMessage()
mail.From = New MailAddress("xxx#xx.com")
mail.To.Add(listaCorreo)
mail.Subject = " string "
mail.Body = "string "
mail.IsBodyHtml = True
SmtpServer.Send(mail)
Catch ex As Exception
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine("Class -\> ClassProcessingError, Method -\> MyContactByMail, Error -\> " & ex.Message)
End Try

VB.Net - Sending out mail as html and plaintext

I have a mail function 'Sendmail' in my VB app, as so...
Public Function Sendmail(ByVal mailrecipient As String, ByVal mailsubject As String, ByVal mailbody As String)
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New _
Net.NetworkCredential(internal_mail_server_username, internal_mail_server_password)
SmtpServer.Port = 25
SmtpServer.Host = internal_mail_server
mail = New MailMessage()
mail.From = New MailAddress(internal_email_sender)
mail.To.Add(mailrecipient)
mail.Subject = mailsubject
mail.IsBodyHtml = True
mail.Body = mailbody
SmtpServer.Send(mail)
MessageBox.Show("Mail successfully sent to " & mailrecipient)
Return "Success"
Catch ex As Exception
End Try
End If
End Function
This works great, passing the recipient, subject and body to it sends HTML mail out... fantastic.
What I need to is include with that email a plain text version with the mail that goes out.
Is there a simple way I can achieve this?
Use alterativeViews
'first create the Plain Text part
Dim plainView As AlternateView = AlternateView.CreateAlternateViewFromString(Plain_Text)
'then create the Html part
Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(HTML_Text)
mail.AlternateViews.Add(plainView)
mail.AlternateViews.Add(htmlView)
Obviously you need to pass both the PLain_Text and HTML_Text as parameters to the routine.

An asynchronous call is already in progress. It must be completed or canceled in vb.net

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?

not able to send more than one mail in vb.net

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)

SMTP BCC feature in VB.net

I am looking to add BCC mail feature in the following code. How can I make changes in the following code to send an auto email to more than one email's
Dim SMTPServer As New SmtpClient()
Dim Mail As New MailMessage()
SMTPServer.Credentials = New Net.NetworkCredential(" ")
SMTPServer.Port = 587
SMTPServer.Host = " "
Mail = New MailMessage
Mail.From = New MailAddress(" ")
Mail.To.Add(dr("").ToString)
Mail.Subject = "Patient Assignment"
Mail.Body = " "
SMTPServer.Send(Mail)