Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
When i send email using visual studio 2013 it works but the same code does not work in visual studio 2015.

Please Help !
I have also tried to change port no but doesn't work.
Any will be Appreciated, Thanks In Advance

What I have tried:

Dim s As String
s = vbNewLine
Dim targetURL As String
targetURL = "http://xyz.aspx"
Dim client As New SmtpClient()
client.Host = "abc.mail.com"
Dim sendTo As New MailAddress("wer.mail.com")
Dim from As MailAddress = New MailAddress("qwe.mail.com")
Dim message As New MailMessage(from, sendTo)
message.IsBodyHtml = True
message.Subject = "Rejoining Approval"
' message.SubjectEncoding
Dim body As String

body = "Respected Sir" & vbCrLf &
"I would like to inform you that" & Space(2) & "" & cmbdropdownlist.Text & "" & Space(2) & "has been resigned from" & Space(2) & "" & cmbUnit.Text & "" & vbCrLf &
"and wants to rejoin" & vbCrLf &
"So,Kindly take proper action on it with this given below Link." & vbCrLf &
"" & targetURL & "" & vbCrLf &
"Thanks & Regards" & vbCrLf &
" HRD "
message.Body = body
' Use the same account in app.config to authenticate.
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network
Dim basicAuthenticationInfo As New System.Net.NetworkCredential("qwe.mail.com", "erty")
client.UseDefaultCredentials = False
client.Credentials = basicAuthenticationInfo
client.Port = 25

client.EnableSsl = True

'Try
client.Send(message)
MsgBox("Rejoining Info has been send successfully to Senior", MsgBoxStyle.Information)
Posted
Updated 17-Feb-17 21:49pm
Comments
Afzaal Ahmad Zeeshan 18-Feb-17 2:50am    
Ok, so what is the error message that you get?
Anjani Rawat 18-Feb-17 3:18am    
An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll

Afzaal Ahmad Zeeshan 18-Feb-17 3:31am    
And the message in front of that: Failure sending mail? Or what?
Anjani Rawat 18-Feb-17 3:42am    
Yes that same message "Failure sending Mail" also comes,below the error meassage

Like that :

An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll

"Failure sending Mail"


1 solution

These are a bit general problems in SMTP programming, not just in .NET framework but almost in every platform because the server requires something out of us, and we are not providing those content details. A few things to consider are to make sure that your SMTP server is configured to accept the requests to send email from control panel. You should try the following code with any of the provider, such as Gmail or Outlook and see if they work correctly.
' You should use a using statement
Using client As New SmtpClient("<smtp-server-address>", 25)
	' Configure the client
	client.EnableSsl = True
	client.Credentials = New NetworkCredential("<username>", "<password>")
	' client.UseDefaultCredentials = true;

	' A client has been created, now you need to create a MailMessage object
	' From field
	' Recipient field
	' Subject of the email message
		' Email message body
	Dim message As New MailMessage("from@example.com", "to@example.com", "Hello", "World!")

	' Send the message
	client.Send(message)

	' 
'    * Since I was using Console app, that is why I am able to use the Console
'    * object, your framework would have different ones. 
'    * There is actually no need for these following lines, you can ignore them
'    * if you want to. SMTP protocol would still send the email of yours. 


	' Print a notification message
	Console.WriteLine("Email has been sent.")
	' Just for the sake of pausing the application
	Console.Read()
End Using

'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik
'Facebook: facebook.com/telerik
'=======================================================

The code was captured and converted from my article, Sending emails over .NET framework, and general problems – using C# code[^] and at the end of that article I talk about a few problems and "Failure sending mail" is one of them. In your code, I see no problem: 1) No http in hostname. 2) 25 port used. 3) username/password set.

The problem should be on the server, check that you are allowed to send the emails; it would be under the SMTP server's control panel. Otherwise, try the same VB.NET code for any other provider; Gmail, Outlook and see the results.
 
Share this answer
 
Comments
Anjani Rawat 20-Feb-17 0:24am    
Hello Afzaal,

I have outlook configured on my machine and it work's fine, also communicating with server but my concern is that why this code does gives above mentioned error in visual studio 2015 on another machine but works fine on visual studio 2013 in my machine.


Please Review this issue.
Afzaal Ahmad Zeeshan 20-Feb-17 7:17am    
Again, since the issue is on another machine, the problem would be with the Firewall. There are several other issues that we cannot look into from this position. However, the solution is same: 1) Check if firewall permits communication. 2) Application can connect to the server. 3) Server allows the communication by the account being used. Since the problem is not with authentication (yet), you can check if the account has allowed SMTP.

Various other stuff to look into. :-)

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