0

I want to an email message Application

I used new keyword but still the problem exist.

Imports System.Net.Mail
Public Class Form1
  Private myMsg As MailMessage
  Private smtpSender As SmtpClient
  Private Sub CreateMessage()
      myMsg.From = New MailAddress(txtForm.Text.Trim)
      myMsg.To.Add(txtTo.Text.Trim)
      myMsg.Subject = txtSubject.Text
      myMsg.Body = txtBody.Text
      myMsg.Priority = MailPriority.Normal
  End Sub
  Private Sub createSmtp()
      smtpSender.Credentials = New Net.NetworkCredential(txtForm.Text.Trim, txtPass.Text.Trim)
      smtpSender.EnableSsl = True
      smtpSender.Host = "smtp.live.com"
      smtpSender.Port = "587"
  End Sub
  Private Sub sendMessage()
      Try
          smtpSender.Send(myMsg)
      Catch ex As Exception
          MessageBox.Show(ex.ToString())
      End Try
  End Sub
  Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
      CreateMessage()
      createSmtp()
      sendMessage()
  End Sub

  Private Sub btnResetField_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResetField.Click
      txtBody.Clear()
      txtForm.Clear()
      txtPass.Clear()
      txtSubject.Clear()
      txtTo.Clear()
  End Sub
End Class

Error in this line: myMsg.From = New MailAddress(txtForm.Text.Trim)

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • I update the correction from you, but still the error exist here: smtpSender.Credentials = New Net.NetworkCredential(txtForm.Text.Trim, txtPass.Text.Trim) – Abu Mus'ab Auna Jul 31 '19 at 15:52
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Blackwood Aug 03 '19 at 19:24

1 Answers1

1

This line isn't complete: Private myMsg As MailMessage
It is declared as a variable but never instantiated as a new object.

Should be:

 Private myMsg As New MailMessage
 Private smtpSender As New SmtpClient

Objects and classes in Visual Basic

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
SouXin
  • 1,565
  • 11
  • 17
  • @AbuMus'abAuna The way to thank people on Stack Overflow is to accept their answer. You do this by clicking the check mark (tick mark) to the left of the answer. – Mary Jul 31 '19 at 22:10