Results 1 to 2 of 2

Thread: Code to send an email via SSL and Gmail

Hybrid View

  1. #1

    Code to send an email via SSL and Gmail

    This is C# 2010 code to run as a console application (you can try it with MS Visual C# 2010 Express):

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net.Mail;
    
    
    namespace gmailsend
    {
        class Program
        {
            static void Main(string[] args)
            {
                string from = "me@dfgr.com"; //Replace this with your own correct Gmail Address
    
    
                MailAddress to = new MailAddress("fred@xzc.wx", ""); //Replace this with the Email Address to whom you want to send the mail
    
    
                MailMessage mail = new MailMessage();
                mail.To.Add(to);
                mail.From = new MailAddress(from, "Bilbo", System.Text.Encoding.UTF8);
                mail.Subject = "This is a test mail" ;
                mail.SubjectEncoding = System.Text.Encoding.UTF8;
                mail.Body = "This is Email Body Text";
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                mail.IsBodyHtml = false ;
                mail.Priority = System.Net.Mail.MailPriority.High;
    
    
                SmtpClient client = new SmtpClient();
                //Add the Creddentials- use your own email id and password
    
    
                client.Credentials = new System.Net.NetworkCredential(from, "your-gmail-password");
    
    
                client.Port = 587; // Gmail works on this port
                client.Host = "smtp.gmail.com";
                client.EnableSsl = true; //Gmail works on Server Secured Layer
               try
                {
                    client.Send(mail);
                    Console.WriteLine("Message sent.");
                }
                catch (Exception ex) 
                {
                    Exception ex2 = ex;
                    string errorMessage = string.Empty; 
                    while (ex2 != null)
                    {
                        errorMessage += ex2.ToString();
                        ex2 = ex2.InnerException;
                    }
                    Console.WriteLine(errorMessage );
                } // end try 
            }
        }
    }
    To run the compiled code (after you developed it into a useful app.) on a server, you would need to install the dot net 2010 run time distribution code.
    Last edited by Andy101; 11 April, 2013 at 11:15 AM. Reason: more detail added

  2. #2
    Here's some PHP code which I haven't tested yet:

    PHP Code:
    <?php require_once "Mail.php";  $from "Sandra Sender <sender@example.com>"$to "Ramona Recipient <recipient@example.com>"$subject "Hi!"$body "Hi,\n\nHow are you?";  $host "ssl://smtp.gmail.com"$port "587"$username "smtp_username"$password "smtp_password";  $headers = array ('From' => $from,   'To' => $to,   'Subject' => $subject); $smtp Mail::factory('smtp',   array ('host' => $host,     'port' => $port,     'auth' => true,     'username' => $username,     'password' => $password));  $mail $smtp->send($to$headers$body);  if (PEAR::isError($mail)) {   echo("<p>" $mail->getMessage() . "</p>");  } else {   echo("<p>Message successfully sent!</p>");  }
     echo 
    "OK";
    This code requires the PEAR package to be installed with PHP and the Mail module.

    Manual :: Getting and installing the PEAR package manager

    Download and run the PHP script: php go-pear.phar from the command line on your server.

    Install the Mail package with: C:\php5\pear.bat install Mail

    Mail

    If you are lucky, your hosting setup may already have this installed. Or on a VPS you could get it set up.

    A note on using gmail for bulk emailing: there is a 500 email per day limit (for normal users) until your account is temporarily suspended. And you want to avoid bounced emails so that you don't get flagged as a spammer. At least that is my understanding. Ideally, you would encourage your subscribers to send you an email so that the flow is bi-directional.

    For my email automation solution, I think that I will use PHP rather than C# since it seems easier to integrate with web page subscribe forms. For the timing of emails, I am thinking to send 1 per minute up to 500 max per day. So that fits in with a normal working day where a computer may be switched on acting as a part-time mail server.

    Result of testing: I couldn't get the PHP script to work with Gmail and Pear. So tried http://phpmailer.worxware.com which I also couldn't make work with Gmail. However, phpmailer worked well for me connecting to an SMTP server on my shared hosting account, and it is simple to install.

    In conclusion: I would use a DOT NET script for SSL connections and phpmailer for non-SSL in a PHP environment.
    Last edited by Andy101; 20 April, 2013 at 08:11 AM. Reason: Progress update

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •