Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending mails by .NET 2

Status
Not open for further replies.

tg2003

IS-IT--Management
Feb 6, 2003
270
0
0
IL
Hi,

I'm planning an app that will send mails when error occur. This app is a turn-key solution and intended to be delivered to organization and firms.

Currently I'm planning to send it as SMTP client, so I have these questions:
1. Does VB.NET let me send emails by SMTP, or should I use another 3rd party component? If yes - How?

2. Should I think about another protocols of sending mails, like MAPI or POP3?
FAIK, most of the organizations use SMTP, while a few use MAPI. I don't think that organizations use POP3.
If you think quite different, please tell me.

Thanks!
 
1: It's pretty simple to send the mail, although there are a few things to be aware of, such as firewall restrictions. Also you will want to allow for a way to receive messages back from the server your sending to in the event of bad email addresses, bounced messages for other reasons.

2: POP stands for post office protocol and is for recieving. Depending on how you approach the items I mentioned in #1 above, this is most likely unimportant.
 
Thanks to both of you.

I've been wanting to update an old VB6 app.

This should do it.

Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
SavantMan, Can I jump in and ask a question here? I am already sending emails through our Windows 2003 server and have incorporated a Try-Cath-Endtry around the Send portion in my code. Is this what you were referring to in item # 1 of your post to receive messages back from the server, or is there something else I should be doing? Thanks in advance!

Auguy
Northwest Ohio
 
Actually what I'm referring to is return messages from the target server. An example would be if you were to send to a email address that doesn't exist on the target domain. The server should send a message back stating that. Your try/catch will not find any issues because your mail may be formatted correctly. While you probably don't need or even want to write anything to recieve return messages, the return address noted in your mail object should be a valid one and one that is checked.
 
Thanks for the clarification. Because many of the emails I send are automated, I put an address in the "From" of the email that is made up of a group of employees to make sure someone sees any returned mail. Thanks again.

Auguy
Northwest Ohio
 
The above sample is only in C# and C++

Is there any VB.Net code I could perhapse look at?

I did try to use the System.web.mail class but that now seems to be Obsolete....

This seems more and more complex to do with every new version of VB.net



Lennieboy -
Life is Good and God is Good
B-)
 
Look in the FAQ's for Automatic Email, that should help.

Auguy
Northwest Ohio
 
Lennie,

I translated to VB - note that I didn't test it and I removed the special character handling parts that were in c#, but it should get you started.

Code:
Imports System
Imports System.Net
Imports System.Net.Mail
Imports System.Net.Mime
Imports System.Threading
Imports System.ComponentModel

Namespace Examples.SmptExamples.Async
   Public Class SimpleAsynchronousExample

      Shared mailSent As Boolean = False

      Shared Sub SendCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
         ' Get the unique identifier for this asynchronous operation.
         Dim token As String = DirectCast(e.UserState, String)

         If (e.Cancelled) Then
            Console.WriteLine("[{0}] Send canceled.", token)
         Else
            If (Not e.Error Is Nothing) Then
               Console.WriteLine("[{0}] {1}", token, e.Error.ToString())
            Else
               Console.WriteLine("Message sent.")
            End If
         End If

         mailSent = True
      End Sub

      Shared Sub Main(ByVal args As String())
         ' // Command line argument must the the SMTP host.
         Dim client As SmtpClient = New SmtpClient(args(0))
         ' // Specify the e-mail sender.

         ' // Create a mailing address 
         ' // in the display name.
         Dim from As MailAddress = New MailAddress("jane@contoso.com", _
                                                  "Jane Clayton", _
                                                   System.Text.Encoding.UTF8)

         ' // Set destinations for the e-mail message.
         Dim toAddress As MailAddress = New MailAddress("ben@contoso.com")

         ' // Specify the message content.
         Dim message As MailMessage = New MailMessage(from, toAddress)
         Message.Body = "This is a test e-mail message sent by an application. "

         ' // Set the method that is called back when the send operation ends.
         AddHandler client.SendCompleted, AddressOf SendCompletedCallback

         ' // The userState can be any object that allows your callback 
         ' // method to identify this send operation.
         ' // For this example, the userToken is a string constant.
         Dim userstate As String = "test message1"
         client.SendAsync(message, userstate)
         Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.")
         Dim answer As String = Console.ReadLine()

         ' // If the user canceled the send, and mail hasn't been sent yet,
         ' // then cancel the pending operation.
         If (answer.StartsWith("c") AndAlso mailSent = False) Then
            client.SendAsyncCancel()
         End If

         ' // Clean up.
         Message.Dispose()

         Console.WriteLine("Goodbye.")

      End Sub

   End Class
End Namespace
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top