Servergrid UserLink

... connection ... education ... discovery ... information ...
Welcome to Servergrid UserLink Sign in | Join | Help
Home Blogs Forums Photos Downloads Servergrid Join Now Private Messages

Re: Mailing Lists

  •  11-24-2006, 3:38 PM

    Re: Mailing Lists

    Below is some ASP.NET C# code you can use to control your mailing lists.




    using System.Net.Mail;

    /// <summary>
    /// Sends mail to specified list
    /// </summary>
    /// <param name="emailAddress"></param>
    private bool SendToServergridLists(string mailToAddress, string mailFromAddress)
    {
        // Sending to the lists is easy.
        // If you are having people subscribe/unsubscribe via
        // web forms, make sure that you have the
        // subscribe witout confirmation and unsubscribe without confirmation
        // boxes checked in the properties of your mailing list
        // in H-SPHERE. Also, you will probably want to enable sending
        // from the mailing list address.
        //
        // this example can be used in any case, to subscribe, unsubscribe,
        // or send to the list



        // set mail server
        SmtpClient cli = new SmtpClient("localhost");        // define the mail server
                                                            // if you are on Servergrid
                                                            // use localhost

        MailMessage myMessage= new MailMessage();            // Create a new Mail Message object
        myMessage.IsBodyHtml = false;                        // This isn't necessary

        try
        {
            // set the message properties

            // the TO will change depending on your intention
            // to subscribe, you want your TO address to be myList-subscribe@whateveryourdomainis.xx
            // your FROM should be the address you want added to the list
            // to unsubscribe, you want your TO address to be myList-unsubscribe@whateveryourdomainis.xx
            // your FROM should be the address you want removed from the list
            // to send an email to the people on the list you want your TO and FROM to be myList@whateveryourdomainis.xx

            myMessage.To.Add(new MailAddress(mailToAddress));    
            myMessage.From = new MailAddress(mailFromAddress);            
            myMessage.Body = string.Format("This is the message body");
            myMessage.Subject = "This is the message subject";

            // These next lines are essential to sending a message TO your list from a page

            // add a header describing the sending agent
            myMessage.Headers.Add("User-Agent", "ASP.NET");                                

            // define the sender, this is different than "From"
            myMessage.Sender = new MailAddress(mailFromAddress);

            // set the encoding for the message
            myMessage.BodyEncoding = System.Text.Encoding.ASCII;
            myMessage.SubjectEncoding = System.Text.Encoding.ASCII;

            cli.Send(myMessage);
        }
        catch (SmtpException stmpExp)
        {
            throw new Exception("Mail send failed due to smtp error: " + stmpExp.Message);
        }
        catch (Exception exp)
        {
            throw new Exception("Send failed : " + exp.Message);
        }

        myMessage.Dispose();

        return true;
    }

    Michael R. Czarnecki
    Senior Web Developer
    http://www.servergrid.com
    Get a quote on web development
    consulting@servergrid.com
View Complete Thread
Powered by Community Server, by Telligent Systems