如何使用ASP .net(Web表单)发送电子邮件 [英] How to send an E-mail using ASP .net (Web Forms)

查看:98
本文介绍了如何使用ASP .net(Web表单)发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用smtp服务器使用ASP .net(Web窗体)发送电子邮件

How to send an E-mail using ASP .net (Web Forms) using smtp server

推荐答案

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 );
        }


谢谢您的提问,

Thank you for your question,

using System.Net;
using System.Net.Mail;





MailMessage msg = new MailMessage();

msg.From = new MailAddress(from);
msg.To.Add(to);
msg.Subject = subject;
msg.Body = body;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.High;

SmtpClient smtpClient = new SmtpClient(YourSMTPServer, Port );

smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;smtpClient.Credentials = new NetworkCredential(UserId, Password);
        
//smtpClient.EnableSsl = true;  //if you use SSL then true

if (msg.To.Count > 0)
{
   smtpClient.Send(msg);                    
}




谢谢,
马蒙




Thanks,
Mamun


阅读:

http://forums.asp.net/t/1360420.aspx#_What_classes_are [ ^ ]
read this :

http://forums.asp.net/t/1360420.aspx#_What_classes_are[^]


这篇关于如何使用ASP .net(Web表单)发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆