dba123
asked on
Failure Sending Mail / smtp.Send(message)
for some reason for this simple function I created, the portion smtp.Send(message) is throwing an error;
I get the error "Failure Sending Mail". I don't get anything much other than that in the error message...unles there's another place I should be looking.
This is ASP.NET 2.0 code using the System.Net.mail namespace:
Private Function SendEmail(ByVal exception_msg As String, ByVal place As String)
Dim message As New System.Net.Mail.MailMessag e("myemail @whatever. com", "myemail@whatever.com")
message.Subject = "Exception"
message.Body = "Error happened in this area" & place & "Error information: " & exception_msg
Dim smtp As New System.Net.Mail.SmtpClient ("Exchange servername")
smtp.Send(message) <-------------- Error happens here
End Function
The servername I'm puting in there is one I've used on other servers as an smtp client. I'm not sure if this just can't talk to external servers or not or just that I have a syntax problem.
I get the error "Failure Sending Mail". I don't get anything much other than that in the error message...unles there's another place I should be looking.
This is ASP.NET 2.0 code using the System.Net.mail namespace:
Private Function SendEmail(ByVal exception_msg As String, ByVal place As String)
Dim message As New System.Net.Mail.MailMessag
message.Subject = "Exception"
message.Body = "Error happened in this area" & place & "Error information: " & exception_msg
Dim smtp As New System.Net.Mail.SmtpClient
smtp.Send(message) <-------------- Error happens here
End Function
The servername I'm puting in there is one I've used on other servers as an smtp client. I'm not sure if this just can't talk to external servers or not or just that I have a syntax problem.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok, thanks. So I have to use the local server then and setup smtp on that I assume.
ASKER
this is not sending email to external domains. What I'm saying is I'm tryign to reference an internal mail server...and use that as the IP for the smtpClient
ASKER
the "relay server" in this case is our own internal exchange server...emails are only being sent to internal exchange mailboxes.
Try this:
Imports System.Web.Mail
Private Sub SendEmail(ByVal exception_msg As String, ByVal place As String)
Dim email As SmtpMail
Dim msg As New MailMessage
email.SmtpServer = "Exchange servername"
msg.Priority = MailPriority.Normal
msg.From = "myemail@whatever.com"
msg.To = "myemail@whatever.com"
msg.Subject = "Exception"
msg.Body = "Error happened in this area" & place & vbCrLf & "Error information: " & exception_msg
msg.BodyFormat = MailFormat.Html
email.Send(msg)
End Sub
Imports System.Web.Mail
Private Sub SendEmail(ByVal exception_msg As String, ByVal place As String)
Dim email As SmtpMail
Dim msg As New MailMessage
email.SmtpServer = "Exchange servername"
msg.Priority = MailPriority.Normal
msg.From = "myemail@whatever.com"
msg.To = "myemail@whatever.com"
msg.Subject = "Exception"
msg.Body = "Error happened in this area" & place & vbCrLf & "Error information: " & exception_msg
msg.BodyFormat = MailFormat.Html
email.Send(msg)
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
aki4u, just wondering, what is wrong with mine?
ASKER
aki4u, yours is not quite there, remember, this is .NET 2.0 and VS 2005 I'm using:
Dim email As SmtpMail
Error 3 Type 'SmtpMail' is not defined.
msg.From = "myemail@whatever.com"
Error 4 Value of type 'String' cannot be converted to 'System.Net.Mail.MailAddre ss'.
msg.To = "myemail@whatever.com"
Error 5 Property 'To' is 'ReadOnly'.
msg.BodyFormat = MailFormat.Html
Error 6 'BodyFormat' is not a member of 'System.Net.Mail.MailMessa ge'.
msg.BodyFormat = MailFormat.Html
Error 7 Name 'MailFormat' is not declared.
Dim email As SmtpMail
Error 3 Type 'SmtpMail' is not defined.
msg.From = "myemail@whatever.com"
Error 4 Value of type 'String' cannot be converted to 'System.Net.Mail.MailAddre
msg.To = "myemail@whatever.com"
Error 5 Property 'To' is 'ReadOnly'.
msg.BodyFormat = MailFormat.Html
Error 6 'BodyFormat' is not a member of 'System.Net.Mail.MailMessa
msg.BodyFormat = MailFormat.Html
Error 7 Name 'MailFormat' is not declared.
ASKER
nauman_ahmed, awesome links....thanks! I had forgotten to setup smtp on my server
Create a new page and try this:
Imports System.Web.Mail
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Call SendEmail("whatever", "whatever")
End Sub
Private Sub SendEmail(ByVal exception_msg As String, ByVal place As String)
Dim email As SmtpMail
Dim msg As New MailMessage
email.SmtpServer = "Exchange servername"
With msg
.Priority = MailPriority.Normal
.From = "myemail@whatever.com"
.To = "myemail@whatever.com"
.Subject = "Exception"
.Body = "Error happened in this area" & place & vbCrLf & "Error information: " & exception_msg
.BodyFormat = MailFormat.Html
End With
email.Send(msg)
End Sub
End Class
Imports System.Web.Mail
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Call SendEmail("whatever", "whatever")
End Sub
Private Sub SendEmail(ByVal exception_msg As String, ByVal place As String)
Dim email As SmtpMail
Dim msg As New MailMessage
email.SmtpServer = "Exchange servername"
With msg
.Priority = MailPriority.Normal
.From = "myemail@whatever.com"
.To = "myemail@whatever.com"
.Subject = "Exception"
.Body = "Error happened in this area" & place & vbCrLf & "Error information: " & exception_msg
.BodyFormat = MailFormat.Html
End With
email.Send(msg)
End Sub
End Class
I have converted a MSDN sample on this topic that also sends attachment with the mail. The code is translated using the utility at http://carlosag.net/Tools/CodeTranslator/Default.aspx:
Public Shared Sub CreateMessageWithAttachmen t(ByVal server As String)
' Specify the file to be attached and sent.
' This example assumes that a file named Data.xls exists in the
' current working directory.
Dim file As String = "data.xls"
' Create a message and set up the recipients.
Dim message As MailMessage = New MailMessage("jane@contoso. com", "ben@contoso.com", "Quarterly data report.", "See the attached spreadsheet.")
' Create the file attachment for this e-mail message.
Dim data As Attachment = New Attachment(file, MediaTypeNames.Application .Octet)
' Add time stamp information for the file.
Dim disposition As ContentDisposition = data.ContentDisposition
disposition.CreationDate = System.IO.File.GetCreation Time(file)
disposition.ModificationDa te = System.IO.File.GetLastWrit eTime(file )
disposition.ReadDate = System.IO.File.GetLastAcce ssTime(fil e)
' Add the file attachment to this e-mail message.
message.Attachments.Add(da ta)
'Send the message.
Dim client As SmtpClient = New SmtpClient(server)
' Add credentials if the SMTP server requires them.
client.Credentials = CredentialCache.DefaultNet workCreden tials
client.Send(message)
' Display the values in the ContentDisposition for the attachment.
Dim cd As ContentDisposition = data.ContentDisposition
Console.WriteLine("Content disposition")
Console.WriteLine(cd.ToStr ing)
Console.WriteLine("File {0}", cd.FileName)
Console.WriteLine("Size {0}", cd.Size)
Console.WriteLine("Creatio n {0}", cd.CreationDate)
Console.WriteLine("Modific ation {0}", cd.ModificationDate)
Console.WriteLine("Read {0}", cd.ReadDate)
Console.WriteLine("Inline {0}", cd.Inline)
Console.WriteLine("Paramet ers: {0}", cd.Parameters.Count)
For Each d As DictionaryEntry In cd.Parameters
Console.WriteLine("{0} = {1}", d.Key, d.Value)
Next
data.Dispose
End Sub
Public Shared Sub CreateMessageWithAttachmen
' Specify the file to be attached and sent.
' This example assumes that a file named Data.xls exists in the
' current working directory.
Dim file As String = "data.xls"
' Create a message and set up the recipients.
Dim message As MailMessage = New MailMessage("jane@contoso.
' Create the file attachment for this e-mail message.
Dim data As Attachment = New Attachment(file, MediaTypeNames.Application
' Add time stamp information for the file.
Dim disposition As ContentDisposition = data.ContentDisposition
disposition.CreationDate = System.IO.File.GetCreation
disposition.ModificationDa
disposition.ReadDate = System.IO.File.GetLastAcce
' Add the file attachment to this e-mail message.
message.Attachments.Add(da
'Send the message.
Dim client As SmtpClient = New SmtpClient(server)
' Add credentials if the SMTP server requires them.
client.Credentials = CredentialCache.DefaultNet
client.Send(message)
' Display the values in the ContentDisposition for the attachment.
Dim cd As ContentDisposition = data.ContentDisposition
Console.WriteLine("Content
Console.WriteLine(cd.ToStr
Console.WriteLine("File {0}", cd.FileName)
Console.WriteLine("Size {0}", cd.Size)
Console.WriteLine("Creatio
Console.WriteLine("Modific
Console.WriteLine("Read {0}", cd.ReadDate)
Console.WriteLine("Inline {0}", cd.Inline)
Console.WriteLine("Paramet
For Each d As DictionaryEntry In cd.Parameters
Console.WriteLine("{0} = {1}", d.Key, d.Value)
Next
data.Dispose
End Sub
ASKER
I want to know why mine wasn't working! my last post.
ASKER
aki4u, I had the correct namespace at the top of my page, why didn't your first example work in my .net page?
ASKER
I am workign in a console project, not an aspx
ASKER
Look, smtpMail doesn't exist in the namespace I'm using: see here:
http://www.webfound.net/smtpMail.jpg
where is it? I'm assuming that's asp.net 1.1 or 1.0 which is why...different namespace that 2.0 doesn't use in your example?
http://www.webfound.net/smtpMail.jpg
where is it? I'm assuming that's asp.net 1.1 or 1.0 which is why...different namespace that 2.0 doesn't use in your example?
ASKER
Ok folks. I'm using System.Net.Mail class so let's not focus on System.Web.Mail here...the syntax I've seen so far is System.Web.Mail. Mine is correct when using System.Net.Mail and that's what I'm sticking to.
dba123,
Which .NET 2.0 version are you using? The final version of .NET 2.0 do not have any parameters in the MailMessage constructor. The following simple code work in VS.NET 2005 without any errors:
Dim msg As MailMessage = New MailMessage
msg.To = "toemails@site.com"
msg.From = "fromemail@site.com"
msg.Subject = "Subject"
msg.Body = "Body"
SmtpMail.SmtpServer = "localhost"
SmtpMail.Send(msg)
-Nauman.
Which .NET 2.0 version are you using? The final version of .NET 2.0 do not have any parameters in the MailMessage constructor. The following simple code work in VS.NET 2005 without any errors:
Dim msg As MailMessage = New MailMessage
msg.To = "toemails@site.com"
msg.From = "fromemail@site.com"
msg.Subject = "Subject"
msg.Body = "Body"
SmtpMail.SmtpServer = "localhost"
SmtpMail.Send(msg)
-Nauman.
ASKER
so far nauman_ahmed is on target...I'm still testing my code and getting rid of errors and will get back to you.... ASP.NET 2.0!!! not 1.0 or 1.1 here so therefore I'm using System.Net.Mail!
woops dba123, I didn know about System.Net.Mail was introduced in 2.0. Thanks for the info. I tried compiling the following code and didn recieve any error:
Dim msg As MailMessage = New MailMessage("myemail@whate ver.com", "myemail@whatever.com")
msg.Subject = "Exception"
msg.Body = "Body"
Dim client As SmtpClient = New SmtpClient("localhost")
client.Send(msg)
Can you please tell if you recieve the error in compilation or running the application? If so what is the error code?
-Nauman.
Dim msg As MailMessage = New MailMessage("myemail@whate
msg.Subject = "Exception"
msg.Body = "Body"
Dim client As SmtpClient = New SmtpClient("localhost")
client.Send(msg)
Can you please tell if you recieve the error in compilation or running the application? If so what is the error code?
-Nauman.
Did you also include imports statement for System.Net.Mail?
-Nauman.
-Nauman.
ASKER
of course, I said I was using Imports System.Net.Mail am I missing another import?
Imports System.Net.Mail
Module Module1
Sub Main()
Dim msg As MailMessage = New MailMessage("myemail@whate ver.com", "myemail@whatever.com")
msg.Subject = "Exception"
msg.Body = "Body"
Dim client As SmtpClient = New SmtpClient("localhost")
client.Send(msg)
End Sub
End Module
Tried the above code and it was compiled.
-Nauman.
Module Module1
Sub Main()
Dim msg As MailMessage = New MailMessage("myemail@whate
msg.Subject = "Exception"
msg.Body = "Body"
Dim client As SmtpClient = New SmtpClient("localhost")
client.Send(msg)
End Sub
End Module
Tried the above code and it was compiled.
-Nauman.
ASKER
I forget how to find the version I'm using....
ASKER
on the server .net 2.0.50727 which is where my code lies. On mylocal PC, not sure...
Console.WriteLine(System.E nvironment .Version.T oString())
-Nauman.
-Nauman.
ASKER
Errors with my code below:
message.From = "someemailaddress"
Error 3 Value of type 'String' cannot be converted to 'System.Net.Mail.MailAddre ss'.
message.To = "myemailaddress"
Error 4 Property 'To' is 'ReadOnly'.
Private Function SendEmail(ByVal exception_msg As String, ByVal place As String)
Dim message As New MailMessage
message.From = "someemailaddress"
message.To = "myemailaddress"
message.Subject = "HEDI 2 Exception"
message.IsBodyHtml = True
message.Body = "Error happened in this area" & place & "Error information: " & exception_msg
Dim smtp As New SmtpClient("relayservernam e")
smtp.Send(message)
End Function
I'm not sure why I get the read-only error, is there some sort of property I need to add?
message.From = "someemailaddress"
Error 3 Value of type 'String' cannot be converted to 'System.Net.Mail.MailAddre
message.To = "myemailaddress"
Error 4 Property 'To' is 'ReadOnly'.
Private Function SendEmail(ByVal exception_msg As String, ByVal place As String)
Dim message As New MailMessage
message.From = "someemailaddress"
message.To = "myemailaddress"
message.Subject = "HEDI 2 Exception"
message.IsBodyHtml = True
message.Body = "Error happened in this area" & place & "Error information: " & exception_msg
Dim smtp As New SmtpClient("relayservernam
smtp.Send(message)
End Function
I'm not sure why I get the read-only error, is there some sort of property I need to add?
dba123,
your code also works, but message it's not instantly sent, like in my example.
Code I sent you works for 1.1/2.0 framework.
the simplest code I can think of is this(also works in 2.0):
Dim smtp As New System.Net.Mail.SmtpClient
smtp.Host = "Exchange servername"
smtp.Send("myemail@whateve r.com", "myemail@whatever.com", "Subject", "Test")
your code also works, but message it's not instantly sent, like in my example.
Code I sent you works for 1.1/2.0 framework.
the simplest code I can think of is this(also works in 2.0):
Dim smtp As New System.Net.Mail.SmtpClient
smtp.Host = "Exchange servername"
smtp.Send("myemail@whateve
ASKER
you have to typecast the To addresses now...see article above...
ASKER
that article I found tells it all folks. Thanks for all your help though!!!
Private Function SendEmail(ByVal exception_msg As String, ByVal place As String)
Dim message As New MailMessage
Dim fromAddress As New MailAddress("mike@elsewher e.com", "Mike")
Dim toAddress As New MailAddress("mike@work.com ", "Mike")
Dim msessage As New MailMessage(fromAddress, toAddress)
message.Subject = " Exception"
message.IsBodyHtml = True
message.Body = "Error happened in this area" & place & "Error information: " & exception_msg
Dim smtp As New SmtpClient("myrelayservern ame")
smtp.Send(message)
End Function
Private Function SendEmail(ByVal exception_msg As String, ByVal place As String)
Dim message As New MailMessage
Dim fromAddress As New MailAddress("mike@elsewher
Dim toAddress As New MailAddress("mike@work.com
Dim msessage As New MailMessage(fromAddress, toAddress)
message.Subject = " Exception"
message.IsBodyHtml = True
message.Body = "Error happened in this area" & place & "Error information: " & exception_msg
Dim smtp As New SmtpClient("myrelayservern
smtp.Send(message)
End Function
static void Authenticate()
{
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("me@mycompany.
mail.To.Add("you@yourcompa
//set the content
mail.Subject = "This is an email";
mail.Body = "this is the body content of the email.";
//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
//to authenticate we set the username and password properites on the SmtpClient
smtp.Credentials = new NetworkCredential("usernam
smtp.Send(mail);
}