利用smtp发送邮件都要遇到验证问题,在.Net2.0下,直接使用smtpclient 类的Credentials 属性就可以。
但在.Net1.1下MailMessage.Fields.Add方法.具体代码如下(只提供函数)
命名空间:using System.Web.Mail
- public void sendMail(string from, string to, string subject,string body,string username,string userpwd)
- {
- try
- {
- System.Web.Mail.MailMessage myMail=new MailMessage();
- myMail.From =from;
- myMail.To = to;
- myMail.Subject =subject;
- myMail.Priority = MailPriority.Low;
- myMail.BodyFormat = MailFormat.Text;
- myMail.Body =body;
- myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
- myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", username); //set your username here
- myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", userpwd); //set your password here
- SmtpMail.SmtpServer="smtp.163.com";//your smtp server here
- SmtpMail.Send(myMail);
- }
- catch(Exception e)
- {
- throw e;
- }
- }