我如何转换我的C#代码:System.Web.Mail"转换为" System.Net.Mail"

时间:2014-05-08 14:06:56

标签: c# email system.net.mail system.web.mail

我正在使用" System.Web.Mail "使用C#通过以下代码通过SMTP发送电子邮件。它正在运作,但我认为它已经过时了

所以我想使用" System.Net.Mail "

如何更改或转换以下代码

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", "myUserName@domain.com"); //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 );



//(...)

这是我的答案/解决方案 :::::::::::::::::::::::::::::::::: 嗨,谢谢你的文件,

我尝试使用以下新代码:

很有效,很棒......

根据你的说法,这段代码是正确的吗?

这是我的答案/解决方案 :::::::::::::::::::::::::::::::::

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", "myUserName@domain.com"); //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("myUserName@domain.com", "##########"); //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());
    }




        //......
}

1 个答案:

答案 0 :(得分:0)

看看这个: SmtpClient.Send

SmtpClient替代SmtpMail并且几乎完全相同。