通过gmail发送Asp.Net电子邮件 [英] Sending Asp.Net email through gmail

查看:238
本文介绍了通过gmail发送Asp.Net电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过使用下面的代码和配置从ASP.Net通过GMail发送电子邮件。不幸的是,它似乎没有工作,也没有发出错误消息。服务器日志或邮件IIS邮件文件夹中没有任何内容,我甚至检查了地址的垃圾邮件,看看邮件是否在那里结束。任何帮助将非常感激。

I am trying to send an email via GMail from ASP.Net using the code and config below. Unfortunatly it doesn't seem to be working and it also isn't throwing an error message. There is nothing in the server logs or the mail IIS mail folders, I even checked the trash of the from address to see if the mail ended up there. Any help would be really appreciated.

C#部分

    public void SendFeedback()
    {
        string emailFrom = this.Email.Text;

        MailMessage message = new MailMessage();
        // here is an important part:
        message.From = new MailAddress(emailFrom, "Mailer");
        // it's superfluous part here since from address is defined in .config file
        // in my example. But since you don't use .config file, you will need it.
        message.Subject = "www.gumpshen.com - Website Query";
        message.IsBodyHtml = true;
        message.Body = string.Format(" Name = {0}, Phone = {1}", Name.Text, Phone.Text);
        message.Body += Environment.NewLine;
        message.Body += Environment.NewLine;
        message.Body += ProjectDetails.Text; ;

        var client = new SmtpClient();

        try
        {
            client.Send(message);

这是配置部分:

<system.net>
  <mailSettings>
    <smtp from="myEmail@gmail.com" deliveryMethod="Network" >
      <network host="smtp.gmail.com" port="587" 
        userName="myEmail@gmail.com" password="myPassword"/>
    </smtp>
  </mailSettings>
</system.net>


推荐答案

您需要 client.EnableSsl = true;

检查本网站的代码:通过Gmail发送电子邮件


以下是使用您的Google帐户从ASP.NET页面发送HTML电子邮件的示例。
(此设置可以轻松地用于通过需要身份验证的任何其他SMTP服务器发送消息)。
注意:代码片段假设您在页面上有一个Label组件,名称为lblMsg,将向发送电子邮件的用户显示
成功/失败消息。
(但是这可以很容易地改变)。

Here is an example on how to send HTML email from your ASP.NET page using your Google account. (This setup can be easily used to send messages via any other SMTP server that requires authentication). Note: the code snippet assumes you have a Label component on Page with named lblMsg that will show success/failure message to the user that is sending email. (But this can be easily changed).

   SmtpClient client = new SmtpClient();
   client.DeliveryMethod = SmtpDeliveryMethod.Network;
   client.EnableSsl = true;
   client.Host = "smtp.gmail.com";
   client.Port = 587;

   // setup Smtp authentication
   System.Net.NetworkCredential credentials = 
       new System.Net.NetworkCredential("your_account@gmail.com", "yourpassword");
   client.UseDefaultCredentials = false;
   client.Credentials = credentials;                

   MailMessage msg = new MailMessage();
   msg.From = new MailAddress("your_account@gmail.com");
   msg.To.Add(new MailAddress("destination_address@someserver.com"));

   msg.Subject = "This is a test Email subject";
   msg.IsBodyHtml = true;
   msg.Body = string.Format("<html><head></head><body><b>Test HTML Email</b></body>");

   try
   {
       client.Send(msg);
       lblMsg.Text = "Your message has been successfully sent.";
   }
   catch (Exception ex)
   {
       lblMsg.ForeColor = Color.Red;
       lblMsg.Text = "Error occured while sending your message." + ex.Message;
   }


这篇关于通过gmail发送Asp.Net电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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