c# - 发送 Gmail 电子邮件

标签 c# email gmail

我正在尝试通过 C# 应用程序(控制台)发送电子邮件。实际上,我希望它能够向任何类型的电子邮件发送电子邮件,但目前如果我能将其发送到 Gmail 帐户,我会很高兴。

我暂时只想将其发送到 Gmail 帐户?

整个程序:

namespace EmailAddress
{
    class Program
    {
        static void Main(string[] args)
        {
            Program test = new Program();
            test.email_send();
        }

        public void email_send()
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6d050a2d0a000c0401430e0200" rel="noreferrer noopener nofollow">[email protected]</a>");
            mail.To.Add("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2f47486f48424e4643014c4042" rel="noreferrer noopener nofollow">[email protected]</a>");
            mail.Subject = "Test Mail - 1";
            mail.Body = "Your attachment is accessible on your computer!";

            System.Net.Mail.Attachment attachment;
            attachment = new System.Net.Mail.Attachment("g:/example1.txt");
            mail.Attachments.Add(attachment);

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("your <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d5b8b4bcb995b2b8b4bcb9fbb6bab8" rel="noreferrer noopener nofollow">[email protected]</a>", "your password");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);
        }
    }
}

新代码:不会挂起,但不会将消息发送到收件箱

static void Main(string[] args)
        {
            Program test = new Program();
            //test.email_send();
            test.CreateTestMessage4();
        }

        public void email_send()
        {
            MailMessage mail = new MailMessage();
            SmtpClient smtp = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec8bac8b818d8580c28f8381" rel="noreferrer noopener nofollow">[email protected]</a>");
            mail.To.Add("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="593e193e34383035773a3634" rel="noreferrer noopener nofollow">[email protected]</a>");
            mail.Subject = "Test Mail - 1";
            mail.Body = "Your attachment is accessible on your computer!";

            System.Net.Mail.Attachment attachment;

            attachment = new System.Net.Mail.Attachment("g:\\example1.txt");
            mail.Attachments.Add(attachment);//list of attachements

            smtp.Port = 587;//google standard -- most of the time wouldn't not set
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Credentials = new System.Net.NetworkCredential("*","*");

            smtp.Send(mail);

            Console.WriteLine("-- Sending Email --");
            Console.ReadLine();
        }

有人可以尝试一下这段代码并看看它是否有效。由于某种原因,这不起作用,所以我希望有人对此有新的看法。我只是在类实例的主方法中调用它。

 public void email_send()
        {
            MailMessage mail = new MailMessage();
            SmtpClient smtp = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("your email address");
            mail.To.Add("your email address");
            mail.Subject = "Test Mail - 1";
            mail.Body = "Your attachment is accessible on your computer!";

            System.Net.Mail.Attachment attachment;

            attachment = new System.Net.Mail.Attachment("g:\\example1.txt");
            mail.Attachments.Add(attachment);//list of attachements

            //smtp.Port = 587;//google standard -- most of the time wouldn't not set
            //smtp.EnableSsl = true;
            //smtp.UseDefaultCredentials = true;
            //smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            //smtp.Credentials = new System.Net.NetworkCredential("your address", 
                                                                 "your password");

            smtp.Port = 587;
            smtp.Host = "smtp.gmail.com";
            smtp.UseDefaultCredentials = false;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("*", "*"); smtp.Send(mail);

            Console.WriteLine("-- Sending Email --");
            Console.ReadLine();
        }

最佳答案

您的代码已接近:

MailMessage mail = new MailMessage();
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com")) {
    mail.From = new MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cba3aabeadafa4beac8baca6aaa2a7e5a8a4a6" rel="noreferrer noopener nofollow">[email protected]</a>");
    mail.To.Add("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="731b120615171c061433141e121a1f5d101c1e" rel="noreferrer noopener nofollow">[email protected]</a>");
    mail.Subject = "Test Mail - 1";
    mail.Body = "Your attachment is accessible on your computer!";

    System.Net.Mail.Attachment attachment;

    attachment = new System.Net.Mail.Attachment("g:\\example1.txt");
    mail.Attachments.Add(attachment);

    smtp.Port = 587;
    smtp.EnableSsl = true;
    smtp.UseDefaultCredentials = false;
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.Credentials = new System.Net.NetworkCredential("your <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cfa2aea6a38fa8a2aea6a3e1aca0a2" rel="noreferrer noopener nofollow">[email protected]</a>", "your password");

    smtp.Send(mail);
}

现在,关于您的一些其他问题。

端口 587 仅供 Google 使用。通常,您连接的邮件服务器会告诉您 SMTP 邮件使用哪个端口; Google 说有 587 个。

对于通过其他服务器发送,您通常不会设置端口号。

旁注:

  1. SmtpClient 实现 IDisposable 接口(interface)。因此,应该将其包含在 using 子句中,以确保完成后正确处理它。除非您使用 .SendAsync() 方法。
  2. 出于两个原因,我将变量名称从 SmtpServer 更改为 smtp。首先,命名约定。建议方法内的变量采用驼峰式大小写(对于ExampleThis)。其次,SmtpServer 是一个用词不当,因为该对象是一个 SmtpClient。这些是非常不同的事情,错误命名是不好的做法。

关于c# - 发送 Gmail 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20672022/

相关文章:

c# - 身份 2.2.1 中具有角色列表的用户列表

php - 在 Web 上创建电子邮件表单时的安全注意事项

php - 如何防止浏览器用 JavaScript 替换文本字段中的电子邮件?

html - 内容与表格底部不对齐

HTML 电子邮件 : How do I remove spacing between 2 tables? Gmail 特别

java - Cursor 是否有独立的数据,或者它只是一个指向数据的指针?

c# - 更新时从 GridView 内的下拉列表中获取选定的值

c# - 如何更新超表中的数据

c# - 在 .NET 中的 JSON 响应中嵌入外键对象

ruby-on-rails - 是否已成功安装 action_mailer_optional_tls 以使用 GMail 发送?