Search Wiki:

Send E-mail

A simple C#/VB.NET code snippet for sending an SMTP-based e-mail message. It supports multiple recipients but does not support attachments.

Sending E-mail

Sending a quick e-mail message using this code snippet is simple:
  1. Download the source code from the Releases tab.
  2. Copy the C# or VB.NET version of the SendMessage method to your project.
  3. Modify the Client.Host property within the SendMessage method to point to your SMTP server (look for the "TODO" in the source code).
  4. Call the SendMessage method passing in the appropriate arguments.

Here are a couple of examples:

C#
  // Send a quick e-mail message
  SendEmail.SendMessage("This is a Test", 
     "This is a test message...",
     "noboday@nowhere.com",
     "somebody@somewhere.com", 
     "ccme@somewhere.com");

VB.NET
  ' Send a quick e-mail message
  SendEmail.SendMessage("This is a Test", _
     "This is a test message...", _
     "noboday@nowhere.com", _
     "somebody@somewhere.com", _
     "ccme@somewhere.com")

Supported .NET Framework Versions
- .NET Framework 2.0
- .NET Framework 3.0
- .NET Framework 3.5
Last edited Feb 13 2008 at 4:01 PM  by jbramwell, version 6
Comments
almostrookie wrote  Nov 8 2008 at 6:04 AM  
The downloaded code sample has the same problem as all code samples I can find on this subject.

'TODO: *** Modify for your SMTP server ***
'Set the SMTP server to be used to send the message
client.Host = "YourMailServer"

I can do this if I'm sending the email, but if I distribute the program to others, I don't know what their mail server is.

How do you create a program that can be distributed and send emails from anybody's (almost) computer?

ChrisRomp wrote  Nov 24 2008 at 12:57 AM  
I would suggest adding a parameter to AppSettings in web.config or app.config file, and using that to set the client.Host parameter.

Chris

kb5ylg wrote  Dec 16 2008 at 10:11 PM  
I agree with ChrisRomp, it would need to be a user setting. Every ISP and corporate user would have different needs regarding smtp server and authentication and outbound port and so on.
-- just David --

oldGoat wrote  Mar 25 2009 at 9:44 AM  
The alternative is to pass that as another parameter?
This is fine for basic email - bug trapping/alerts, but missing a lot for any serious application - html/attachments.

Vozzie wrote  Apr 2 2009 at 12:13 AM  
I was/am dealing with the same "server parameter" issue like almoststrookie.
Creating a mail and saving it to a file and then shellexecute it would be an option to let the user send it with the default mail program, but...

In .Net the System.Web.Mail or the System.Net.Mail don't allow the saving of the email.

So the only option left with trying this path is using the CDO.Message object.

Option Explicit
{{

Dim sFileName
sFileName = Replace(WScript.ScriptFullName, "vbs", "eml")

Dim iMsg
Set iMsg = WScript.CreateObject("CDO.Message")

iMsg.Subject = "The message"
'iMsg.Body = "This is some text"
iMsg.HTMLBody = "<h1 style=""color:green"">Some text...</h1>"

iMsg.BodyPart.Fields("urn:schemas:mailheader:x-unsent") = "TRUE"
iMsg.BodyPart.Fields.Update

iMsg.GetStream.SaveToFile sFileName

Dim oWs
Set oWs = CreateObject("WScript.Shell")
oWs.Run """" & sFileName & """"

}}

sharpey22 wrote  Aug 14 2009 at 6:44 PM  
hi all,
i have been given a task to make a program that you can put x amount of files/attactments in then be able to select one that you want to download it to their email,
does anyone know haow to do this please, you can contact me at robert.sharpe27@googlemail.com
many thanks
Robert

Updating...
Page view tracker