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

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

问题描述

我试图从ASP.Net使用code和下面的配置通过Gmail发送电子邮件。不幸的是它似乎并不奏效,它也不会引发错误消息。有没有在服务器日志或邮件的IIS邮件文件夹,我甚至查的垃圾桶从地址,看看邮件结束了那里。任何帮助将是非常美联社preciated。

C#栏目

 公共无效SendFeedback()
    {
        字符串emailFrom = this.Email.Text;        消息MAILMESSAGE新= MAILMESSAGE();
        //这里是一个重要的组成部分:
        message.From =新的MailAddress(emailFrom,梅勒);
        //在这里是因为从地址config文件的定义是多余的部分
        //在我的例子。但既然你不使用.config文件,你会需要它。
        message.Subject =www.gumpshen.com - 网站查询;
        message.IsBodyHtml = TRUE;
        message.Body =的String.Format(名称= {0},手机= {1},Name.Text,Phone.Text);
        message.Body + = Environment.NewLine;
        message.Body + = Environment.NewLine;
        message.Body + = ProjectDetails.Text; ;        VAR的客户=新SmtpClient();        尝试
        {
            client.Send(消息);

这是配置部分:

 < system.net>
  < mailSettings>
    <从SMTP =myEmail@gmail.comdeliveryMethod =网络>
      <网络主机=smtp.gmail.com端口=587
        的userName =myEmail@gmail.com密码=MYPASSWORD/>
    < / SMTP>
  < / mailSettings>
< /system.net>


解决方案

您需要 client.EnableSsl = TRUE;

检查从本网站的code:<一href=\"https://web.archive.org/web/20130531014149/http://www.aspdotnetfaq.com/Faq/How-to-send-HTML-Email-from-ASP-NET-using-your-Gmail-account.aspx\">Email通过Gmail


  

下面是一个关于如何使用您的谷歌帐户的ASP.NET页面发送HTML电子邮件的例子。
     (这种设置可以很容易地通过使用需要身份验证的任何其他SMTP服务器发送消息)。
     注:code段假定你有一个页面标签组件名为lblMsg,将显示
     成功/失败消息发送到发送电子邮件用户。
     (但是,这可以很容易地改变)。

  SmtpClient客户端=新SmtpClient();
   client.DeliveryMethod = SmtpDeliveryMethod.Network;
   client.EnableSsl = TRUE;
   client.Host =smtp.gmail.com;
   client.Port = 587;   //设置SMTP认证
   System.Net.NetworkCredential凭证=
       新System.Net.NetworkCredential(your_account@gmail.com,你的密码);
   client.UseDefaultCredentials = FALSE;
   client.Credentials =凭证;   味精MAILMESSAGE新= MAILMESSAGE();
   msg.From =新的MailAddress(your_account@gmail.com);
   msg.To.Add(新MailAddress(destination_address@someserver.com));   msg.Subject =这是一个测试电子邮件主题;
   msg.IsBodyHtml =真;
   msg.Body =的String.Format(&LT; HTML&GT;&LT; HEAD&GT;&LT; /头&GT;&LT;身体GT;&LT; B&GT;测试HTML电子邮件与LT; / B&GT;&LT; / BODY&gt;中);   尝试
   {
       client.Send(MSG);
       lblMsg.Text =您的信息已经发送成功。
   }
   赶上(异常前)
   {
       lblMsg.ForeColor = Color.Red;
       lblMsg.Text =在发送消息时发生错误。 + ex.Message;
   }


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# Section

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

This is the Config section:

<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>

解决方案

You need client.EnableSsl=true;

Check the code from this site: Email via Gmail

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天全站免登陆