2013-12-19 281 views
2

我想通过和C#应用程序(控制台)发送电子邮件。其实我希望它发送一封电子邮件到任何类型的电子邮件,但暂时如果我能把它发送到一个Gmail帐户,我会很高兴。发送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("[email protected]"); 
      mail.To.Add("[email protected]"); 
      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 [email protected]", "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("[email protected]"); 
      mail.To.Add("[email protected]"); 
      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(); 
     } 
+2

发送_TO的account_和发送_via的account_是完全不同的。你的问题似乎在混合这两件事。你能澄清你遇到的问题吗? –

+1

@Doug Hauf请保留每个帖子一个问题。 – kaptan

+0

关于你的新代码。我从来没有看到你调用'email_send()'方法。尝试在'Program test = new Program();'后立即调用'test.email_send();' – mason

回答

2

你的代码是接近:

MailMessage mail = new MailMessage(); 
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com")) { 
    mail.From = new MailAddress("[email protected]"); 
    mail.To.Add("[email protected]"); 
    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 [email protected]", "your password"); 

    smtp.Send(mail); 
} 

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

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

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

侧笔记:

  1. SmtpClient实现IDisposable接口。所以它应该包含在一个使用条款中,以确保在完成时妥善处理它。除非你正在使用.SendAsync()方法。
  2. 由于两个原因,我将变量名从SmtpServer更改为smtp。首先,命名约定。一个方法内的变量被推荐为骆驼案例(for Example)。其次,由于对象是SmtpClient,SmtpServer是一个误称。这些是非常不同的事情,错误的事情是不好的做法。
+0

这是我刚刚收到的错误,是不是必须有用GMail设置的密码。SMTP服务器需要安全连接或客户端未通过身份验证。服务器响应是:5.5.1需要身份验证。在 –

+0

了解更多@DougHauf:好的。这就是'smtp.Credentials = ...'行的用途。用''[email protected]“'和''你的密码''用你的实际Gmail密码替换''你的[email protected]'''。它会使用你的凭证。 – NotMe

+0

@DougHauf永远不要把你的密码放到这样的公共网站上。现在,如果我愿意,我可以登录到您的Gmail帐户。我可以登录到其他网站,因为您可能使用了相同的密码。您需要立即更改您使用相同密码的任何网站的gmail密码和密码。 – mason

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 [email protected]", "your password");