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?
I have tried different port numbers but I keep getting the error message and no email sent. I aim to be able to send the email successfully and the message will contain details from an appointment booked. Any help please?
Dim UserName As String = "example#gmail.com"
Dim mail As MailMessage = New MailMessage
mail.From = New MailAddress(UserName)
mail.To.Add(New MailAddress(txtEmailAddress.Text))
mail.Subject = "Appointment Details"
mail.Body = "Test message"
mail.IsBodyHtml = True
Dim client As SmtpClient = New SmtpClient("smtp.gmail.com", 465)
client.EnableSsl = True
client.Credentials = New System.Net.NetworkCredential(UserName, "***")
Try
client.Send(mail)
Catch ex As Exception
MessageBox.Show("Sending email failed. Please Try again")
End Try
What you need to do is add client.UseDefaultCredentials = False, but make sure you set it to false before calling client.Credentials = New System.Net.NetworkCredential(UserName, "***")
Also, for GMail, you need to use port 587 which is the port that supports the STARTTLS extension (which is the only way that System.Net.Mail.SmtpClient supports SSL).
In other words, change your code to this:
Dim UserName As String = "example#gmail.com"
Dim mail As MailMessage = New MailMessage
mail.From = New MailAddress(UserName)
mail.To.Add(New MailAddress(txtEmailAddress.Text))
mail.Subject = "Appointment Details"
mail.Body = "Test message"
mail.IsBodyHtml = True
Dim client As SmtpClient = New SmtpClient("smtp.gmail.com", 587)
client.EnableSsl = True
client.UseDefaultCredentials = False
client.Credentials = New System.Net.NetworkCredential(UserName, "***")
Try
client.Send(mail)
Catch ex As Exception
MessageBox.Show("Sending email failed. Please Try again")
End Try
Try this,
go to your gmail account (www.gmail.com)
login, and check that you have "enabled Pop for all mail".
you can find this under the tab "Forwarding and POP/IMAP"
Also try switching the port number to 587
I wanna send an Email from my Webservice, written in vb.net, but I get an Timeout.
What is wrong?
Dim toaddress As MailAddress = New MailAddress("xxx")
Dim fromaddress As MailAddress = New MailAddress("yyy")
' The structure for MailMessage(from, to)
Dim message As MailMessage = New MailMessage(fromaddress, toaddress)
message.Subject = "I have sent you a message from a program!"
message.Body = "Hello World!"
Dim messanger As SmtpClient = New SmtpClient("smtpxxx", 995)
messanger.Credentials = New NetworkCredential("user", "password")
messanger.EnableSsl = True
messanger.Send(message)
You are doing many things wrong and some of the information you have given are not clear enough. host name and port names have to give properly. it chooses which mail service you are using, from your code it is not correctly provided, so i will give a snippet that uses gmail to send mail. please go through this and make changes as per your SmtpHost.
Try
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential("email", "password")
Smtp_Server.Port = 587
Smtp_Server.EnableSsl = True
Smtp_Server.Host = "smtp.gmail.com"
e_mail = New MailMessage()
e_mail.From = New MailAddress(txtemail.Text)
e_mail.To.Add(txtemail.Text)
e_mail.Subject = "Email Sending"
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text
Smtp_Server.Send(e_mail)
MsgBox("Mail Sent")
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
This link help you to find the smtp.Server and Port numbers, you can use this thread for check the limitations of smtp.servers
I am trying to make a simple e-mail sending program but it has confused me when it said object reference not set to an instance of an object
Try
Dim mail As MailMessage
Dim client As SmtpClient
Dim SmtpServer As New SmtpClient()
Dim fromAddress As String
Dim toAddress1 As String
Dim toAddress2 As String
Dim toAddress3 As String
Dim subject As String
Dim message As String
SmtpServer.Credentials = New Net.NetworkCredential("*********#gmail.com", "**********")
client.UseDefaultCredentials = False
fromAddress = FromEmail.Text
If ToEmail1.Visible = True Then
toAddress1 = ToEmail1.Text
ElseIf ToEmail1.Visible = True And ToEmail2.Visible = True Then
toAddress1 = ToEmail1.Text
toAddress2 = ToEmail2.Text
ElseIf ToEmail1.Visible = True And ToEmail2.Visible = True And ToEmail3.Visible = True Then
toAddress1 = ToEmail1.Text
toAddress2 = ToEmail2.Text
toAddress3 = ToEmail3.Text
End If
subject = "Subject"
Message = "Message"
mail = New MailMessage(fromAddress, toAddress1, subject, Message)
client = New SmtpClient("Smtp.live.com")
client.Port = 587
Dim SMTPUserInfo As New System.Net.NetworkCredential("user#hotmail.com", "password")
client.Credentials = SMTPUserInfo
client.Send(mail)
MsgBox("sent")
Catch ex As Exception
MsgBox(ex.Message.ToString())
End Try
you're declaring two smtpclients
Dim client As SmtpClient
Dim SmtpServer As New SmtpClient()
you instantiated SmtpServer, but not client. Then you do
client.UseDefaultCredentials = False
I guess this is where the exception is raised.
the code looks like having some lines to send some mail through SmtpServer from a google account and other lines to send some mail through client from a hotmail account. Besides, SmtpServer is using some server defined in config.sys.
I have a LINQ VB.NET project. I have a table "User". I enter all the user details into the table. This includes and email address. I have a method which randomly generates a password. I have another method which calls an email address, based on what has been entered in a text box, and sends the password to that email address. My problem is that I can't figure out how to insert the newly generated password into the row belonging to the email address I have just called. The text box that the email address is entered into is "txtEmail". It can be assumed that the entry a "User" has already been completed, minus the password. The following method generates the password:
Public Function GeneratePassword() As String
'String variables tells how many characters password will contain.
Dim PasswordLength As Int16 = "12"
'Empty string will hold randomly generated password
Dim NewPassword As String = ""
'Characters allowed in this new password
Dim allowedChars As String = ""
allowedChars = "1,2,3,4,5,6,7,8,9,0"
allowedChars += "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,"
allowedChars += "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,"
'Declare array to house each character generated from allowed list of characters
Dim sep As Char() = {","c}
Dim arr As String() = allowedChars.Split(sep)
Dim IDString As String = ""
Dim temp As String = ""
'utilize the "random" class
Dim rand As New Random()
'loop through the character generation process until PasswordLength is met
For i As Integer = 0 To Convert.ToInt32(PasswordLength) - 1
temp = arr(rand.[Next](0, arr.Length))
IDString += temp
NewPassword = IDString
Next
'places new password in variable
Return NewPassword
End Function
Following on from that the following method fetches the email address from the database, calls the "GeneratePassword" method and emails the password to the user. It does not save the password to the database, which is the problem I have.
Protected Sub btnPassword_Click(sender As Object, e As System.EventArgs) Handles btnPassword.Click
If True Then
'establish connection with database
Dim db As New OrionDataClassesDataContext()
'initialise global variabel to take email string entered in text box
GlobalVariables.SearchUserEmail = txtEmailAddress.Text
'initialise global variable to hold email address retrieved from database
GlobalVariables.CurrentEmailAddress = (From u In db.Users
Where u.Email = SearchUserEmail
Select u.Email).FirstOrDefault
'calls generatepassword method
Dim stNewPassword As String = GeneratePassword()
'create new mailmessage to construct and sendemail
Dim Mail As New MailMessage
Try
Mail.Subject = "Your Password Has Been Reset"
'gets email address from database when user enters it
Mail.To.Add(GlobalVariables.CurrentEmailAddress)
'address for email to be sent from
Mail.From = New MailAddress("XXX#XXXX.com")
Mail.Body = "Your new pasword is: " + stNewPassword + ". Please keep this safe."
'define smpt server to be used
Dim SMTP As New SmtpClient("smtp.gmail.com")
SMTP.EnableSsl = True
'authenticate connection with smpt server and email address
SMTP.Credentials = New System.Net.NetworkCredential("XXXX#XXX.com", "XXXXX")
SMTP.Port = 587
SMTP.Send(Mail)
'alert when message is sent correctly
MsgBox("Email sent successfully!")
txtEmailAddress.Text = ""
Catch ex As Exception
'displays error message when email is not sent
MsgBox("Email not sent - Check address")
txtEmailAddress.Text = ""
End Try
End If
End Sub
I have been playing around with a few scenarios but I always seem to end up just trying to create a brand new row entry, which is not what I want to do. Any advice would be appreciated.