Request Read Receipt on Email when useing smtpserver - vb.net

I was sucessful in sending email using smtpserver. I am writing a VB.Net application.
My current code is:
SmtpServer.Credentials = New Net.NetworkCredential(mEMailUser, mEMailPassword)
SmtpServer.Port = 587
SmtpServer.Host = mHostName
mail = New MailMessage()
mail.From = New MailAddress(mFromEMail)
mail.To.Add(mfrmSendAnEmail.txtTo.Text)
mail.Subject = mfrmSendAnEmail.txtSubject.Text
_Attachment = mDirectory & "\" & gcloGlobals.DocumentName & ".pdf"
Dim oAttch As Attachment = New Attachment(_Attachment)
mail.Attachments.Add(oAttch)
mail.Body = mfrmSendAnEmail.txtBody.Text
SmtpServer.Send(mail)
I want the Recipient to get a read request notification so that when they open the email they will have the option of sending the sender a notification that it was read.
I thought this might work:
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess
but it didn't.

Here's the code I use:
message.Headers.Add("Disposition-Notification-To", "\"" + fromAddress.DisplayName + "\" <" + fromAddress.Address + ">");
In your code you don't set a display name for the from address, so you should use the simpler form instead of the line given above:
message.Headers.Add("Disposition-Notification-To", mFromEMail);

Related

Cant attached printed pdf document as email attachment in VB.net

I am able to create a pdf document and I can browse the folder and open the document with no issue. But when my code try to attach the file as an attachment then it fails with this error, but the path and filename is correct. I suspect the file is somehow open which prevents the attachment.
Proof of files:
Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.dll
Could not find file 'C:\TEMP\1104280343081_INV0950.pdf'.
This part works perfectly
Dim txtVarFile As String
Dim txtVarEmail As String = "test#test.com"
Dim txtVarPasss As String = "password"
Dim txtVarSMTP As String = "smtp.gmail.com"
Dim intVarPort As Integer = 587
Dim txtVarDescription As String
'Create Invoice and Save as pdf document
txtVarFile = "C:\TEMP\" & strClientNum & "_" & strInvNum & ".pdf"
PageSetupDialog1.Document = PrintDocument1
PageSetupDialog1.PrinterSettings.DefaultPageSettings.Landscape = True
prtFrmInvoice.PrinterSettings = PageSetupDialog1.PrinterSettings
If prtFrmInvoice.PrinterSettings.IsValid Then
prtFrmInvoice.PrinterSettings.PrinterName = "Microsoft Print to PDF"
prtFrmInvoice.PrintFileName = txtVarFile
prtFrmInvoice.PrintAction = Printing.PrintAction.PrintToFile
prtFrmInvoice.Print()
End If
But this does not work, it tells me the file can not be found, but the file is there
Exception is thrown at this line: eMail.From = New MailAddress(txtVarEmail)
'Send copy of Invoice per Email
Try
Dim SmtpServer As New SmtpClient()
Dim eMail As New MailMessage()
'Dim attachment As System.Net.Mail.Attachment
LogFile.Refresh()
SmtpServer.UseDefaultCredentials = False
SmtpServer.Credentials = New Net.NetworkCredential(txtVarEmail, txtVarPasss)
SmtpServer.Port = intVarPort
SmtpServer.EnableSsl = True
SmtpServer.Host = txtVarSMTP
eMail = New MailMessage()
eMail.From = New MailAddress(txtVarEmail)
eMail.To.Add(strClientEmail)
eMail.Subject = "AltHealth Invoice"
eMail.Body = "Please find your latest invoice attached"
'attachment = New System.Net.Mail.Attachment(txtVarFile)
'eMail.Attachments.Add(attachment)
eMail.Attachments.Add(New Attachment(txtVarFile))
SmtpServer.Send(eMail)
MsgBox("The Invoice has been sent sucessfully via email - File: " & txtVarFile)
Catch ex As Exception
MsgBox("Send failure: " & ex.ToString())
End Try

Send Email is going to JUNK when provided the display name

I am sending email using smtp client from office 365 account . below is the code that i am using to send the email. The email is going to the JUNk folder in outlook and also the From address is showing as EmployeeName# <yyy.com noreply#xxx.com> instead of EmployeeName#yyy.com <noreply#xxx.com>. So can any one please give me any solution to fix this issue
Dim client As New SmtpClient()
Dim message As New MailMessage()
Dim par_ReplyName As String = "noreply#xxx.com"
Dim par_ReplyAddress As String = "EmployeeName#yyy.com"
message.From = New MailAddress(par_ReplyName, par_ReplyAddress)
message.ReplyToList.Add(par_ReplyAddress)
message.Priority = MailPriority.Normal
message.Subject = "Test Email"
message.Body = "some HTML content"
message.IsBodyHtml = True
client.Host = "smtp.office365.com"
client.Port = 587
client.Credentials = New System.Net.NetworkCredential(smtpUser, smtpPassword)
client.EnableSsl = True
client.Send(message)
Here's how you're using the MailAddress constructor:
message.From = New MailAddress(par_ReplyName, par_ReplyAddress)
Here's the declaration for that constructor from the documentation:
'Declaration
Public Sub New ( _
address As String, _
displayName As String _
)
Do you see anything amiss there, like maybe you have the arguments the wrong way around?

System.Net.Mail Display Name Fails

I'm currently using System.Net.Mail in the following fashion:
Message.BodyEncoding = Encoding.UTF8
Message.From = New System.Net.Mail.MailAddress(EMail)
Message.IsBodyHtml = True
Message.Subject = theSubject
Message.Body = theBody
Dim client As New System.Net.Mail.SmtpClient()
client.UseDefaultCredentials = False
client.Credentials = New System.Net.NetworkCredential(EMail, EMailPwd)
client.EnableSsl = True
client.Host = "smtp.gmail.com"
client.Port = 587
Sending it this way works fine. But when trying to specify a display name:
Message.From = New System.Net.Mail.MailAddress(EMail, firstName & " " & lastName)
I never receive the message, and it doesn't throw an exception. I've also tried formatting it this way:
Message.From = New System.Net.Mail.MailAddress(fullName & " <" & EMail & ">")
Is there a reason this keeps failing without giving any sort of error? Is there a restriction from Gmail's SMTP server that's blocking it, perhaps?
After messing with it, I was able to get it to send by specifying a unicode encoding type for the from address:
Message.From = New System.Net.Mail.MailAddress(EMail, FirstName & " " & LastName, Encoding.Unicode)

SMTP BCC feature in VB.net

I am looking to add BCC mail feature in the following code. How can I make changes in the following code to send an auto email to more than one email's
Dim SMTPServer As New SmtpClient()
Dim Mail As New MailMessage()
SMTPServer.Credentials = New Net.NetworkCredential(" ")
SMTPServer.Port = 587
SMTPServer.Host = " "
Mail = New MailMessage
Mail.From = New MailAddress(" ")
Mail.To.Add(dr("").ToString)
Mail.Subject = "Patient Assignment"
Mail.Body = " "
SMTPServer.Send(Mail)

Adding multiple lines to body of SMTP email VB.NET

I can use this code to send an email on my Exchange server
Try
Dim SmtpServer As New SmtpClient
Dim mail As New MailMessage
SmtpServer.Credentials = New Net.NetworkCredential()
SmtpServer.Port = 25
SmtpServer.Host = "email.host.com"
mail = New MailMessage
mail.From = New MailAddress("myemail#email.com")
mail.To.Add("otheremail#email.com")
mail.Subject = "Equipment Request"
mail.Body = "This is for testing SMTP mail from me"
SmtpServer.Send(mail)
catch ex As Exception
MsgBox(ex.ToString)
End Try
But how can I add multiple lines to the body?
Just treat it like a normal text object where you can use Environment.NewLine or vbNewLine between sentences.
StringBuilder is useful here:
Dim sb As New StringBuilder
sb.AppendLine("Line One")
sb.AppendLine("Line Two")
mail.Body = sb.ToString()
I would create a variable for your body and then add that to the mail.Body so it would look something like this.
Try
Dim strBody as string = ""
Dim SmtpServer As New SmtpClient
Dim mail As New MailMessage
SmtpServer.Credentials = New Net.NetworkCredential()
SmtpServer.Port = 25
SmtpServer.Host = "email.host.com"
mail = New MailMessage
mail.From = New MailAddress("myemail#email.com")
mail.To.Add("otheremail#email.com")
mail.Subject = "Equipment Request"
strBody = "This is for testing SMTP mail from me" & vbCrLf
strBody += "line 2" & vbCrLf
mail.Body = strBody
SmtpServer.Send(mail)
catch ex As Exception
MsgBox(ex.ToString)
End Try
That will append the line breaks and you should have each line on it's own in the email.
If the body of your message needs to be in HTML format, add the <br> tags right in your String. vbCrLf and StringBuilder don't work if the body is in HTML format.
Dim mail As New MailMessage
mail.IsBodyHtml = True
mail.Body = "First Line<br>"
mail.Body += "Second Line<br>"
mail.Body += "Third Line"
If it is not in HTML format, the other answers here appear to be good.
Like this?
Dim myMessage as String = "This is for testing SMTP mail from me" + Environment.NewLine
myMessage = myMessage + "Line1" + Environment.NewLine
then
mail.Body = myMessage
try the system.environment.newline in the the string ... should work
What it works for me is using in the string..
strBody = "This is for testing SMTP mail from me<BR> line 2"