设置的SMTP有时并不容易。 去年我是发展中国家照片社会网络(如的Flickr),我们需要通过电子邮件注册确认制度。 的主机服务上,我们进行的测试,所以我真的吸倾销他们的SMTP和安装我们的域名谷歌应用服务的SMTP设置。 Gmail提供方便的SMTP发送电子邮件访问,如果你已经配置在谷歌您的域的电子邮件应用程序的话,我认为它的最佳使用任何电子邮件方案。

在本教程中,我会告诉你如何使用你可以很容易地在ASP.NET发送邮件(丙的GMail#)。 我用我的ProgrammerFish 谷歌企业应用套件电子邮件帐户,但您也可以使用任何Gmail帐户。 最后的网页看起来像这样:

Simplest way to send email using GMail in ASP.Net (C#)

所以这里是如何做到这一点:

转到文件“>新建”>网站,并命名此项目GmailMailSender(或任何你喜欢的),然后按确定

Simplest way to send email using GMail in ASP.Net (C#)

现在,让我们创造了一些网页的界面。 解决方案资源管理器 ,双击Default.aspx的。并单击设计按钮。

创建一个这样的接口。 4插入文本框命名为tb_GmailAccount,tb_GmailPassword(同'的TextMode'作为“密码属性” ),tb_RecieverEmailtb_Message(同'的TextMode'作为“多行”)的财产。 还插入为lb_error命名(与'文本'属性为“发送邮件错误”和'为“false”)bt_sendMaillb_MailSent可见的财产(以'文本'属性为“邮件发送与'”可见'财产2标签“假“)

Simplest way to send email using GMail in ASP.Net (C#)

现在解决方案资源管理器,在Web项目节点,并添加新项点击右键

Simplest way to send email using GMail in ASP.Net (C#)

Visual Studio 安装的模板选择 名称GmailSender.cs并点击添加 如果出现一个对话框,然后单击

Simplest way to send email using GMail in ASP.Net (C#)

添加该类下面的代码:

 System.Collections.Generic; using System.Linq; using System.Web; using System.Net; using System.Net.Mail; /// <summary> /// Summary description for GmailSender /// </summary> public class GmailSender { public GmailSender() { // // TODO: Add constructor logic here // } public static bool SendMail( string gMailAccount, string password, string to, string subject, string message) { try { NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password); MailMessage msg = new MailMessage(); msg.From = new MailAddress(gMailAccount); msg.To.Add( new MailAddress(to)); msg.Subject = subject; msg.Body = message; msg.IsBodyHtml = true ; SmtpClient client = new SmtpClient( "smtp.gmail.com" ); client.EnableSsl = true ; client.UseDefaultCredentials = false ; client.Credentials = loginInfo; client.Send(msg); return true ; } catch (Exception) { return false ; } } } 使用系统; 使用 System.Collections.Generic程序, 使用 System.Linq, 使用 System.Web程序, 使用 System.Net; 使用 System.Net.Mail程序/ / / <summary> / / /使用GmailSender摘要说明/ / /“/总结“ 公共类 GmailSender( 公众 GmailSender()(/ / / / TODO:在此处添加构造函数逻辑/ /) 中public static bool Sendmail的( 字符串 gMailAccount, 密码, 字符串字符串的问题, 字符串消息)( 尝试 (NetworkCredential loginInfo =  NetworkCredential(gMailAccount,密码); MailMessage味精=  MailMessage(); msg.From = 信箱订阅(gMailAccount); msg.To.Add( 信箱订阅(至)); msg.Subject =主题; msg.Body =信息; msg.IsBodyHtml = 真实 ; SmtpClient客户端=  SmtpClient(“smtp.gmail.com”); client.EnableSsl = 真实 ; client.UseDefaultCredentials =  ; client.Credentials = loginInfo; client.Send(味精); 返回true;捕获 (异常)( 返回false;))) 

现在打开双击Default.aspx的发送邮件按钮来打开bt_sendMail_click事件函数的代码。

Simplest way to send email using GMail in ASP.Net (C#) 添加这一类的代码:

  sender, EventArgs e) 保护无效 bt_sendMail_Click( 对象发送,EventArgs五)
    
         如果 (GmailSender.SendMail(tb_GmailAccount.Text,tb_GmailPassword.Text,tb_RecieverEmail.Text,tb_Subject.Text,tb_Message.Text))
        
             lb_MailSent.Visible =  ;

        
         其他的
        
             lb_error.Visible =  ;
        
     

F5运行,如果未启用调试对话框单击确定

由于ProgrammerFish。com正在使用的电子邮件谷歌应用服务,我将用我ProgrammerFish的电子邮件地址发送电子邮件。

Simplest way to send email using GMail in ASP.Net (C#) 如果邮件发送成功,它会显示电子邮件发送的标签,其他明智的,能够显示发送邮件错误的标签,可以检查它,如果它发出与否,在这里它是!

Simplest way to send email using GMail in ASP.Net (C#)

下载解决方案文件:GMailMailSender.zip

像这样的文章? 订阅我们更有趣和新颖的技术更新的资讯点击这里获得通过电子邮件更新 您也可以加入我们的Facebook的扇页