Search code examples
c#emailsystem.net.mailsystem.web.mail

How I can transform my C# code : System.Web.Mail" transform by "System.Net.Mail"


I am using "System.Web.Mail" to send e-mail via SMTP using C# with my following code. It is working but I think It is obsolete

So I want use "System.Net.Mail"

How can I change or transform my following code

using System;
using System.Data;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Mail;
using System.Net;

//(...)





MailMessage mail = new MailMessage();

mail.To = s.EmailFirst;
mail.Cc = s.EmailSecond;
mail.Bcc = ConfigurationManager.AppSettings["mailConfirmeMe"];
mail.Bcc = ConfigurationManager.AppSettings["mailConfirmeTheir"];



mail.From = ConfigurationManager.AppSettings["FromEmail"];
mail.Subject = "OBJET : CONFIRMATION";
mail.BodyFormat = MailFormat.Html;
mail.Body = "<p>this is my test email body</p>";


mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");    //basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "[email protected]"); //set my username here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "##########");   //set my password here

SmtpMail.SmtpServer = "000.000.0.0"; //set my serveur email
SmtpMail.Send( mail );



//(...)

HERE IS MY Answer/SOLUTION ::::::::::::::::::::::::::::::: Hi thanks for your documents,

I try with new following code :

It works, great...

According to you, is that this code is correct?

HERE IS MY Answer/SOLUTION :::::::::::::::::::::::::::::::

using System.Net.Mail;

//.....

protected void OkButton_Click(object sender, System.EventArgs e)
{
    MailAddress from = new MailAddress(ConfigurationManager.AppSettings["FromEmail"]);


    MailAddress to = new MailAddress(s.EmailFirst);

    MailMessage message = new MailMessage(from, to);

    message.Subject = "OBJET : CONFIRMATION";
    message.Body = @"<p>this is my test email body</p>";
    message.BodyEncoding = System.Text.Encoding.UTF8;

    // Add a carbon copy recipient.
    MailAddress copy = new MailAddress(s.EmailSecond);
    message.CC.Add(copy);


    MailAddress bcc = new MailAddress(ConfigurationManager.AppSettings["mailConfirmeMe"]);
    message.Bcc.Add(bcc);

    MailAddress bcc2 = new MailAddress(ConfigurationManager.AppSettings["mailConfirmeTheir"]);
    message.Bcc.Add(bcc2);

    //set my serveur email
    /*
    string server = "000.000.0.0"; //set my serveur email
    SmtpClient client = new SmtpClient(server);

    SmtpClient.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");  //basic authentication
    SmtpClient.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "[email protected]"); //set my username here
    SmtpClient.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "##########"); //set my password here
    */

    SmtpClient client = new SmtpClient();
    client.Host = "000.000.0.0"; //set my serveur email
    client.Credentials = new System.Net.NetworkCredential("[email protected]", "##########"); //set my username here and   my password here



    //client.Credentials = CredentialCache.DefaultNetworkCredentials;
    Console.WriteLine("Sending an e-mail message to {0} by using the SMTP host {1}.",
         to.Address, message.CC.ToString(), message.Bcc.ToString());

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}",
                    ex.ToString());
    }




        //......
}

Solution

  • Take a look at this: SmtpClient.Send

    SmtpClient substitutes SmtpMail and works almost exactly the same.