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

发布于 2024-10-23 18:51:57 字数 1755 浏览 1 评论 0 原文

可能的重复:
使用 C# 通过 Gmail SMTP 服务器发送电子邮件

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

如何使用 smtp.gmail.com 和我的 GMail 帐户发送电子邮件?如果我的电子邮件是 Web.config 中添加什么内容以及编写哪些代码data-cfemail="e084928193948f8d81898ca0878d81898cce838f8d">[电子邮件受保护] 并且我的密码是 password

谢谢


编辑

当我尝试以下演示程序时:

class Program {
    static void Main(string[] args) {
        var client = new SmtpClient("smtp.gmail.com", 587) {
            Credentials = new NetworkCredential("[email protected]", "puzzlehunters111"),
            EnableSsl = true
        };
        client.Send("[email protected]", "[email protected]", "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 个)都给了我相同的异常。我能用它做什么呢?

Possible Duplicate:
Sending email through Gmail SMTP server with C#

I would like my ASP.NET MVC application to send some standard letters to users of the web site. For testing purposes I have no local SMTP server and my provider has non that I know of. So I have to use public services like GMail's SMTP.

How do I send e-mail using smtp.gmail.com and my GMail account? What exactly should I put to Web.config and whot to code provided my e-mail is [email protected] and my password is password?

Thank you


EDIT

When I try following demo program:

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

It fails with exception. Most inner exception carries this message:

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

Also any all present answers (3 earliest) give me the same exception. What can I do with that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暗喜 2024-10-30 18:51:57

这是我过去使用过的一个类:

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 = "[email protected]";
GMailer.GmailPassword = "password";

用法:

GMailer mailer = new GMailer();
mailer.ToEmail = "[email protected]";
mailer.Subject = "Email Subject Line";
mailer.Body = "This is a test message";
mailer.IsHtml = false;
mailer.Send();

Here's a class I've used in the past:

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

The class needs to be configured in your Application_Start event:

GMailer.GmailUsername = "[email protected]";
GMailer.GmailPassword = "password";

Usage:

GMailer mailer = new GMailer();
mailer.ToEmail = "[email protected]";
mailer.Subject = "Email Subject Line";
mailer.Body = "This is a test message";
mailer.IsHtml = false;
mailer.Send();
柳若烟 2024-10-30 18:51:57

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

This could be a firewall issue. The firewall on the server your code is running on could be blocking traffic on TCP port 587. It is also possible that it is being blocked in the network infrastructure between your server and the Internet.

折戟 2024-10-30 18:51:57

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

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

    }

}

Use the simple utility class as below:

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

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文