发送Gmail电子邮件

时间:2013-12-19 01:47:09

标签: 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("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();
        }

2 个答案:

答案 0 :(得分:2)

您的代码已关闭:

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有两个原因。首先,命名约定。建议将方法中的变量设置为驼峰(forExampleThis)。其次,SmtpServer是一个误称,因为对象是SmtpClient。那些是非常不同的东西,错误的做法只是不好的做法。

答案 1 :(得分:2)

你能尝试一下吗,它对我有用:

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");
相关问题