C#电子邮件未收到SMTP

时间:2016-08-26 06:04:04

标签: c# .net visual-studio email smtp

我正在尝试使用C#以编程方式发送电子邮件,但我找不到我所遗漏的内容。这是相同的代码

string SendEmail(string toAddress, string subject, string body, string senderID)
        {
            string result = "Message Sent Successfully..!!";
            const string senderPassword = "mypassword";
            try
            {
                SmtpClient smtp = new SmtpClient
                {
                    Host = "smtp.gmail.com",
                    Port = 587,
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
                    Timeout = 30000,
                };
                MailMessage message = new MailMessage(senderID, toAddress, subject, body);
                smtp.Send(message);
            }
            catch (SmtpException e)
            {
                result = "Smtp status code " + e.StatusCode;
            }
            catch (Exception ex)
            {
                result ="Error sending email " +ex.ToString();
            }
            return result;
        } 

我试过看到结果变量里面的值有两个值

  1. Smtp状态代码一般失败
  2. 操作已超时//如果我们评论代码捕获(SmtpException e)
  3. 如果我查看msdn网站上的SMTP状态码"一般失败" smtp status code interpretation 我们会发现状态代码意味着无法找到主机,我尝试了nslookup和主机" smtp.gmail.com"可以通过telnet访问。

    我在节目中缺少什么?

3 个答案:

答案 0 :(得分:0)

以下是使用c#以编程方式发送邮件的代码。它在我的机器上工作正常。

    try
    {
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

        mail.From = new MailAddress("email");
        mail.To.Add("email");
        mail.Subject = "Subject";
        mail.Body = "Message";
        SmtpServer.Port = 587;
        SmtpServer.Credentials = new System.Net.NetworkCredential("email", "password");
        SmtpServer.EnableSsl = true;

        SmtpServer.Send(mail);
        return true;
    }
    catch (Exception ex)
    {
        return "false";
    }

答案 1 :(得分:0)

   MailMessage mail = new MailMessage();
   mail.From = new MailAddress("frommail", "fromText");
   mail.To.Add(toEMailId);
   mail.Subject = "TestSubject";
   mail.IsBodyHtml = true;
   mail.BodyEncoding = System.Text.Encoding.UTF8;
   mail.Body = "Test mailBody";
   //SMTPServer
   SmtpClient SmtpServer = new SmtpClient("yourSMTPServer");
   //SMTPPort
   SmtpServer.Port = Convert.ToInt32("PostNumber");
   SmtpServer.UseDefaultCredentials = false;
   //Set SMTP Username and Password
   SmtpServer.Credentials = new             System.Net.NetworkCredential("SMTPUserName","SMTPPassword");
  //EnableSSL
  SmtpServer.EnableSsl = false;
  SmtpServer.Send(mail);

答案 2 :(得分:0)

这是我的代码,你可以试试。

public static bool SendEmail(string subject, MailAddress from, List<string> to,List<string> cc, string body, Attachment attachment)
{
    try
    {

        MailMessage mail = new MailMessage();
        mail.Subject = subject;
        mail.From = from;
        foreach (var item in to)
            mail.To.Add(item);
        foreach (var item in cc)
            mail.CC.Add(item);
        mail.IsBodyHtml = true;
        mail.Body = body;
        if (attachment != null)
            mail.Attachments.Add(attachment);

        SmtpClient mailClient = new SmtpClient();
        mailClient.Port = 587;//maybe 25
        mailClient.Host = "smtp.gmail.com";
        mailClient.EnableSsl = true;
        mailClient.Credentials = new NetworkCredential("EmailAddress", "EmailPassword");
        //mailClient.Credentials = new NetworkCredential("a@gmail.com", "1234");


        try
        {
            mailClient.Send(mail);
        }
        catch (Exception ex)
        {
            return false;
        }

        return true;
    }
    catch { return false; }
}