The parameter 'from' cannot be an empty string. Parameter name: from - asp.net

Ok, so basically I have a comments form on my website. People but comments in the comment box and it emails the comments to my email. The email part works fine and goes through. However, the error comes when I try to clear the form fields. It gives me an error saying:
Error: The parameter 'from' cannot be an empty string.
Parameter name: from
So anyone have any thoughts? like I said the email part works fine its just clearing the fields is giving me trouble
here is the code:
Imports System.Web.Mail
Imports System.Text
Partial Class Pages_ContactUs : Inherits System.Web.UI.Page
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim body As String = "From: " + YourName.Text + " " + Environment.NewLine + "Email: " + YourEmail.Text + Environment.NewLine + Environment.NewLine + "Message: " + Environment.NewLine + Comments.Text
Dim MM As New System.Net.Mail.SmtpClient
MM.EnableSsl = True
MM.Host = "smtp.gmail.com"
Dim cred As New System.Net.NetworkCredential("myEmail#gmail.com", "myPassword")
MM.Credentials = cred
MM.Send(YourEmail.Text, "myEmail#gmail.com", Subject.Text, body)
ClearFields()
lblEmail.ForeColor = Drawing.Color.Green
lblEmail.Text = "Your message has been sent successfuly"
lblEmail.Visible = True
End Sub
Protected Sub ClearFields()
YourName.Text = ""
YourEmail.Text = ""
Comments.Text = ""
Subject.Text = ""
End Sub
End Class

You must supply it, just make it yours. Basic pattern:
Using SmtpServer As New SmtpClient()
Using mail As New System.Web.Mail.MailMessage()
Try
SmtpServer.UseDefaultCredentials = True
SmtpServer.EnableSsl = True
SmtpServer.Credentials = New Net.NetworkCredential(un, pw)
SmtpServer.Port = 25
SmtpServer.Host = "smtp.address"
mail.From = New MailAddress(sender)
mail.To.Add(some address)
mail.Subject = subject
mail.Body = body
mail.Sender = New MailAddress(sender)
SmtpServer.Send(mail)
Catch
End Try
End Using
End Using

Related

SMTP Email Works until triggering a 404 error in the middle of sending Mail

I have a table for email recipients imma database, and use the "select all" button from the gridview table as trigger to call the mail function. my iteration func to call the 'executeApprove' repeatedly described here :
For Each row In gridApproval.Rows
chkApprove = CType(gridApproval.Rows(i).FindControl("chkApprove"), CheckBox)
If chkApprove.Checked Then
executeApprove(Right(lblTglProses.Text, 8), Left(gridApproval.Rows(i).Cells.Item(2).Text, 3), Session("role"), "APPROVE", Session("userID"), False)
End If
i = i + 1
Next
Catch ex As Exception
ScriptManager.RegisterStartupScript(Me.UpdatePanel1, Me.GetType, "Message", "alert('Error : ' + '" & Replace(Replace(ex.Message.ToString, "'", ""), vbNewLine, "") & "');", True)
End Try
gridApproval.DataBind()
End If
on my executeApprove :
Protected Sub executeApprove(ByVal tanggal As String, ByVal branch As String, ByVal role As String, ByVal status As String, ByVal user As String, ByVal isDone As Boolean)
Dim Title As String
Dim Body As String
Try
conn = run.connect(conn, "open")
cmd = New SqlCommand("usp_status_trial", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandTimeout = 0
cmd.Parameters.Add("#date", SqlDbType.VarChar).Value = tanggal
cmd.Parameters.Add("#branch", SqlDbType.VarChar).Value = branch
cmd.Parameters.Add("#role", SqlDbType.VarChar).Value = role
cmd.Parameters.Add("#status", SqlDbType.VarChar).Value = status
cmd.Parameters.Add("#user", SqlDbType.VarChar).Value = user
reader = cmd.ExecuteReader
If reader.HasRows Then
reader.Read()
result = reader.Item(0).ToString
ScriptManager.RegisterStartupScript(Me.UpdatePanel1, Me.GetType, "Message", "alert('" & result & "');", True)
If result = "APPROVED" Then
If Session("role") = "APP" Then
Title = "Title Email"
Body = "Body Email"
sendMultipleMail(Title, Body)
End If
ElseIf result = "UNAPPROVED" Then
If Session("role") = "APP" Then
Title = "Title Email"
Body = "Body Email"
sendMultipleMail(Title, Body)
End If
End If
If isDone Then
Response.Redirect("~/dashboard.aspx?id=" + Request.QueryString("id"), True)
End If
End If
conn = run.connect(conn, "close")
Catch ex As Exception
ScriptManager.RegisterStartupScript(Me.UpdatePanel1, Me.GetType, "Message", "alert('Error : ' + '" & Replace(Replace(ex.Message.ToString, "'", ""), vbNewLine, "") & "');", True)
End Try
End Sub
and my sendMultipleMail :
Public Sub sendMultipleMail(ByVal subject As String, ByVal body As String)
Dim dsMail As New DataSet
Dim strQuery As New SqlCommand
Dim SendFrom As MailAddress
Dim SendTo As MailAddress
Dim emailClient As SmtpClient
Dim SMTP As String
Dim mailFrom As String
SMTP = getAppParam("SMTPserver") 'got the SMTP
mailFrom = getMailSetting("mailFrom")
strQuery.CommandText = "%'Query for select all the receiver'%"
dsMail = RunQuery(strQuery)
For Each rowMail In dsMail.Tables(0).Rows
SendFrom = New MailAddress(mailFrom)
SendTo = New MailAddress(rowMail("mail_address").ToString())
Dim MyMessage As MailMessage = New MailMessage(SendFrom, SendTo)
MyMessage.Subject = subject
MyMessage.Body = body
emailClient = New SmtpClient(SMTP)
emailClient.Send(MyMessage)
Next
End Sub
everything works just fine until a few minutes while sending the lot of email (in the middle of calling the func repeatedly), the IIS returning a strange 404 response.
POST http://localhost/myApp/approval?type=all&id=userid 500 (Internal
Server Error) -- MicrosoftAjax.js:6
Uncaught TypeError: Cannot read properties of undefined (reading 'PRM_ServerError')
at Sys.WebForms.PageRequestManager._createPageRequestManagerServerError
(MicrosoftAjaxWebForms.js:6:11462)
at Sys.WebForms.PageRequestManager._onFormSubmitCompleted (MicrosoftAjaxWebForms.js:6:25554)
at Array. (MicrosoftAjax.js:6:307)
at MicrosoftAjax.js:6:51370
at Sys.Net.WebRequest.completed (MicrosoftAjax.js:6:89678)
at XMLHttpRequest._onReadyStateChange (MicrosoftAjax.js:6:84277)
am I made some mistakes?
Thank you for the answer. Error 404 appears just because the SMTP takes a lot of time to send a whole of email to the receiver. so i've been fixing them using
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="3000000000" />
</requestFiltering>
</security>
inside .config file to increase the timeout.

Send email failed conversion from string "" to type 'Boolean' is not valid

Imports System.Net.Mail
Partial Class ch5_proj4_ch5_proj4
Inherits System.Web.UI.Page
Protected Sub EmailBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles EmailBtn.Click
' insert code here
Dim MySmtpClient As New SmtpClient()
Dim MM As New MailMessage()
Try
MySmtpClient.Host = "stmp.gmail.com"
MySmtpClient.Port = 587
MySmtpClient.EnableSsl = True
MySmtpClient.UseDefaultCredentials = True
MySmtpClient.Credentials = New System.Net.NetworkCredential()
'mail message
MM.To.Add(New MailAddress(TextBox2.Text, TextBox1.Text))
Dim fromAddress As New MailAddress(TextBox4.Text, TextBox3.Text)
MM.From = fromAddress
MM.Subject = TextBox4.Text
MM.IsBodyHtml = RadioButtonList1.SelectedValue
MM.Body = txtMessage.Text
MySmtpClient.Send(MM)
Label5.Text = "Email successfully sent."
Catch exc As Exception
Label5.Text = "Send email failed" + exc.Message
End Try
MM = Nothing
MySmtpClient = Nothing
End Sub
End Class
What am I doing wrong? Every time I do it I have an error
"Send email failed Conversion from string "" to type 'Boolean' is not valid."
Please help.
Try to change this MM.IsBodyHtml = RadioButtonList1.SelectedValue to this MM.IsBodyHtml = If(RadioButtonList1.SelectedValue = "True",True,False)

How to send mail smtp in local host by VB

I have used VB.net for my project, ticketing system, where you add new ticket it should be sending mail to some Emails I have defined in other class
but I have silly problem, which is sending mail through local host.
is there any suggestion from this source code, when I uploaded to specific domain it works, but for localhost not working with me which that I don't receive any mail when I run my project
I want to send mail from localhost
Private Function SendConfirmationEmail(ByVal EmailAddress As String, ByVal Operation As String, ByVal CorreCode As String, ByVal CorrSubj As String, ByVal CutName As String, ByVal phone As String, ByVal CustomerEmail As String) As Boolean
''''''''''''''''''''''Getting Email Setting Information'''''''''''''''''''''''''''''''''''''''
Try
Dim FromMail As String = System.Configuration.ConfigurationManager.AppSettings.GetValues("DefualtMail")(0).ToString()
Dim PassMail As String = System.Configuration.ConfigurationManager.AppSettings.GetValues("DefualtMailPass")(0).ToString
Dim SenderName As String = "Customer Managment Team"
Dim SMTPHost As String = System.Configuration.ConfigurationManager.AppSettings.GetValues("SMTPHost")(0).ToString
''''''''''''''''''''''''''''''''''''Sending Emails'''''''''''''''''''''''''''''''''''''''''''''''''
Dim Mail As New MailMessage
Dim Body As String = ""
Dim Subject As String = Operation
Dim SendTo As String = EmailAddress
''''''''''''''''''''''''''''''''''E-mail'''''''''''''''''''''''''''''''''''''
If SendTo <> vbNullString Then
Mail.To.Clear()
Mail.CC.Clear()
Mail.Bcc.Clear()
Mail.To.Add(SendTo)
Mail.From = New MailAddress(FromMail, "Ticket Code : " + CorreCode)
Mail.IsBodyHtml = True
Dim reader As StreamReader
reader = New StreamReader(Server.MapPath("Index.html"))
Body = reader.ReadToEnd
Body = Body.Replace("MessageSubject", Operation)
Body = Body.Replace("Description", "description : " + CorrSubj)
Body = Body.Replace("customer_info", "Customer Name : " + CutName)
Body = Body.Replace("phone_number", "Customer Phone : " + phone)
Body = Body.Replace("email_address", "Customer address : " + CustomerEmail)
' Body = Body.Replace("PropertyTitle", "Ticket Code :" + CorreCode + " Ticket Description :" + CorrSubj + " Customer info : " + CutName + " phone number : " + phone + " email_address : " + EmailAddress)
Mail.Body = Body
' "<html dir='ltr'><body><h1>Confirmation</h1><br/> Please confirm you email at this link <br/> <a href='108.60.209.97/enmaa/ConfirmEmail.aspx/Enmaa0000" + ApproveId + "'>Confirm</a></body></html>"
Mail.Subject = Subject
'######################################## Sending The EMail########################################################
Dim SMTP As New SmtpClient(SMTPHost)
SMTP.Credentials = New System.Net.NetworkCredential(FromMail, PassMail)
SMTP.Host = SMTPHost
SMTP.Port = 587 '587
SMTP.EnableSsl = True
SMTP.Send(Mail)
End If
Return True

How to Send email with High Importance in asp.net VB

How do I set the code below to generate email with high priority in the subject line? Thank you for any guidance.
Private Sub SendEmail(ByVal pharmEmail As String, ByVal backupEmail As String)
Dim smtpClient As New System.Net.Mail.SmtpClient()
Dim message As New System.Net.Mail.MailMessage()
Try
Dim fromAddress As New System.Net.Mail.MailAddress(WebConfigurationManager.AppSettings("EmailFromAddr"), WebConfigurationManager.AppSettings("EmailFromName"))
message.From = fromAddress
message.To.Add(pharmEmail)
message.Subject = WebConfigurationManager.AppSettings("EmailSubject")
If (WebConfigurationManager.AppSettings("backupEnabled") = True) Then
message.CC.Add(backupEmail)
End If
message.IsBodyHtml = True
Dim orderURL As New HyperLink
orderURL.Text = "here"
orderURL.NavigateUrl = "http://" & WebConfigurationManager.AppSettings("ServerName") & "/User/ReviewOrder.aspx?orderID=" & webOrderID
message.Body = "An order was created using the account of " + Profile.FirstName.ToString() + " " + Profile.LastName.ToString() + ". " + WebConfigurationManager.AppSettings("EmailBody") + "<a href='" + orderURL.NavigateUrl + "'>here.</a>"
'message.Body = WebConfigurationManager.AppSettings("EmailBody") & " " & orderURL.
smtpClient.Send(message)
Catch ex As Exception
ErrorHandler.WriteError(ex.ToString)
Throw ex
End Try
I believe you can set the Priority property on the MailMessage. See MSDN for more details.

Asp.net email application mail comes twice when it sends

I made this simple mailing application but problem is recipients gets email twice.
This is my code on button click
Protected Sub submit_feedback_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit_feedback.Click
Dim feedback As String = "<b>Client Name :</b><br/>" + ParseOutput.Text + "<br/><br/> <b>Clients Experience :</b><br/>" + brief_details.Text + "<br/><br/> <b>Improvement Needed :</b><br/>" + brief_details2.Text + "<br/><br/> <b>Rating Given :</b><br/>" + rate1.SelectedValue.ToString + "<br/><br/> <b>How do you know about us :</b><br/>" + choice2.SelectedValue.ToString
Dim mail As New MailMessage()
Dim SmtpServer As New SmtpClient()
mail.To.Add("surajprince20#gmail.com")
mail.From = New MailAddress("something#gmail.com", "SuRaj_ Email Test")
mail.Subject = "Feedback - Reply From Client "
mail.Body = feedback
mail.IsBodyHtml = True
SmtpServer.Port = 25
SmtpServer.Credentials = New System.Net.NetworkCredential("something#gmail.com", "1234")
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.EnableSsl = True
SmtpServer.Send(mail)
Try
SmtpServer.Send(mail)
Response.Write("Successfull")
Catch ex As SmtpException
Response.Write(ex)
End Try
End Sub
It is clear from your code that you are sending the mail twice, have a look into your code:
SmtpServer.Send(mail) '<----- first sending attempt
Try
SmtpServer.Send(mail)'<----- second sending attempt
Response.Write("Successfull")
Catch ex As SmtpException
Response.Write(ex)
End Try
so you need to remove the SmtpServer.Send(mail) which is placed outside the TRY
ie.,
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.EnableSsl = True
Try ' <------ eleminate one line
SmtpServer.Send(mail)
Response.Write("Successfull")
Catch ex As SmtpException
Response.Write(ex)
End Try
SmtpServer.Send(mail)
Try
SmtpServer.Send(mail)
you are acutally sending the mail twice
Protected Sub submit_feedback_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit_feedback.Click
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New System.Net.NetworkCredential("something#gmail.com", "1234")
SmtpServer.Port = 587
SmtpServer.Host = "smtp.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress("something#gmail.com", "SuRaj_ Email Test")
mail.To.Add("surajprince20#gmail.com")
mail.Subject = "Feedback - Reply From Client "
mail.Body = feedback
SmtpServer.Send(mail)
MsgBox("mail send")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
END SUB

Resources