Sending email from within app

C

cj

There are several ways. Also depends on if your using 2003 or 2005.
I'll pull some of my code and get back to you.
 
C

cj

Here's a simple way

Public Sub StartMail(ByVal [To] As String, Optional ByVal subj As String
= "", Optional ByVal Body As String = "")
Try
Dim mailProcess As New ProcessStartInfo
mailProcess.UseShellExecute = True
mailProcess.FileName = "mailto:" & [To] & "?subject=" &
subj & "&body=" & Body
Process.Start(mailProcess)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Here's my favorite way but it's 2005 only

Dim msg As New System.Net.Mail.MailMessage
Dim smtp As New System.Net.Mail.SmtpClient
msg.From = New System.Net.Mail.MailAddress("(e-mail address removed)")
msg.To.Add("(e-mail address removed)")
msg.Subject = "Test Msg"
msg.Body = "Can you hear me now?"
smtp.Host = "smtp.myco.com"
smtp.Port = 25
smtp.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
smtp.Credentials = New
System.Net.NetworkCredential("(e-mail address removed)", "mypass")
smtp.Send(msg)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

Got one more I'll find later.
 
C

cj

Here's the other. I haven't used it in 2005 yet but in 2003 I did. I
used it to give a user of an app the option to send the contents of say
a text box to someone. It brings up their default mail and puts the
contents and subject in but leaves who they're sending it to blank and
allows them to edit the message etc and send it themselves.

My 2005 example in the previous email is used by some of my programs to
wake us up in the middle of the night with an email to our cell phones
because something happened it didn't like. :( In other words it is a
totally automatic generate and send of a message.

I don't quite remember how the sendto: works any more--it didn't do what
I wanted.

There are other ways to do this but these are all the examples I have.


Private Sub SndMAPIMail(ByVal subj As String, ByVal msg As String)
Try
Dim myMAPISession As New MSMAPI.MAPISession
Dim myMAPIMessage As New MSMAPI.MAPIMessages

myMAPISession.DownLoadMail = False
myMAPISession.SignOn()
myMAPISession.NewSession = True

myMAPIMessage.SessionID = myMAPISession.SessionID

myMAPIMessage.Compose()

myMAPIMessage.MsgSubject = subj
myMAPIMessage.MsgNoteText = msg

Try
myMAPIMessage.Send(True)
Catch ex As Exception
If Not ex.Message = "User cancelled process" Then
MessageBox.Show(ex.Message)
End If
End Try

myMAPISession.SignOff()
myMAPISession.NewSession = False
Catch
End Try
End Sub

Here's a simple way

Public Sub StartMail(ByVal [To] As String, Optional ByVal subj As String
= "", Optional ByVal Body As String = "")
Try
Dim mailProcess As New ProcessStartInfo
mailProcess.UseShellExecute = True
mailProcess.FileName = "mailto:" & [To] & "?subject=" & subj
& "&body=" & Body
Process.Start(mailProcess)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Here's my favorite way but it's 2005 only

Dim msg As New System.Net.Mail.MailMessage
Dim smtp As New System.Net.Mail.SmtpClient
msg.From = New System.Net.Mail.MailAddress("(e-mail address removed)")
msg.To.Add("(e-mail address removed)")
msg.Subject = "Test Msg"
msg.Body = "Can you hear me now?"
smtp.Host = "smtp.myco.com"
smtp.Port = 25
smtp.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
smtp.Credentials = New
System.Net.NetworkCredential("(e-mail address removed)", "mypass")
smtp.Send(msg)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

Got one more I'll find later.

Hi

How does one send email from within a vb.net app?

Thanks

Regards
 
C

cj

????

I use Thunderbird and most folks use Outlook here and it works fine for
us as I have it in the example. Fine being the way I want it too that
is. Seems I do recall something that was different in the way Outlook
and Thunderbird responded to it but can't remember what.
 
G

Guest

cj said:
I use Thunderbird and most folks use Outlook here and it works fine for
us as I have it in the example. Fine being the way I want it too that
is. Seems I do recall something that was different in the way Outlook
and Thunderbird responded to it but can't remember what.

MAPI is Messaging API... if you have an antivirus it often blocks MAPI
commands. MAPI can only relied on in an enterprise environment in which you
have complete control of the computers.
 
J

John

Many thanks cj for wonderful examples. How do attachments work?

Many Thanks

Regards

cj said:
Here's the other. I haven't used it in 2005 yet but in 2003 I did. I
used it to give a user of an app the option to send the contents of say a
text box to someone. It brings up their default mail and puts the
contents and subject in but leaves who they're sending it to blank and
allows them to edit the message etc and send it themselves.

My 2005 example in the previous email is used by some of my programs to
wake us up in the middle of the night with an email to our cell phones
because something happened it didn't like. :( In other words it is a
totally automatic generate and send of a message.

I don't quite remember how the sendto: works any more--it didn't do what I
wanted.

There are other ways to do this but these are all the examples I have.


Private Sub SndMAPIMail(ByVal subj As String, ByVal msg As String)
Try
Dim myMAPISession As New MSMAPI.MAPISession
Dim myMAPIMessage As New MSMAPI.MAPIMessages

myMAPISession.DownLoadMail = False
myMAPISession.SignOn()
myMAPISession.NewSession = True

myMAPIMessage.SessionID = myMAPISession.SessionID

myMAPIMessage.Compose()

myMAPIMessage.MsgSubject = subj
myMAPIMessage.MsgNoteText = msg

Try
myMAPIMessage.Send(True)
Catch ex As Exception
If Not ex.Message = "User cancelled process" Then
MessageBox.Show(ex.Message)
End If
End Try

myMAPISession.SignOff()
myMAPISession.NewSession = False
Catch
End Try
End Sub

Here's a simple way

Public Sub StartMail(ByVal [To] As String, Optional ByVal subj As String
= "", Optional ByVal Body As String = "")
Try
Dim mailProcess As New ProcessStartInfo
mailProcess.UseShellExecute = True
mailProcess.FileName = "mailto:" & [To] & "?subject=" & subj
& "&body=" & Body
Process.Start(mailProcess)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Here's my favorite way but it's 2005 only

Dim msg As New System.Net.Mail.MailMessage
Dim smtp As New System.Net.Mail.SmtpClient
msg.From = New System.Net.Mail.MailAddress("(e-mail address removed)")
msg.To.Add("(e-mail address removed)")
msg.Subject = "Test Msg"
msg.Body = "Can you hear me now?"
smtp.Host = "smtp.myco.com"
smtp.Port = 25
smtp.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
smtp.Credentials = New
System.Net.NetworkCredential("(e-mail address removed)", "mypass")
smtp.Send(msg)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

Got one more I'll find later.

Hi

How does one send email from within a vb.net app?

Thanks

Regards
 
J

Jeff

Spam Catcher said:
MAPI is Messaging API... if you have an antivirus it often blocks MAPI
commands. MAPI can only relied on in an enterprise environment in which
you
have complete control of the computers.


Antivirus, at least the McAfee on my server, will also block messages that
aren't in MAPI.

I just went through that yesterday. McAfee was set to permit sending by the
popular email programs, and I had to add two extra exceptions for the .net
application - one when running the app directly in the development
environment, and another when running over the web. After poking around in
McAfee, I found a log stating what it was blocking, and then I added it to
the list of exceptions under McAfee's setting dealing with the block of
"mass email worms on port 25"

Jeff
 
C

cj

I haven't done much with them. But, I know it can be done. If I was
you I'd cut and paste my examples into a test prg and when they work try
adding attachments in a similar manner to recipients or the subject and
body text. I have a program that I use for testing little bits of code
and ideas. It's a form with 36 buttons currently and a couple of
textboxes and labels. Each button was added to test a bit of code.
When I wrote the apps that send email I made a button in the test
program and played with the code there until I got the button to send a
message. That is where the code I sent you came from. It'll be Monday
before I can look into attachments--and that's if I'm not swamped with
problems when I get in the office. Good Luck.
Many thanks cj for wonderful examples. How do attachments work?

Many Thanks

Regards

cj said:
Here's the other. I haven't used it in 2005 yet but in 2003 I did. I
used it to give a user of an app the option to send the contents of say a
text box to someone. It brings up their default mail and puts the
contents and subject in but leaves who they're sending it to blank and
allows them to edit the message etc and send it themselves.

My 2005 example in the previous email is used by some of my programs to
wake us up in the middle of the night with an email to our cell phones
because something happened it didn't like. :( In other words it is a
totally automatic generate and send of a message.

I don't quite remember how the sendto: works any more--it didn't do what I
wanted.

There are other ways to do this but these are all the examples I have.


Private Sub SndMAPIMail(ByVal subj As String, ByVal msg As String)
Try
Dim myMAPISession As New MSMAPI.MAPISession
Dim myMAPIMessage As New MSMAPI.MAPIMessages

myMAPISession.DownLoadMail = False
myMAPISession.SignOn()
myMAPISession.NewSession = True

myMAPIMessage.SessionID = myMAPISession.SessionID

myMAPIMessage.Compose()

myMAPIMessage.MsgSubject = subj
myMAPIMessage.MsgNoteText = msg

Try
myMAPIMessage.Send(True)
Catch ex As Exception
If Not ex.Message = "User cancelled process" Then
MessageBox.Show(ex.Message)
End If
End Try

myMAPISession.SignOff()
myMAPISession.NewSession = False
Catch
End Try
End Sub

Here's a simple way

Public Sub StartMail(ByVal [To] As String, Optional ByVal subj As String
= "", Optional ByVal Body As String = "")
Try
Dim mailProcess As New ProcessStartInfo
mailProcess.UseShellExecute = True
mailProcess.FileName = "mailto:" & [To] & "?subject=" & subj
& "&body=" & Body
Process.Start(mailProcess)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Here's my favorite way but it's 2005 only

Dim msg As New System.Net.Mail.MailMessage
Dim smtp As New System.Net.Mail.SmtpClient
msg.From = New System.Net.Mail.MailAddress("(e-mail address removed)")
msg.To.Add("(e-mail address removed)")
msg.Subject = "Test Msg"
msg.Body = "Can you hear me now?"
smtp.Host = "smtp.myco.com"
smtp.Port = 25
smtp.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
smtp.Credentials = New
System.Net.NetworkCredential("(e-mail address removed)", "mypass")
smtp.Send(msg)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

Got one more I'll find later.


John wrote:
Hi

How does one send email from within a vb.net app?

Thanks

Regards
 
G

Guest

Antivirus, at least the McAfee on my server, will also block messages
that aren't in MAPI.

Isn't it better to run the "mailto:[email protected]" command instead? That way
the OS will automatically launch the default mail program without the need
to worry about the AV?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top