Difference between revisions of "Sending emails with attachments"

From HashVB
Jump to: navigation, search
 
m (Tided and Wikiised)
Line 1: Line 1:
 
{{VB.NET}}
 
{{VB.NET}}
  
 +
First a function to emulate ShellExecute:
  
Private Function SendEmail(ByVal MailtoSyntax As String) As Boolean
+
Private Function SendEmail(ByVal MailtoSyntax As String) As Boolean
        Dim myProcess As New Process
+
  Dim myProcess As New Process
        myProcess.StartInfo.FileName = MailtoSyntax
+
  myProcess.StartInfo.FileName = MailtoSyntax
        myProcess.StartInfo.UseShellExecute = True
+
  myProcess.StartInfo.UseShellExecute = True
        myProcess.StartInfo.RedirectStandardOutput = False
+
  myProcess.StartInfo.RedirectStandardOutput = False
        myProcess.Start()
+
  myProcess.Start()
        myProcess.Dispose()
+
  myProcess.Dispose()
End Function
+
End Function
  
---Example how to send an email with attachment using default email client:
+
The mailto: syntax is handled by windows to use the default mail client. You can use the following code to build the email string and "execute" it:
  
---just build the mailto syntax
+
Dim mailto As New System.Text.StringBuilder
 +
mailto.Append("<nowiki>mailto:teste@gmail.com</nowiki>")
 +
mailto.Append("&cc=testcc@testcc.com,testcc1@testcc.com")
 +
mailto.Append("&bcc=testcc@testbcc.com,testcc1@testbcc.com")
 +
mailto.Append("&subject=test subject")
 +
mailto.Append("&body=Look to attachment")
 +
mailto.Append("&Attach=" & Strings.Chr(34) & Strings.Chr(34) & "c:\teste.html" & Strings.Chr(34) & Strings.Chr(34))
 +
SendEmail(mailto.ToString)
  
Dim mailto As New System.Text.StringBuilder
+
Please note that the file must exist and that some file extensions (.zip, .rar, and .exe) are blocked by default (e.g. Outlook 2003). You must change Email client security to allows specific extensions.
 
+
mailto.Append("mailto:teste@gmail.com")
+
 
+
mailto.Append("&cc=testcc@testcc.com,testcc1@testcc.com")
+
 
+
mailto.Append("&bcc=testcc@testbcc.com,testcc1@testbcc.com")
+
 
+
mailto.Append("&subject=test subject")
+
 
+
mailto.Append("&body=Look to attachment")
+
 
+
mailto.Append("&Attach=" & Strings.Chr(34) & Strings.Chr(34) & "c:\teste.html" & Strings.Chr(34) & Strings.Chr(34))
+
 
+
SendEmail(mailto.ToString)
+
 
+
'Note:
+
- File attached must exists
+
- Some file extensions ( .zip .rar .exe) are not allowed by default (e.g. Outlook 2003)
+
  You must change Email client security (e.g Outlook) to support specific extension
+

Revision as of 12:47, 10 January 2006

float
 This article is based on Visual Basic.NET. Find other Visual Basic.NET articles.

First a function to emulate ShellExecute:

Private Function SendEmail(ByVal MailtoSyntax As String) As Boolean
  Dim myProcess As New Process
  myProcess.StartInfo.FileName = MailtoSyntax
  myProcess.StartInfo.UseShellExecute = True
  myProcess.StartInfo.RedirectStandardOutput = False
  myProcess.Start()
  myProcess.Dispose()
End Function

The mailto: syntax is handled by windows to use the default mail client. You can use the following code to build the email string and "execute" it:

Dim mailto As New System.Text.StringBuilder
mailto.Append("mailto:teste@gmail.com")
mailto.Append("&cc=testcc@testcc.com,testcc1@testcc.com")
mailto.Append("&bcc=testcc@testbcc.com,testcc1@testbcc.com")
mailto.Append("&subject=test subject")
mailto.Append("&body=Look to attachment")
mailto.Append("&Attach=" & Strings.Chr(34) & Strings.Chr(34) & "c:\teste.html" & Strings.Chr(34) & Strings.Chr(34))
SendEmail(mailto.ToString)

Please note that the file must exist and that some file extensions (.zip, .rar, and .exe) are blocked by default (e.g. Outlook 2003). You must change Email client security to allows specific extensions.