Mailing Attempt Object Reference? - vb.net

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.

Related

Mail is not being delivered

I am sending mail using SmtpClient. while attempting to send any-mail, I am getting success response. But mail is not getting delivered.
Dim mMailMessage As New Net.Mail.MailMessage
Dim client As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("domain", 587)
client.EnableSsl = True
client.Timeout = 100000
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network
client.UseDefaultCredentials = False
client.Credentials = New NetworkCredential("username***", "password")
Dim msg As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
mMailMessage.[To].Add(pstrToMailid)
mMailMessage.From = New System.Net.Mail.MailAddress("frommailid")
mMailMessage.Subject = pstrSubject
mMailMessage.IsBodyHtml = True
mMailMessage.Body = text
If Not pstrfilenames Is Nothing Then
Dim lint As Integer
Dim larrattachment() As String
larrattachment = Split(pstrfilenames, ",")
For lint = 0 To larrattachment.Length - 2
attachment = New Net.Mail.Attachment(larrattachment(lint))
mMailMessage.Attachments.Add(attachment)
Next
End If
client.Send(mMailMessage)

The SMTP server requires a secure connection VB.NET

Dim smtp As New SmtpClient
Dim mail As New MailMessage
smtp.Credentials = New Net.NetworkCredential("mail#gmail", "password")
mail.From = New MailAddress("mail#gmail.com")
mail.To.Add(totxt.Text$)
mail.Body = bodytxt.Text
If Not ListBox1.Items.Count <= 0 Then
Dim d As Integer
Dim attach As New Attachment(ListBox1.Items(d))
mail.Attachments.Add(attach)
End If
mail.Subject = subjecttxt.Text
smtp.EnableSsl = True
smtp.Port = "587"
smtp.Host = "smtp.gmail.com"
smtp.Send(mail)
smtp.Dispose()
done.Text = "Mail sent"
PictureBox4.BackgroundImage = My.Resources.tickfnl
dtls.Visible = False
I am trying to send email from my gmail account.But i am getting the error "The SMTP Server requires a secure connection".I even enabled LESS-SECURE APP login in my account settings.The password and email address is correct.I tried another email but same issue.Any fix ?
I TRIED ALL THE SOLUTIONS FROM THE DUPLICATE LINK,STILL THE SAME PROBLEM
**IF I REMOVE THIS LINE
smtl.enablessl=true
then i get this error
the server resposnse was 5.7.0 **
Fixed the error using EASendMail
Fixed it using EASendmail :
Panel6.Visible = True
done.Text = "Sending..."
''''''''''''''''''''''''
Dim oMail As New SmtpMail("TryIt")
Dim oSmtp As New EASendMail.SmtpClient()
oMail.From = fromtxt.Text
oMail.To = New AddressCollection(totxt.Text)
oMail.Subject = subjecttxt.Text
If html.CheckAlign = True Then
oMail.HtmlBody = bodytxt.Text
Else
oMail.TextBody = bodytxt.Text
End If
Dim oServer As New SmtpServer(MailConfig.host.Text)
oServer.Port = MailConfig.port.Text
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto
oServer.User = fromtxt.Text
oServer.Password = MailConfig.password.Text
Dim r As Integer
If ListBox1.Items.Count <= 0 Then
Else
oMail.AddAttachment(ListBox1.Items(r))
End If
oSmtp.LogFileName = Application.StartupPath & "\maillog.log"
Try
oSmtp.SendMail(oServer, oMail)
done.Text = "Mail sent !"
PictureBox4.BackgroundImage = My.Resources.tickfnl
Catch ex As Exception
aa = MsgBox(ex.Message)
done.Text = "Sending failed."
PictureBox4.BackgroundImage = My.Resources.excll
End Try

can't send Email from Webservice written in vb.net

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

Email not send using Gmail smtp on host

I've used this code to sending emails. When I test this code in LOCALHOST, it works correctly, but in host, I get this error:
Failure sending mail.few
My code is:
Dim MailMsg As New MailMessage(New Net.Mail.MailAddress("MYUSERNAME#gmail.com"), New Net.Mail.MailAddress("myname#gmail.com"))
MailMsg.Subject = "test"
MailMsg.Body = "test"
MailMsg.IsBodyHtml = True
Dim SmtpMail As New Net.Mail.SmtpClient
Dim SMTPUserInfo As New Net.NetworkCredential()
SMTPUserInfo.UserName = "MYUSERNAME"
SMTPUserInfo.Password = "MYPASSWORD"
SmtpMail.UseDefaultCredentials = False
SmtpMail.Credentials = SMTPUserInfo
SmtpMail.Host = "smtp.gmail.com"
SmtpMail.Port = 587
SmtpMail.EnableSsl = True
SmtpMail.Send(MailMsg)
'Start by creating a mail message object
Dim MyMailMessage As New MailMessage()
'From requires an instance of the MailAddress type
MyMailMessage.From = New MailAddress("USERNAME#gmail.com")
'To is a collection of types
MyMailMessage.To.Add("DESTINATION#EXAMPLE.COM")
MyMailMessage.Subject = "XXXXXXX" & x
MyMailMessage.Body = "XXXXXXXXXXXXXXXXXXX "
Dim attach As New Attachment(fileName)
MyMailMessage.Attachments.Add(attach)
'Create the SMTPClient object and specify the SMTP GMail server
Dim SMTPServer As New SmtpClient("smtp.gmail.com")
SMTPServer.Port = 587
SMTPServer.Credentials = New System.Net.NetworkCredential("USERNAME#gmail.com", "PASSWD")
SMTPServer.EnableSsl = True
SMTPServer.Send(MyMailMessage)
SMTPServer.Dispose()
MyMailMessage.Dispose()

How to send email to the user with all data input

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