Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have below method
VB
Private Sub SendHtmlFormattedEmail(ByVal recepientEmail As String, ByVal subject As String, ByVal body As String)

Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage

Try

mailMessage.From = New MailAddress(ConfigurationManager.AppSettings("UserName"))

' mailMessage.CC.Add("danish.habib@plan-international.org")

mailMessage.Bcc.Add("spober_woh@yahoo.com")

mailMessage.Subject = subject

mailMessage.Body = body

mailMessage.IsBodyHtml = True

mailMessage.To.Add(New MailAddress(recepientEmail))

Dim files As List(Of HttpPostedFile) = DirectCast(Cache(Me.Key), List(Of HttpPostedFile))

For Each file As HttpPostedFile In files

mailMessage.Attachments.Add(New Attachment(file.InputStream, Path.GetFileName(file.FileName), file.ContentType))

Next

mailMessage.From = New MailAddress("omer@planrbme.com.pk")


Dim smtp As New SmtpClient()

smtp.Host = "202.165.228.15"

smtp.EnableSsl = False

smtp.Credentials = New System.Net.NetworkCredential("omer@planrbme.com.pk", "Pakistan_123")

smtp.Port = 25

smtp.Send(mailMessage)


Catch ex As Exception

ex.Message.ToString()

End Try

End Sub

When i am getting the mail.To address from the database like below
mailMessage.To.Add(New MailAddress(recepientEmail)) then it did not send any email to my email address it did not give any exception as well , but when i hard code it like
mailMessage.To.Add("d.habib@plan-international.org") it send email to that address why it is so?

What I have tried:

I have tried to send email to google account like marktabor@gmail.com it send correctly it did not send just with this domain and when i am using the method to get the email address then it gets the email correctly like d.habib@plan-international.org but it did not send to that email when i hard code it then it send email to that address
Posted
Updated 31-Jul-16 22:01pm
v2
Comments
Richard MacCutchan 1-Aug-16 3:55am    
Use you debugger to see exactly what is being constructed in the MailAddress and MailMessage.
Malikdanish 1-Aug-16 3:57am    
it gets the email address correctly
F-ES Sitecore 1-Aug-16 4:25am    
It might be a Unicode issue or some funny characters\spaces you can't immediately see. As said below, we don't have your data and we can't use the debugger on your code so it's hard to say for sure. Use the hex view of the string in the debugger and compare the hex values with a hard-coded string, that will let you know if they're really the same.

1 solution

We can't tell - the problem probably isn't in that code but in the code that calls it.
So use the debugger: put a breakpoint on the first line of that Sub and when the debugger hits it look at exactly what you got passed as a recepientEmail string.
If it's not what you expected - and I suspect it won't be - use the Call Stack to look back in your code to find out why it is what it is.
But we can't do any of that for you - we don't have the rest of your code, so we can't run it, and it wouldn't help anyway as we don't have your data to provide us with the same values!
 
Share this answer
 
Comments
Malikdanish 1-Aug-16 7:36am    
method in the button click
1- SendMAilMessage()
2- Private Sub SendMAilMessage()
Dim body As String = Me.PopulateBody("Dear User", _
"Result based Monitoring And Evaluation system from Plan PAKISTAN", _
"http://www.planrbme.com.pk" & _
"", _
("we are very pleased to inform you that your requested achievement against the targets have been approved by the supervisor .<br/> " & _
"Suggested Comments are :  {" & AcceptComments & "} .<br/>.<br/> " & _
"<br/>.<br/>Dated:" & DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss ")))
Me.SendHtmlFormattedEmail(GetUserIDEmail(ddlusers.SelectedValue), "Record Accepted", body)
End Sub
3- Private Sub SendHtmlFormattedEmail(ByVal recepientEmail As String, ByVal subject As String, ByVal body As String)



Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage
Try
mailMessage.From = New MailAddress(ConfigurationManager.AppSettings("UserName"))
' mailMessage.CC.Add("danish.habib@plan-international.org")
mailMessage.Bcc.Add("spober_woh@yahoo.com")
mailMessage.Subject = subject
mailMessage.Body = body
mailMessage.IsBodyHtml = True
mailMessage.To.Add(New MailAddress(recepientEmail))
Dim files As List(Of HttpPostedFile) = DirectCast(Cache(Me.Key), List(Of HttpPostedFile))
For Each file As HttpPostedFile In files
mailMessage.Attachments.Add(New Attachment(file.InputStream, Path.GetFileName(file.FileName), file.ContentType))
Next
mailMessage.From = New MailAddress("omer@planrbme.com.pk")

Dim smtp As New SmtpClient()
smtp.Host = "202.165.228.15"
smtp.EnableSsl = False
smtp.Credentials = New System.Net.NetworkCredential("omer@planrbme.com.pk", "Pakistan_123")
smtp.Port = 25
smtp.Send(mailMessage)

Catch ex As Exception
ex.Message.ToString()
End Try



End Sub
Private Function GetUserIDEmail(ByVal UserId As Integer) As String
Dim UserType As String = Nothing
Dim cnnResource As New SqlConnection(AppSettings("DbSqlPortal"))
Dim cmdResource As New SqlCommand("GetEmailAddressForDropdownFromPartner", cnnResource)
Dim drResource As SqlDataReader = Nothing

cmdResource.CommandType = CommandType.StoredProcedure
cmdResource.Parameters.Add("@UserId", SqlDbType.NVarChar).Value = ddlusers.SelectedValue
Try
cnnResource.Open()
drResource = cmdResource.ExecuteReader(CommandBehavior.SingleRow)
If drResource IsNot Nothing AndAlso drResource.Read() Then
UserType = drResource("Email")
Else
Throw New Exception("Record could not be found/read.")
End If
Catch ex As Exception
ex.Message.ToString()
Finally

End Try
Hdm.Value = UserType
Return UserType



End Function
OriginalGriff 1-Aug-16 8:16am    
Use the debugger!
As I said: "... it wouldn't help anyway as we don't have your data to provide us with the same values!"
Malikdanish 2-Aug-16 3:16am    
it was the issue from the mail server which was blocking the IP , Thanks you all for support OriginalGriff many thanks you too to put me on debugging until the problem resolved .:) thanks its a good practice
OriginalGriff 2-Aug-16 3:27am    
You're welcome!
Glad you sorted it. :thumbsup:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900