簡體   English   中英

發送Gmail電子郵件

[英]Sending a gmail email

我正在嘗試通過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();
        }

有人可以嘗試此代碼,看看它是否有效。 出於某種原因,這是行不通的,所以我想對此有所了解。 我只是在main方法中為類的實例調用此方法。

 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("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. 我將變量名稱從SmtpServersmtp的原因有兩個。 首先,命名約定。 建議將方法內部的變量設置為駝峰式(例如ExampleThis)。 其次,由於對象是SmtpClient ,因此SmtpServer是錯誤的名稱。 這些是完全不同的事情,錯誤命名是錯誤的做法。

您可以嘗試一下嗎,它對我有用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM