System.Net.Mail Display Name Fails - vb.net

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)

Related

What is the best way to send emails via VB using O365 domain?

I'm trying to send emails automatically, the configuration below worked perfectly with Gmail, but now with office365. I can't send the emails, I need help.
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.UseDefaultCredentials = False
SmtpServer.Credentials = New Net.NetworkCredential("xxx#xx.com", "xxxx")
SmtpServer.Port = 587
SmtpServer.Host = "smtp.office365.com"
SmtpServer.EnableSsl = True
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
mail = New MailMessage()
mail.From = New MailAddress("xxx#xx.com")
mail.To.Add(listaCorreo)
mail.Subject = " string "
mail.Body = "string "
mail.IsBodyHtml = True
SmtpServer.Send(mail)
Catch ex As Exception
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine("Class -\> ClassProcessingError, Method -\> MyContactByMail, Error -\> " & ex.Message)
End Try

How to search for files and attach them to a Email

I have an CheckedListbox (imported from mysql Database) that lists all my folders where my files are saved. And what I need to do is for every checked item in the CheckListbox, to search the folder and then attach the file from that folder to a Mail.
What I have done is to create an Email and to send it with only one attachment.
Just with the: "e_mail. Attachments. Add". The Mail will be generated and that found file will be attached and send.
But when I do that in a FOR EACH LOOP then no file will be selected and added to my Mail. The Mail will be generated and send without any errors, but there are no attached files.
Private Sub SendMail()
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential("MAIL", "PASS")
Smtp_Server.Port = 587
Smtp_Server.EnableSsl = True
Smtp_Server.DeliveryMethod = SmtpDeliveryMethod.Network
Smtp_Server.Host = "HOST"
If MonthTextBox.Text = "" Or MonthTextBox.ForeColor = Color.Silver Then
MsgBox("Input month please", Title:="MO Text Box")
Else
Dim MOTextValue As String = MonthTextBox.Text
Dim nowYear As Integer = Date.Now.Year
For Each itemchecked As DataRowView In CheckedListBoxWO.CheckedItems
Dim File_path As String = "C:\Test\" & itemchecked.Item(1) & "\" & nowYear & "\" & MOTextValue & "\"
Dim File_Name As String = Dir("C:\Test\" & itemchecked.Item(1) & "\" & nowYear & "\" & MOTextValue & "\File " & MOTextValue & "*.xlsx")
Dim attachmentFile As Net.Mail.Attachment = New Net.Mail.Attachment(File_path & File_Name)
e_mail.Attachments.Add(attachmentFile)
Next
Dim sb As New System.Text.StringBuilder
sb.AppendLine("Hello,<br />")
sb.AppendLine("<br />")
sb.AppendLine("Test Attachment,<br />")
sb.AppendLine("<br />")
sb.AppendLine("------------------------------------------<br />")
e_mail.From = New MailAddress("MY MAIL")
e_mail.To.Add("EMAIL")
e_mail.Subject = ("TESTFILE - " & MOTextValue & "")
e_mail.SubjectEncoding = System.Text.Encoding.UTF8
e_mail.IsBodyHtml = True
e_mail.Priority = MailPriority.Normal
e_mail.Body = sb.ToString()
e_mail.BodyEncoding = System.Text.Encoding.UTF8
Smtp_Server.Send(e_mail)
End If
MsgBox("Mails was sent", Title:="Mail")
End Sub
Like I told with this code the Mail will be created and send, but there is no Attachment file in it.
If I do it without the "FOR EACH"... loop, then one File will be added without a problem. But I need to add more files, actually for every checked Item in the CheckedListBox my program should search that folder and if there is a file than attach it in the Mail, and look in the other folder and so on...
Your adding attachments then newing up your e_mail object again, this will wipe your existing data, try removing the indicated line below:
sb.AppendLine("<br />")
sb.AppendLine("------------------------------------------<br />")
e_mail = New MailMessage() <--- remove this line
e_mail.From = New MailAddress("MY MAIL")
e_mail.To.Add("EMAIL")
The code is actually working, my problem was that the reference to this CheckListBox was incorrect, I have multiple CheckListBoxes, and that one in the Code was not the one I was actually using... But looking it from the bright side, THE CODE IS WORKING :)

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?

Email form: personalised 'thank you' page

I have this in a VB.NET contact form:
Protected Sub btnSubmit_Click1(sender As Object, e As EventArgs)
Dim mail As New MailMessage()
mail.To.Add(txtEmail.Text)
mail.From = New MailAddress("info#mySite.net")
mail.Subject = "Contact Form"
'mail.Bcc = New MailAddress
mail.Body = txtName.Text & vbCrLf & txtComments.Text
Dim smtp As New SmtpClient("mail.server")
smtp.Credentials = New NetworkCredential("EMAIL ID", "PWD")
smtp.Send(mail)
Response.Redirect("thankyou.aspx" & mail.To)
lblStatus.Text = "Your data has been submitted successfully"
txtName.Text = ""
txtEmail.Text = ""
txtComments.Text = ""
End Sub
The mail.To in Response.Redirect is giving me an error:
Operator '&' is not defined for types 'String' and 'System.Net.Mail.MailAddressCollection'.
I imagine I need to create a separate personalised aspx 'thank you' page, so I need a way of getting the user's name from the form field data.
Thanks for any advice.
Do you mean something like this:
Response.Redirect("thankyou.aspx" & Request.QueryString["email.Text"];
Mail.To is a collection. Try email.text (which is what you passed to the mail address).

Request Read Receipt on Email when useing smtpserver

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);