返回首页

如何使用ASP。NET(Web窗体)使用SMTP服务器发送一个E-mail

回答

评论会员:拉克什・米尔 时间:2012/02/06
string from = me@gmail.com; //Replace this with your own correct Gmail Address



string to = you@gmail.com //Replace this with the Email Address to whom you want to send the mail



System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();

 mail.To.Add(to);

 mail.From = new MailAddress(from, "One Ghost" , System.Text.Encoding.UTF8);

mail.Subject = "This is a test mail" ;

mail.SubjectEncoding = System.Text.Encoding.UTF8;

mail.Body = "This is Email Body Text";

mail.BodyEncoding = System.Text.Encoding.UTF8;

mail.IsBodyHtml = true ;

mail.Priority = MailPriority.High;

 

SmtpClient client = new SmtpClient();

//Add the Creddentials- use your own email id and password



 client.Credentials = new System.Net.NetworkCredential(from, "Password");

 

client.Port = 587; // Gmail works on this port

client.Host = "smtp.gmail.com";

client.EnableSsl = true; //Gmail works on Server Secured Layer

       try

        {

            client.Send(mail);

        }

        catch (Exception ex) 

        {

            Exception ex2 = ex;

            string errorMessage = string.Empty; 

            while (ex2 != null)

            {

                errorMessage += ex2.ToString();

                ex2 = ex2.InnerException;

            }

   HttpContext.Current.Response.Write(errorMessage );

        }
评论会员:游客 时间:2012/02/06
P.Salini:谢谢你的提问,{C}codeprelang="cs"MailMessagemsg=spanclass="code-keyword"new/spanMailMessage(); msg.From=spanclass="code-keyword"new/spanMailAddress(from);msg.To.Add(to);msg.Subject=subject;msg.Body=body;msg.IsBodyHtml=spanclass="code-keyword"true/span;msg.Priority=MailPriority.High; SmtpClientsmtpClient=spanclass="code-keyword"new/spanSmtpClient(YourSMTPServer,Port); smtpClient.DeliveryMethod=SmtpDeliveryMethod.Network;smtpClient.Credentials=spanclass="code-keyword"new/spanNetworkCredential(UserId,Password);spanclass="code-comment"///spanspanclass="code-comment"smtpClient.EnableSsl=true;//ifyouuseSSLthentrue/spanspanclass="code-keyword"if/span(msg.To.Count>spanclass="code-digit"0/span){smtpClient.Send(msg);}/pre/code谢谢,马蒙
马蒙・阿卜杜勒・Quader
评论会员:游客 时间:2012/02/06
阅读:imgsrc=]
阿米尔Mahfoozi
评论会员:{A1}] 时间:2012/02/06
P.Salini:试试这个方法
{体C3}
评论会员:乌玛・尚卡尔帕特尔 时间:2012/02/06
{的C4} 试试这个
评论会员:archanakumari 时间:2012/02/06
P.Salini