请教各位:
在.net下,c#,需要一个发送邮件的方法,如何编写?用户在域内,使用Exchange发送邮件,都在局域网内部,而且他用Outlook发送邮件不需要设置用户名以及密码。另外,我也实验了一些程序,可是却发送不出去!请问如何解决?谢谢!下面是我使用的方法:
/// <summary>
/// Send the mails accord to the parameters gived by the Array
/// index 0: SmtpServer
/// index 1: MailFrom
/// index 2: MailTo
/// index 3: MailSubject
/// index 4: MailBody
/// </summary>
private void sendMailByCDO1(string[] MailParameters)
{
// System.Web.Mail.MailMessage myMail=new MailMessage();
// myMail.From = MailParameters[1];
// myMail.To = MailParameters[2];
// myMail.Subject = MailParameters[3];
// myMail.Priority = MailPriority.High;
// myMail.BodyFormat = MailFormat.Text;
// myMail.Body = MailParameters[4];
// SmtpMail.SmtpServer = MailParameters[0]; //smtp server here
// myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
// myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", MailParameters[5]); //set your username here
// myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", MailParameters[6]); //set your password here
//
//
//// SmtpMail.SmtpServer.
// SmtpMail.Send(myMail); //send one email CDO.Message oMsg = new CDO.Message();
oMsg.From = MailParameters[1];
oMsg.To = MailParameters[2];
oMsg.Subject = MailParameters[3];
oMsg.HTMLBody = "<html><body>" + MailParameters[4] + " CDO 1" + "</body></html>";
CDO.IConfiguration iConfg = oMsg.Configuration;
ADODB.Fields oFields = iConfg.Fields;
oFields[" http://schemas.microsoft.com/cdo/configuration/sendusing"].Value=2 ;
oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress "]
.Value = MailParameters[1]; //sender mail
oFields[" http://schemas.microsoft.com/cdo/configuration/smtpaccountname "]
.Value = MailParameters[5]; //email account
oFields["http://schemas.microsoft.com/cdo/configuration/sendusername "]
.Value = MailParameters[5];
oFields[" http://schemas.microsoft.com/cdo/configuration/sendpassword "]
.Value=MailParameters[6];
oFields[" http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"]
.Value=1;
//分别实验了0,1,2,都不行
//value=0 代表Anonymous验证方式(不需要验证)
//value=1 代表Basic验证方式(使用basic (clear-text) authentication.
//The configuration sendusername/sendpassword or postusername/postpassword fields are used to specify credentials.)
//Value=2 代表NTLM验证方式(Secure Password Authentication in Microsoft Outlook Express)
oFields[" http://schemas.microsoft.com/cdo/configuration/languagecode"].Value=0x0804 ;
oFields[" http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = MailParameters[0]; oFields.Update();
oMsg.BodyPart.Charset = "gb2312";
oMsg.HTMLBodyPart.Charset= "gb2312"; oMsg.Send();
oMsg = null;
}/// <summary>
/// Send the mails accord to the parameters gived by the Array
/// index 0: SmtpServer
/// index 1: MailFrom
/// index 2: MailTo
/// index 3: MailSubject
/// index 4: MailBody
/// </summary>
private void sendMailBySmtp2(string[] MailParameters)
{
System.Web.Mail.MailMessage myMail=new MailMessage();
myMail.From = MailParameters[1];
myMail.To = MailParameters[2];
myMail.Subject = MailParameters[3];
myMail.Priority = MailPriority.High;
myMail.BodyFormat = MailFormat.Text;
myMail.Body = MailParameters[4] + " SMTP1";
SmtpMail.SmtpServer = MailParameters[0]; //smtp server here
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "2"); //basic authentication,实验了0,1,2,都不行
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", MailParameters[5]); //set your username here
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", MailParameters[6]); //set your password here
// SmtpMail.SmtpServer.
SmtpMail.Send(myMail); //send one email
}
在.net下,c#,需要一个发送邮件的方法,如何编写?用户在域内,使用Exchange发送邮件,都在局域网内部,而且他用Outlook发送邮件不需要设置用户名以及密码。另外,我也实验了一些程序,可是却发送不出去!请问如何解决?谢谢!下面是我使用的方法:
/// <summary>
/// Send the mails accord to the parameters gived by the Array
/// index 0: SmtpServer
/// index 1: MailFrom
/// index 2: MailTo
/// index 3: MailSubject
/// index 4: MailBody
/// </summary>
private void sendMailByCDO1(string[] MailParameters)
{
// System.Web.Mail.MailMessage myMail=new MailMessage();
// myMail.From = MailParameters[1];
// myMail.To = MailParameters[2];
// myMail.Subject = MailParameters[3];
// myMail.Priority = MailPriority.High;
// myMail.BodyFormat = MailFormat.Text;
// myMail.Body = MailParameters[4];
// SmtpMail.SmtpServer = MailParameters[0]; //smtp server here
// myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
// myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", MailParameters[5]); //set your username here
// myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", MailParameters[6]); //set your password here
//
//
//// SmtpMail.SmtpServer.
// SmtpMail.Send(myMail); //send one email CDO.Message oMsg = new CDO.Message();
oMsg.From = MailParameters[1];
oMsg.To = MailParameters[2];
oMsg.Subject = MailParameters[3];
oMsg.HTMLBody = "<html><body>" + MailParameters[4] + " CDO 1" + "</body></html>";
CDO.IConfiguration iConfg = oMsg.Configuration;
ADODB.Fields oFields = iConfg.Fields;
oFields[" http://schemas.microsoft.com/cdo/configuration/sendusing"].Value=2 ;
oFields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress "]
.Value = MailParameters[1]; //sender mail
oFields[" http://schemas.microsoft.com/cdo/configuration/smtpaccountname "]
.Value = MailParameters[5]; //email account
oFields["http://schemas.microsoft.com/cdo/configuration/sendusername "]
.Value = MailParameters[5];
oFields[" http://schemas.microsoft.com/cdo/configuration/sendpassword "]
.Value=MailParameters[6];
oFields[" http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"]
.Value=1;
//分别实验了0,1,2,都不行
//value=0 代表Anonymous验证方式(不需要验证)
//value=1 代表Basic验证方式(使用basic (clear-text) authentication.
//The configuration sendusername/sendpassword or postusername/postpassword fields are used to specify credentials.)
//Value=2 代表NTLM验证方式(Secure Password Authentication in Microsoft Outlook Express)
oFields[" http://schemas.microsoft.com/cdo/configuration/languagecode"].Value=0x0804 ;
oFields[" http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = MailParameters[0]; oFields.Update();
oMsg.BodyPart.Charset = "gb2312";
oMsg.HTMLBodyPart.Charset= "gb2312"; oMsg.Send();
oMsg = null;
}/// <summary>
/// Send the mails accord to the parameters gived by the Array
/// index 0: SmtpServer
/// index 1: MailFrom
/// index 2: MailTo
/// index 3: MailSubject
/// index 4: MailBody
/// </summary>
private void sendMailBySmtp2(string[] MailParameters)
{
System.Web.Mail.MailMessage myMail=new MailMessage();
myMail.From = MailParameters[1];
myMail.To = MailParameters[2];
myMail.Subject = MailParameters[3];
myMail.Priority = MailPriority.High;
myMail.BodyFormat = MailFormat.Text;
myMail.Body = MailParameters[4] + " SMTP1";
SmtpMail.SmtpServer = MailParameters[0]; //smtp server here
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "2"); //basic authentication,实验了0,1,2,都不行
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", MailParameters[5]); //set your username here
myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", MailParameters[6]); //set your password here
// SmtpMail.SmtpServer.
SmtpMail.Send(myMail); //send one email
}
objPOrder = (POrder)arrlsPOrders[iCount];
strMailBody = "客户: Wraxall" + "\t\n" + " 订单号: " + objPOrder.POID + "\t\n" + " 预计发货日期: " + objPOrder.RequireDate + "\t\n" + " 请尽快到Wraxall网站确认订单,谢谢!";
arrMailParameter[0] = strSmtpServer;
arrMailParameter[1] = strMailFrom;
arrMailParameter[3] = strMailSubject;
arrMailParameter[4] = strMailBody;
arrMailParameter[5] = strMailUserName;
arrMailParameter[6] = strMailUserPassword;
arrlsManufactures = objPOrderBLL.GetManufacturesByPOrder(objPOrder); //get Manufactures of company by POrder
for (int i = 0; i < arrlsManufactures.Count; i++)
{
strMailTo = ((Company)arrlsManufactures[i]).Email;
arrMailParameter[2] = strMailTo;
sendMailByCDO1(arrMailParameter); //send email to every Company
sendMailByCDO0(arrMailParameter); //send email to every Company
sendMailByCDO2(arrMailParameter); //send email to every Company
sendMailBySmtp0(arrMailParameter);
sendMailBySmtp1(arrMailParameter);
sendMailBySmtp2(arrMailParameter);
}
或者安装一下exchange sdk。