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?!
Related
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.
I am trying to make a ConsoleApplication attach all files in a folder to an email and send it. I know how to do it with a single attachment, but for the life of me, I cannot figure out how to attach all items in a folder.
Current code:
Sub Main()
Try
Dim mail As New MailMessage("from", "to")
Dim client As New SmtpClient()
client.Port = 25
client.DeliveryMethod = SmtpDeliveryMethod.Network
client.UseDefaultCredentials = False
client.Host = ""
mail.Subject = "" + DateTime.Now.AddDays(-1).ToShortDateString()
mail.IsBodyHtml = True
mail.Body = "Test"
Dim file As System.Net.Mail.Attachment
file = New System.Net.Mail.Attachment("Path to single file")
mail.Attachments.Add(file)
client.Send(mail)
Return
Catch [error] As Exception
MsgBox("error")
Return
End Try
End Sub
Thanks in advance
EDIT:
I tried the below code that I found on another post, but it just errors out (and using the ConsoleApplication, I am not sure how to view the exact error its giving)
For Each filePath As String In Directory.GetFiles(My.Settings.FileLoc1)
Dim Attach As New Net.Mail.Attachment(filePath)
mail.Attachments.Add(Attach)
Next
I got it working!
Sub Main()
Try
Dim mail As New MailMessage("from", "too")
Dim client As New SmtpClient()
client.Port = 25
client.DeliveryMethod = SmtpDeliveryMethod.Network
client.UseDefaultCredentials = False
client.Host = ""
mail.Subject = "" + DateTime.Now.AddDays(-1).ToShortDateString()
mail.IsBodyHtml = True
mail.Body = "Test"
For Each filePath As String In Directory.GetFiles(My.Settings.FileLoc1)
Dim Attach As New Net.Mail.Attachment(filePath)
mail.Attachments.Add(Attach)
Next
client.Send(mail)
Return
Catch [error] As Exception
MsgBox("error")
Return
End Try
End Sub
I am trying to make a simple e-mail sending program but it has confused me when it said object reference not set to an instance of an object
Try
Dim mail As MailMessage
Dim client As SmtpClient
Dim SmtpServer As New SmtpClient()
Dim fromAddress As String
Dim toAddress1 As String
Dim toAddress2 As String
Dim toAddress3 As String
Dim subject As String
Dim message As String
SmtpServer.Credentials = New Net.NetworkCredential("*********#gmail.com", "**********")
client.UseDefaultCredentials = False
fromAddress = FromEmail.Text
If ToEmail1.Visible = True Then
toAddress1 = ToEmail1.Text
ElseIf ToEmail1.Visible = True And ToEmail2.Visible = True Then
toAddress1 = ToEmail1.Text
toAddress2 = ToEmail2.Text
ElseIf ToEmail1.Visible = True And ToEmail2.Visible = True And ToEmail3.Visible = True Then
toAddress1 = ToEmail1.Text
toAddress2 = ToEmail2.Text
toAddress3 = ToEmail3.Text
End If
subject = "Subject"
Message = "Message"
mail = New MailMessage(fromAddress, toAddress1, subject, Message)
client = New SmtpClient("Smtp.live.com")
client.Port = 587
Dim SMTPUserInfo As New System.Net.NetworkCredential("user#hotmail.com", "password")
client.Credentials = SMTPUserInfo
client.Send(mail)
MsgBox("sent")
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
you're declaring two smtpclients
Dim client As SmtpClient
Dim SmtpServer As New SmtpClient()
you instantiated SmtpServer, but not client. Then you do
client.UseDefaultCredentials = False
I guess this is where the exception is raised.
the code looks like having some lines to send some mail through SmtpServer from a google account and other lines to send some mail through client from a hotmail account. Besides, SmtpServer is using some server defined in config.sys.
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)
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