发送 Gmail 电子邮件

本文关键字:电子邮件 Gmail 发送 | 更新日期: 2024-10-30 23:47:50

我正在尝试通过 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("hg@gmail.com");
            mail.To.Add("hg@gmail.com");
            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 mail@gmail.com", "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("g@gmail.com");
            mail.To.Add("g@gmail.com");
            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();
        }

发送 Gmail 电子邮件

你的代码很接近:

MailMessage mail = new MailMessage();
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com")) {
    mail.From = new MailAddress("haufdoug@gmail.com");
    mail.To.Add("haufdoug@gmail.com");
    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 mail@gmail.com", "your password");
    smtp.Send(mail);
}

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

端口 587 仅供 Google 使用。 通常,您连接的邮件服务器会告诉您用于SMTP邮件的端口;谷歌说他们的是587。

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

旁注:

  1. SmtpClient 实现了 IDisposable 接口。 因此,它应该包装在 using 子句中,以确保它在完成时得到正确处置。 除非您使用的是.SendAsync()方法。
  2. 我将变量名称从 SmtpServer 更改为 smtp有两个原因。 首先,命名约定。 方法中的变量建议为驼峰大小写(例如)。 其次,SmtpServer 用词不当,因为对象是SmtpClient。 这些都是非常不同的事情,错误命名只是不好的做法。

你能试试这个吗,它对我有用:

SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Port = 587;
SmtpServer.Host = smtp.gmail.com;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.EnableSsl = true; 
SmtpServer.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");