问题描述
我通过SMTP使用C#和我下面的代码使用 System.Web.Mail 发送电子邮件。这是工作,但我认为这是过时的。
所以我想使用 System.Net.Mail
如何更改或使用系统进行改造,我下面的代码
;
使用System.Data这; System.Collections中使用
;
使用System.ComponentModel;
使用System.Configuration;
使用System.Drawing中;
使用的System.Web;
使用System.Web.SessionState;
使用System.Web.UI程序;使用System.Web.UI.WebControls
;使用System.Web.UI.HtmlControls
;
使用System.Web.Mail;使用System.Net
;
//(...)
邮件MAILMESSAGE新= 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:确认;
mail.BodyFormat = MailFormat.Html;
mail.Body =< P>这是我的测试邮件正文和LT; / P>中;
mail.Fields.Add(http://schemas.microsoft.com/cdo/configuration/smtpauthenticate,1); //基本身份验证
mail.Fields.Add(http://schemas.microsoft.com/cdo/configuration/sendusername,[email protected]); //这里设置了用户名
mail.Fields.Add(http://schemas.microsoft.com/cdo/configuration/sendpassword,##########); //这里设置了密码,
SmtpMail的SmtpServer =000.000.0.0; //设置我serveur电子邮件
SmtpMail.Send(邮件);
//(...)
这里是我的接听/ SOLUTION :::::::::::::::::::::::::::::::
你好感谢你的文件,
我尝试用新的下面的代码:
它的工作原理,伟大的...
根据你的是,这个代码是正确的?
这里是我的答案/解决方案 :::::::::::::::::::::::::::::::
使用System.Net.Mail;
// .....
保护无效OkButton_Click(对象发件人,发送System.EventArgs)
{从=新的MailAddress
MailAddress( ConfigurationManager.AppSettings [FromEmail]);
MailAddress到=新MailAddress(s.EmailFirst);
消息MAILMESSAGE新= MAILMESSAGE(从,到);
message.Subject =OBJET:确认;
message.Body = @< P>这是我的测试邮件正文和LT; / P>中;
message.BodyEncoding = System.Text.Encoding.UTF8;
//添加抄送收件人。
MailAddress副本=新的MailAddress(s.EmailSecond);
message.CC.Add(复印件);
MailAddress BCC =新的MailAddress(ConfigurationManager.AppSettings [mailConfirmeMe]);
message.Bcc.Add(BCC);
MailAddress BCC2 =新的MailAddress(ConfigurationManager.AppSettings [mailConfirmeTheir]);
message.Bcc.Add(BCC2);
//设置我serveur电子邮件
/ *
字符串服务器=000.000.0.0; //设置我serveur电子邮件
SmtpClient客户端=新的SmtpClient(服务器);
SmtpClient.Fields.Add(http://schemas.microsoft.com/cdo/configuration/smtpauthenticate,1); //基本身份验证
SmtpClient.Fields.Add(http://schemas.microsoft.com/cdo/configuration/sendusername,[email protected]); //这里设置了用户名
SmtpClient.Fields.Add(http://schemas.microsoft.com/cdo/configuration/sendpassword,##########); //这里设置了密码,
* /
SmtpClient客户端=新SmtpClient();
client.Host =000.000.0.0; //设置我serveur电子邮件
client.Credentials =新System.Net.NetworkCredential([email protected],##########); //这里设置的用户名和这里我的密码
//client.Credentials = CredentialCache.DefaultNetworkCredentials;
Console.WriteLine(发送电子邮件消息{0}使用SMTP主机{1},
to.Address,message.CC.ToString(),message.Bcc。的ToString());
试
{
client.Send(消息);
}
赶上(异常前)
{
Console.WriteLine(异常陷入CreateBccTestMessage(){0},
ex.ToString()) ;
}
// ......
}
看看这个:
的
SmtpClient代用品的SmtpMail和作品几乎一模一样。
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());
}
//......
}
Take a look at this:SmtpClient.Send
SmtpClient substitutes SmtpMail and works almost exactly the same.
这篇关于我怎样才能改变我的C#代码:System.Web.Mail"通过改造和QUOT; System.Net.Mail"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!