如何使用GMail的SMTP服务器从ASP.NET站点发送电子邮件?

时间:2011-03-19 18:05:35

标签: c# asp.net web-config smtp

  

可能重复:
  Sending email through Gmail SMTP server with C#

我希望我的ASP.NET MVC应用程序向网站用户发送一些标准信件。出于测试目的,我没有本地SMTP服务器,我的提供商没有我所知道的。所以我必须使用GMail的SMTP等公共服务。

如何使用smtp.gmail.com和我的GMail帐户发送电子邮件?如果我的电子邮件为Web.config且密码为drastomail@gmail.com,我应该准确地将password和whot编入代码?

谢谢


修改

当我尝试按照演示程序时:

class Program {
    static void Main(string[] args) {
        var client = new SmtpClient("smtp.gmail.com", 587) {
            Credentials = new NetworkCredential("puzzlehunters@gmail.com", "puzzlehunters111"),
            EnableSsl = true
        };
        client.Send("puzzlehunters@gmail.com", "puzzlehunters@gmail.com", "test", "testbody");
        Console.WriteLine("Sent");
        Console.ReadLine();
    }
}

失败但有例外。大多数内部异常都带有这样的信息:

No connection could be made because the target machine actively refused it 209.85.227.109:587

任何所有现在的答案(最早的3个)都给我同样的例外。我该怎么办?

3 个答案:

答案 0 :(得分:3)

这是我过去使用的课程:

namespace MyApp
{
    public class GMailer
    {
        public static string GmailUsername { get; set; }
        public static string GmailPassword { get; set; }
        public static string GmailHost { get; set; }
        public static int GmailPort { get; set; }
        public static bool GmailSSL { get; set; }

        public string ToEmail { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public bool IsHtml { get; set; }

        static GMailer()
        {
            GmailHost = "smtp.gmail.com";
            GmailPort = 25; // Gmail can use ports 25, 465 & 587; but must be 25 for medium trust environment.
            GmailSSL = true;
        }

        public void Send()
        {
            SmtpClient smtp = new SmtpClient();
            smtp.Host = GmailHost;
            smtp.Port = GmailPort;
            smtp.EnableSsl = GmailSSL;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new NetworkCredential(GmailUsername, GmailPassword);

            using (var message = new MailMessage(GmailUsername, ToEmail))
            {
                message.Subject = Subject;
                message.Body = Body;
                message.IsBodyHtml = IsHtml;
                smtp.Send(message);
            }
        }
    }
}

需要在Application_Start事件中配置类:

GMailer.GmailUsername = "you@gmail.com";
GMailer.GmailPassword = "password";

用法:

GMailer mailer = new GMailer();
mailer.ToEmail = "someone@somewhere.com";
mailer.Subject = "Email Subject Line";
mailer.Body = "This is a test message";
mailer.IsHtml = false;
mailer.Send();

答案 1 :(得分:2)

这可能是防火墙问题。运行代码的服务器上的防火墙可能会阻塞TCP端口587上的流量。它也可能在服务器和Internet之间的网络基础结构中被阻止。

答案 2 :(得分:0)

使用如下的简单实用程序类:

using System.IO;
using System.Net.Mail;
using System.Text;
using System.Net;
public sealed class Emailer
{
    private Emailer()
    {
    }

    public static void SendMail(string subject, string to, 
        string from = null, string body = null, Stream attachment = null,
        int port = 25, string host = "localhost", bool isBodyHtml = true)
    {
        MailMessage mailMsg = new MailMessage();
        mailMsg.From = new MailAddress(from);
        mailMsg.To.Add(to);
        mailMsg.Subject = subject;
        mailMsg.IsBodyHtml = isBodyHtml;
        mailMsg.BodyEncoding = Encoding.UTF8;
        mailMsg.Body = body;
        mailMsg.Priority = MailPriority.Normal;

        //Message attahment
        if (attachment != null)
            mailMsg.Attachments.Add(new Attachment(attachment, "my.text"));

        // Smtp configuration
        SmtpClient client = new SmtpClient();
        client.Credentials = new NetworkCredential("YOUR_GMAIL_USER_NAME", "PASSWORD");
        client.UseDefaultCredentials = true;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.Port = port; //use 465 or 587 for gmail           
        client.Host = host;//for gmail "smtp.gmail.com";
        client.EnableSsl = false;

        MailMessage message = mailMsg;

        client.Send(message);

    }

}