| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Stop wasting time looking for files and revisions. Connect your Gmail, DriveDropbox, and Slack accounts and in less than 2 minutes, Dokkio will automatically organize all your file attachments. Learn more and claim your free account.

View
 

Sending-Mail-with-SMTP-Objects

Page history last edited by PBworks 9 years, 8 months ago

 Sending Mail with SMTP Objects

 

 

Prerequisites

 

Visual Studio 2005 or 2008

 

Description of Video

 

A simple video on how to use SMTP objects to send mail. Also shown is some use of XML files.

 

Video 

 

Download Link (right click and save target as ...). 

 

 

Reference Materials

 

C# Code function SendMessage that will send a mail with 3 parameters. It answers true or false based on whether the mail was sent successfully or not.

 

        public static Boolean sendMessage(String toAddress, String subject, String body)

        {
            try
            {

 // This looks comples - but it just retrieves the smtp and other settings from web.config

                Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
                System.Net.Configuration.MailSettingsSectionGroup settings = (System.Net.Configuration.MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");

                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

                message.To.Add(new System.Net.Mail.MailAddress(toAddress));

                message.From = new System.Net.Mail.MailAddress(settings.Smtp.From.ToString());

                message.Subject = subject;

                message.Body = body;

                System.Net.Mail.SmtpClient smtpNet = new System.Net.Mail.SmtpClient();

                smtpNet.Send(message);

                message.Dispose();

                return true;
            }
            catch (Exception e)
            {
                 return false;
            }
        }

 

This code would be placed in the Web.Config - it provides the settings. Be sure to replace with valid addresses and smtp server.

 

<system.net>
  <mailSettings>
   <smtp from="DoNotReply@FromEmailAddress.com">
    <network host="smtpserver.domain.com"  />
   </smtp>
  </mailSettings>
 </system.net>

A simple Visual Basic Version is here - this function simply accepts a To Address

 

 Protected Sub SendMail(ByVal toAddress As String)
        Dim msg As New System.Net.Mail.MailMessage("DoNotReply@domain.com", toAddress)
        msg.Subject = "Email Message from Mail Example"
        msg.Body = "This is a sample email"
        'I have to run a smtp software running on the server
        Dim smtp As New System.Net.Mail.SmtpClient("smtpserver.cecs.ucf.edu")
        smtp.Send(msg)
    End Sub

 

Additional Information

 

A simple article on this   http://www.knowdotnet.com/articles/smtp.html

 

Another good how-to article http://mytechnicalcorner.blogspot.com/2009/04/send-email-and-password-recovery.html

Comments (0)

You don't have permission to comment on this page.