vb.net how to detect if an email is undeliverable in a win form program - vb.net

I have the following code:
Public Function VerstuurMail(ByVal strFrom As String, ByVal strTo As String, ByVal strSubject As String, ByVal strBody As String, ByVal strMailSMTP As String, ByVal MailUser As String, ByVal MailPassword As String, ByVal MailPort As Integer, Optional ByVal AttachmentFiles As String = "") As String
Try
'create the mail message
Dim mail As New MailMessage()
Dim basicCredential As New NetworkCredential(MailUser, MailPassword)
'set the addresses
mail.From = New MailAddress(strFrom)
mail.To.Add(strTo)
'set the content
mail.Subject = strSubject
If File.Exists(strBody) = True Then
Dim objReader As New System.IO.StreamReader(strBody, System.Text.Encoding.GetEncoding(1252))
mail.Body = objReader.ReadToEnd
objReader.Close()
End If
mail.IsBodyHtml = False
'send the message
Dim smtp As New SmtpClient(strMailSMTP)
smtp.DeliveryMethod = SmtpDeliveryMethod.Network
smtp.EnableSsl = True
'smtp.UseDefaultCredentials = True
smtp.Credentials = basicCredential
smtp.Port = MailPort
Dim AttachmentFile As String() = AttachmentFiles.Split("*")
For Each bestand In AttachmentFile
If System.IO.File.Exists(bestand) Then
mail.Attachments.Add(New Attachment(bestand))
Else
Call MessageBox.Show("File can't be found")
End If
Next
'Dim userState As Object = mail
'smtp.SendAsync(mail, userState)
'AddHandler smtp.SendCompleted, AddressOf SendCompletedCallback
smtp.Send(mail)
mailSent = True
smtp.Dispose()
Catch ex As Exception
mailSent = False
Call MessageBox.Show(ex.Message & vbCrLf & "Didn't sent to: " & strTo & vbCrLf & " with extra error message:" & vbCrLf & ex.ToString)
Finally
End Try
Return mailSent
End Function
This function is used in a program which reads a text file, with the parameters on one line, and is called as many lines there are. (in a loop)
This is working fine.
Now when the text file has a wrong email adress the function doesn't trow a error it just sent the email to nobody.
example: sent an mail to joe#gmail.com works, send an email to joe#hmail.com doesn't sent but doesn't give an error either.
I have googled but the examples said that I should use 'smtp.SendAsync(mail, userState)'
But then the program doesn't follow the loop anymore and no mails are being sent. I can't use the debugger and step through the code. It just jumps from one place to the other.
This is the other function:
Private Sub SendCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
Dim mail As MailMessage = CType(e.UserState, MailMessage)
'write out the subject
Dim subject As String = mail.Subject
'If e.Cancelled Then
' Call MessageBox.Show("Send canceled: " & Now & " token " & subject)
' MailLog &= "Send canceled" & vbCrLf
'End If
If Not e.Error Is Nothing Then
Call MessageBox.Show("Foutmelding op: " & Now & " onderwerp " & subject & " met error: " & e.Error.ToString())
MailLog = MailLog & "Niet verstuurd met als fout melding: " & e.Error.ToString() & vbCrLf
Else
'Call MessageBox.Show("Message sent at: " & Now)
MailLog = MailLog & "Bericht verstuurd op: " & Now & vbCrLf
End If
mailSent = True
End Sub
Thanks in advance. I hope somebody can put me in the right direction.
Brian

This is a data issue, not an issue with the actual mechanism to send email. The best you can do is to use a regular expression to make sure the email address is valid per the rules of RFC 2822, like this:
string email = txtemail.Text;
Regex regex = new Regex(#"^([\w\.\-]+)#([\w\-]+)((\.(\w){2,3})+)$");
Match match = regex.Match(email);
if (match.Success)
{
Response.Write(email + " is valid.");
}
else
{
Response.Write(email + " is invalid.");
}
Unfortunately for you, joe#hmail.com is a valid email address by the above logic, but is is not the intended joe#gmail.com address so it ends up in the wrong place. When it is discovered that it is the wrong email address, then changing the data to the right value is the only correct course of action.
Note: Generally, you verify someone's email address when they register for a website, thus the system "knows" the email address is legitimate/correct because they successfully received an email and entered a verification code or clicked on a link that verified with the website that they did get the email. This solves most data issues (misspellings, incorrectly entered values, etc.).

After two full days of testing and searching I have found that when using:
Dim userState As Object = mail
smtp.SendAsync(mail, userState)
AddHandler smtp.SendCompleted, AddressOf SendCompletedCallback
you should not close the smtpClient with smtp.Dispose()
The callback function kept getting the cancel error.
Also I used the backgroundworker for sending the many mails but the async is, in a way, a backgroundworker. So I had two backgroundworkers and that didn't work at all.
It took me two days.....
Brian

Related

Auto insert individual members' names and their amounts into a message body and send as SMS to their corresponding numbers

This question is about BulkSMS Messaging.
Sending the same message to different contacts is successful. The problem is, how can i personalized the message to send to their respective recipients.
For instance, in sending the message, i would like an access vba code to auto insert the individual members' names and their amounts into the message body and send to their corresponding numbers.
Something like this; (Dear [NameField], your [AmountField] has been received. Thank you.)
Updated:
The sSendMessage procedure below is what I call to send my messages. The way it works is like, there is a button that when clicked it populates the ttcontact textbox with MembersNumbers. The user then typed the message in the ttmessage textbox and in sending it then uses sSendMessage procedure to send the message in the ttmessage to the contacts in the ttcontacts.
Ever since you (# Applecore) responded to my question, I have been trying how to work around it but don’t know where to start. This time around too, there will be no ttmessage and ttcontact for the user to typed data, every info will be selected from the tblMember table and uniquely sent to their respective contacts. Can you please possibly look at my sSendMessages and check how it can be called by the sSend2Member to send the message row by row till it gets to the last record.
Private Sub sSendMessages()
Dim myURL As String
Dim sender As String
Dim contact As String
Dim msg As String
Dim postData As String
Dim winHttpReq As Object
apikey = "xxxxxxxxxxxxx"
sender = Me.ttSender.Value
contact = Me.ttContact.Value
msg = Me.ttMessage.Value
Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
myURL = "https://apps.mnotify.net/smsapi?key=" & apikey & "&to=" & contact & "&msg=" & msg & "&sender_id=" & sender
postData = "key=" + apikey _
+ "&to=" + contact _
+ "&msg=" + msg _
+ "&sender_id=" + sender
winHttpReq.Open "POST", myURL, False
winHttpReq.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
winHttpReq.send (postData)
SendSMS = winHttpReq.ResponseText
MsgBox SendSMS
End If
End Sub
If you have already created a procedure that sends a message (for example sSendMessage), you can modify it to accept the number and message. You can then have some code like this which accepts the ID field from the table of members and then calls sSendMessage:
Sub sSend2Member(lngMember As Long)
On Error GoTo E_Handle
Dim db As DAO.Database
Dim rsData As DAO.Recordset
Dim strMsg As String
Set db = DBEngine(0)(0)
Set rsData = db.OpenRecordset("SELECT * FROM tblMember WHERE MemberID=" & lngMember)
If Not (rsData.BOF And rsData.EOF) Then
strMsg = "Dear " & rsData!MemberName & ", "
strMsg = strMsg & "your " & Format(rsData!MemberAmount, "#0.00") & " has been received. Thank you."
Call sSendMessage(rsData!MemberNumber, strMsg)
End If
sExit:
On Error Resume Next
rsData.Close
Set rsData = Nothing
Set db = Nothing
Exit Sub
E_Handle:
MsgBox Err.Description & vbCrLf & vbCrLf & "sSend2Member", vbOKOnly + vbCritical, "Error: " & Err.Number
Resume sExit
End Sub
If you are sending to 6 members, then you would call this procedure with each of the 6 MemberIDs.
Regards,

How do I dispose System.Net.Mail.MailMessage in VB when using SendAsync

I have not managed to find a conclusive answer for this on here or in MSDN. When using the following code, if I try to dispose the message after sending the asynch mail then I get a System.ObjectDisposedException: Cannot access a disposed object. This is when the microsoft example suggests disposing.
I can't dispose the message in the callback because it is out of scope. I have tried using a module level Mail message but get the same results. If I don't dispose the message everything works fine but this is not good practice. Please advice the best place to dispose the mail message.
Public Sub SendEmailWithReport(ByVal v_objEmailAddress As System.Collections.Generic.List(Of String), ByVal v_strSubject As String,
ByVal v_strBody As String, ByVal v_strFileName As String, ByVal v_intContentID As Integer,
ByVal v_strType As String) Implements IMessagingPlatform.SendEmailWithReport
Dim objMessage As New MailMessage
Dim objAttachment As New Attachment(v_strFileName, MediaTypeNames.Application.Pdf)
Try
'configure e-mail addresses and add attachment
With objMessage
For Each strAddress As String In v_objEmailAddress
.To.Add(strAddress)
Next
.Attachments.Add(objAttachment)
End With
SetUpAsynchEmail(objMessage, v_strSubject, v_strBody, v_strFileName, v_intContentID, v_strType)
Catch ex As Exception
Trace.WriteLine(DateTime.Now().ToString("dd/MM/yyyy HH:mm:ss.fff") & " : Failed to setup e-mail with report: " & ex.Message.ToString, "ERR")
Finally
'objMessage.Dispose() *disposing here gives System.ObjectDisposedException: Cannot access a disposed object.**
'objAttachment.Dispose()
End Try
End Sub
Private Sub SetUpAsynchEmail(ByVal v_objMessage As MailMessage, ByVal v_strSubject As String, ByVal v_strBody As String,
ByVal v_strFileName As String, ByVal v_intContentID As Integer, ByVal v_strType As String)
Dim intID As Integer
Try
Dim basicAuthenticationInfo As New System.Net.NetworkCredential(mstrUserName, mstrPassword)
Dim objClient As New SmtpClient()
'configure mail message
With v_objMessage
.IsBodyHtml = True
.Subject = v_strSubject
.Body = v_strBody
If mstrFromEmailName <> "" Then
.From = New MailAddress(mstrFromEmailAddress, mstrFromEmailName)
Else
.From = New MailAddress(mstrFromEmailAddress)
End If
End With
'configure mail client
With objClient
.Host = mstrSMTPHost
.UseDefaultCredentials = False
.Credentials = basicAuthenticationInfo
.EnableSsl = True
.Port = mintSMTPPort
End With
' Set the method that is called back when the send operation ends.
AddHandler objClient.SendCompleted, AddressOf SendCompletedCallback
'Generate a unique message number
intID = mobjContainer.AddUnsentMail(v_intContentID, v_strType, v_strFileName)
If intID > -1 Then
objClient.SendAsync(v_objMessage, intID)
End If
'v_objMessage.Dispose() **disposing here gives System.ObjectDisposedException: Cannot access a disposed object.
Catch ex As Exception
Trace.WriteLine(DateTime.Now().ToString("dd/MM/yyyy HH:mm:ss.fff") & " : Failed to setup e-mail: " & ex.Message.ToString, "ERR")
Finally
End Try
End Sub
Private Sub SendCompletedCallback(ByVal v_objSender As SmtpClient, ByVal e As AsyncCompletedEventArgs)
' Get the unique identifier for this asynchronous operation.
Dim strMessageID As String = CStr(e.UserState)
Try
If e.Cancelled Then
Trace.WriteLine(DateTime.Now().ToString("dd/MM/yyyy HH:mm:ss.fff") & " : E-mail cancelled for message with ID " &
strMessageID, "ERR")
End If
If e.Error IsNot Nothing Then
Trace.WriteLine(DateTime.Now().ToString("dd/MM/yyyy HH:mm:ss.fff") & " : Failed to send e-mail with ID " &
strMessageID & ", " & e.Error.ToString(), "ERR")
'E-mail error - update table
mobjContainer.UpdateUnsentMail(CInt(strMessageID))
Else
'E-mail success - delete record from table
mobjContainer.DeleteUnsentMailItem(CInt(strMessageID))
Trace.WriteLineIf(mobjLogTrace.LogEvents = True And mobjLogTrace.LogDetail >= 4, DateTime.Now().ToString("dd/MM/yyyy HH:mm:ss.fff") &
" : E-mail with ID " & strMessageID & " successfully sent", "EVT")
End If
Catch objException As Exception
Trace.WriteLine(DateTime.Now().ToString("dd/MM/yyyy HH:mm:ss.fff") & " : Failed to send e-mail with ID " &
strMessageID & ", " & objException.ToString(), "ERR")
Finally
v_objSender.Dispose()
End Try
End Sub
If you created a class to hold both intID and your v_objMessage and passed this as the second parameter in your objClient.SendAsync() call you would have access to both the ID and the message in the SendCompletedCallback() sub. You could then call .Dispose on the message in your Finally block.

encrypting/decrypting a password stored on a compact database

My project contains 2 forms, one to register users and one to login. I am using a compact local database to store the passwords. I wrote a function to encrypt the password when the user registers. I then wrote another to decrypt the same password when the user logs in.
The first part, encryption, works just fine. The user registers, and I can see the password encrypted on the database. However, when I try to log in, the password is not being decrypted. Here are my Functions.
Module EncryptionModule
Public Function base64Encode(ByVal sData As String) As String
Try
Dim encData_Byte As Byte() = New Byte(sData.Length - 1) {}
encData_Byte = System.Text.Encoding.UTF8.GetBytes(sData)
Dim encodedData As String = Convert.ToBase64String(encData_Byte)
Return (encodedData)
Catch ex As Exception
Throw (New Exception("Error is base64Encode" & ex.Message))
End Try
End Function
Public Function base64Decode(ByVal sData As String) As String
Dim encoder As New System.Text.UTF8Encoding()
Dim utf8Decode As System.Text.Decoder = encoder.GetDecoder()
Dim todecode_byte As Byte() = Convert.FromBase64String(sData)
Dim charCount As Integer = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length)
Dim decoded_char As Char() = New Char(charCount - 1) {}
utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0)
Dim result As String = New [String](decoded_char)
Return result
End Function
End Module
This is the routine to register a user and encrypting the password:
Private Sub btnRegister_Click(sender As Object, e As EventArgs) Handles btnRegister.Click
'If the username is taken or used on the
'database, then create account
If MasterTableAdapter.CheckUserName(txtUserName.Text) = Nothing Then
Dim pwd As String = base64Encode(Trim(txtConfirmPassword.Text))
MasterTableAdapter.CreateAccount(txtFName.Text, txtLName.Text, txtUserName.Text, pwd, int1)
MsgBox("An account has been created for: " & vbNewLine & _
"Employee: " & txtFName.Text & " " & txtLName.Text & vbNewLine & _
"User Name: " & txtUserName.Text & vbNewLine & _
"Access Level: " & strAccessLevel)
Me.Close()
Else
MessageBox.Show("The username is in use. Please select another username.", "Authentication Error", MessageBoxButtons.OK, _
MessageBoxIcon.Error)
End If
End Sub
Here is the routine to log in and decrypt the password from the Login Form:
Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
Dim pwd As String = base64Decode(Trim(txtPassword.Text))
If Not MasterTableAdapter.Login(txtUserName.Text, pwd) = Nothing Then
'frmWelcomePage.Show()
MsgBox("SUCCESS")
Else
'If no match, display error, clear text boxes and send focus back to the username text box.
MessageBox.Show("Username or password do not match", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtPassword.Text = Nothing
txtUserName.Text = Nothing
txtUserName.Focus()
End If
End if
End Sub
I am new to the whole encryption arena so I don't know what I am doing wrong here.
You shouldn't decrypyt the password.
When the user creates a password, you should generate a hash (ie: a value from which the password can not be reconstructed)
When the user attempts to login, you should compare the hash value of the given password with the stored hash.
First, Base64 encoding is not encryption. Many people can look at a B64 string and know what to do to unscramble it. You should look into hash techniques as podiluska suggested.
That said, since your Decode method cant unscramble what you encode, it means you have an error in one or the other. Simple encoding for what you are doing can be done:
Dim s As String = "MySecretPassword"
' convert to byte array
Dim bArry() As Byte = System.Text.Encoding.UTF8.GetBytes(s)
' convert bytes to Base64:
Dim sb64 As String = System.Convert.ToBase64String(barry)
To decode is just the reverse:
' Base64 -> Byte Array
Dim bOut() As Byte = System.Convert.FromBase64String(sb64)
' Byte Arry -> clear text
Dim sOut As String = System.Text.Encoding.UTF8.GetString(bOut)

Is it safe to call SmtpClient.Dispose() in .NET?

I have a scenario where I need to send 100 emails in one shot (using a loop), but also I am not allowed to send 1 email per SMTP session.
Right now all 100 emails are sharing same SMTP session.
I was thinking that calling SmtpClient.Dispose() will take care of what I need. Please correct me if I am wrong.
So, basically 3 questions:
Will SmtpClient.Dispose() take care of what I need?
If Yes, is it safe to Dispose() SmtpClient without affecting other services on the
server?
If No, What would be the right approach to achieve what I
want?
Sample Code:
Private Shared Sub SendMail(ByVal MailServer As SmtpClient, ByVal body As String, ByVal Subject As String, ByVal FromEmail As String, _
ByVal ToEmailList As String, Optional ByVal AttFile As Attachment = Nothing)
Dim message As New MailMessage
Try
message.From = New MailAddress(FromEmail)
message.Subject = Subject
message.IsBodyHtml = False
message.Body = body
message.Priority = MailPriority.High
If Not AttFile Is Nothing Then
message.Attachments.Add(AttFile)
Else
message.Attachments.Add(AttFile)
End If
MailServer.Send(message)
Catch ex As Exception
Throw New ApplicationException("SERVICE1.SendMail ERROR -- Error sending email [ERROR]:[" & ex.Message.ToString & "] " & vbCrLf & "To:" & ToEmailList & vbCrLf & "From:" & FromEmail & vbCrLf & "Subject: " & Subject & vbCrLf & "Body: " & body)
End Try
message.Dispose()
End Sub
And this is how the method is being executed:
For Each Item In ItemListCollection
m_MailServer = New SmtpClient(MailServerName)
MailServer.Credentials = New System.Net.NetworkCredential(MailServerUserName, MailServerPassword)
SendMail(WeeklyMailServer, msgBody, msgSubject, MsgFromEmail, "xyz#abc.com", rptAttachment)
Next
You could wrap it in a using statement and ensure that it is disposed when execution leaves the block. And you can call Send multiple times in a loop using the same SmtpClient.
Using client = New SmtpClient()
For i As Integer = 0 To 99
Dim message = New MailMessage()
'initialization of whatever is needed
' message creation
client.Send(message)
Next
End Using
Inside execution loop, you can enclose the code in a Using block. This will use a separate smtpclient for each email and will dispose / close it properly.
For Each Item In ItemListCollection
using m_MailServer as New SmtpClient(MailServerName)
MailServer.Credentials = New System.Net.NetworkCredential(MailServerUserName, MailServerPassword)
SendMail(WeeklyMailServer, msgBody, msgSubject, MsgFromEmail, "xyz#abc.com", rptAttachment)
end using
Next

SmtpMail - Change the "From Address" to Name

I use SmtpMail for users to forward site content. The user fills out a form which includes first name and email.
The email sent has the full email address as the "From address" in the recipients inbox (they see From: Joe#Gmail.com while I want them to see From: Joe).
How can I format the "From address" to be the users inputted first name?
Thanks!
The MailAddress class has an optional parameter where you can specify a display name. I assume it will be used when present.
Dim from As MailAddress = New MailAddress("ben#contoso.com", "Ben Miller")
Dim to As MailAddress = New MailAddress("jane#contoso.com", "Jane Clayton")
Dim message As MailMessage = New MailMessage(from, to)
This has always worked for me:
Dim myMessage As New MailMessage
Dim myFrom As MailAddress = New MailAddress("bob#contoso.com", "Bob Denver")
Dim myTo As MailAddress = New MailAddress("steve#contoso.com", "Steve Miller")
myMessage.From = myFrom
myMessage.To.Add(myTo)
The format I ended up using was: mailer.From = name & "<" & emailer & ">"
This formats the from address to include Name as well as Email address. It will be displayed in most email clients as Joe <Joe#email.com>. This was my desired outcome.
Thank you Knslyr and lincolnk for the support.
this method displays 'Rameez' instead of 'Rameez#abc.com.pk'
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim objRecip As Recipient
Dim strMsg As String
Dim res As Integer
Dim strBcc As String
On Error Resume Next
strBcc = """Rameez"" <Rameez#abc.com.pk>"
Set objRecip = Item.Recipients.Add(strBcc)
objRecip.Type = olBCC
If Not objRecip.Resolve Then
strMsg = "Could not resolve the Bcc recipient. " & _
"Do you want still to send the message?"
res = MsgBox(strMsg, vbYesNo + vbDefaultButton1, _
"Could Not Resolve Bcc Recipient")
If res = vbNo Then
Cancel = True
End If
End If
Set objRecip = Nothing
End Sub