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.
Related
I am sending email using smtp client from office 365 account . below is the code that i am using to send the email. The email is going to the JUNk folder in outlook and also the From address is showing as EmployeeName# <yyy.com noreply#xxx.com> instead of EmployeeName#yyy.com <noreply#xxx.com>. So can any one please give me any solution to fix this issue
Dim client As New SmtpClient()
Dim message As New MailMessage()
Dim par_ReplyName As String = "noreply#xxx.com"
Dim par_ReplyAddress As String = "EmployeeName#yyy.com"
message.From = New MailAddress(par_ReplyName, par_ReplyAddress)
message.ReplyToList.Add(par_ReplyAddress)
message.Priority = MailPriority.Normal
message.Subject = "Test Email"
message.Body = "some HTML content"
message.IsBodyHtml = True
client.Host = "smtp.office365.com"
client.Port = 587
client.Credentials = New System.Net.NetworkCredential(smtpUser, smtpPassword)
client.EnableSsl = True
client.Send(message)
Here's how you're using the MailAddress constructor:
message.From = New MailAddress(par_ReplyName, par_ReplyAddress)
Here's the declaration for that constructor from the documentation:
'Declaration
Public Sub New ( _
address As String, _
displayName As String _
)
Do you see anything amiss there, like maybe you have the arguments the wrong way around?
I have a function to send an email, however I am unable to call it in a sub on the same form.
Imports System.Net.Mail
Public Function SendEmail(EmailBody As String,
EmailSubject As String, EmailTo As String,
AttachmentPath As String, EmailAsHTML As Boolean)
Dim Mail As New MailMessage
Try
Dim SMTP As New SmtpClient("smtp.gmail.com")
SMTP.EnableSsl = True
SMTP.Credentials = New System.Net.NetworkCredential("[your gmail address#gmail.com]", "[the associated password]")
SMTP.Port = 587
Mail.From = New MailAddress("""[Friendly Name]"" <[your gmail address#gmail.com>")
'Split Multiple Addresses
If EmailTo.Contains(";") Then
For Each EmailAddress In EmailTo.Split(";")
Mail.To.Add(Trim(EmailAddress))
Next
Else
Mail.To.Add(Trim(EmailTo))
End If
Mail.Subject = EmailSubject
Mail.Body = EmailBody
If AttachmentPath <> "" Then Mail.Attachments.Add(New Mail.Attachment(AttachmentPath))
Mail.IsBodyHtml = EmailAsHTML
SMTP.Send(Mail)
'Clear Mail Object
Mail.Dispose()
'Function Return
Return True
Catch ex As Exception
'Function Return
Return False
End Try
End Function
Any suggestions as to what the correct code it to call this sub?!
I wanna send an Email from my Webservice, written in vb.net, but I get an Timeout.
What is wrong?
Dim toaddress As MailAddress = New MailAddress("xxx")
Dim fromaddress As MailAddress = New MailAddress("yyy")
' The structure for MailMessage(from, to)
Dim message As MailMessage = New MailMessage(fromaddress, toaddress)
message.Subject = "I have sent you a message from a program!"
message.Body = "Hello World!"
Dim messanger As SmtpClient = New SmtpClient("smtpxxx", 995)
messanger.Credentials = New NetworkCredential("user", "password")
messanger.EnableSsl = True
messanger.Send(message)
You are doing many things wrong and some of the information you have given are not clear enough. host name and port names have to give properly. it chooses which mail service you are using, from your code it is not correctly provided, so i will give a snippet that uses gmail to send mail. please go through this and make changes as per your SmtpHost.
Try
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential("email", "password")
Smtp_Server.Port = 587
Smtp_Server.EnableSsl = True
Smtp_Server.Host = "smtp.gmail.com"
e_mail = New MailMessage()
e_mail.From = New MailAddress(txtemail.Text)
e_mail.To.Add(txtemail.Text)
e_mail.Subject = "Email Sending"
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text
Smtp_Server.Send(e_mail)
MsgBox("Mail Sent")
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
This link help you to find the smtp.Server and Port numbers, you can use this thread for check the limitations of smtp.servers
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"
I have two pages the first one will receive all data that are required from the user such as username e.mail and then the same page will do the calculation but when clicking on result button two things should happen: 1-the result and all user inputs should be sent to the second page 2- the same information should be sent to the user email as the e.mail messege
I wrote the e.mail code but I dont know how do put the results as message
Help please
This code worked for me
Try
Dim mail As MailMessage
Dim client As SmtpClient
Dim fromAddress As String
Dim toAddress As String
Dim subject As String
Dim message As String
fromAddress = "user#hotmail.com"
toAddress = "user#hotmail.com"
subject = "Radiation"
message = "mail sent"
mail = New MailMessage(fromAddress, toAddress, subject, message)
client = New SmtpClient("Smtp.live.com")
client.Port = 587
client.EnableSsl = True
client.UseDefaultCredentials = False
Dim SMTPUserInfo As New System.Net.NetworkCredential("user#hotmail.com", "password")
client.Credentials = SMTPUserInfo
client.Send(mail)
Label1.Text = "sent"
Catch ex As Exception
Label1.Text = ex.Message.ToString()
End Try