简体   繁体   English

使用.net中的SmtpClient发送邮件

[英]Sending mail using SmtpClient in .net

I am unable to send the mail using smtp client. 我无法使用smtp客户端发送邮件。 here is the code: 这是代码:

SmtpClient client=new SmtpClient("Host");
client.Credentials=new NetworkCredential("username", "password");
MailMessage mailMessage = new MailMessage();
mailMessage.from="sender@gmail.com";
mailMessage.To.Add("recipient@gmail.com");
mailMessage.body="body";
mailMessage.subject="subject";
client.Send(mailMessage);

The problem is that when I use this code in ASP.NET application, I do not receive any mails. 问题是,当我在ASP.NET应用程序中使用此代码时,我不会收到任何邮件。 When in asp.net I change the from mail address to username given in NetworkCredential, I receive mails. 在asp.net中,我将从邮件地址更改为NetworkCredential中给出的用户名,我收到邮件。

But in C# windows application, I can get emails, even if sender's email address is not valid. 但是在C#windows应用程序中,即使发件人的电子邮件地址无效,我也可以收到电子邮件。

I figured out that setting the SmtpClient Credentials property before setting the ' UseDefaultCredentials = false ' UseDefaultCredentials = false causes the credentials to be ignored. 我发现在设置'UseDefaultCredentials = false'之前设置SmtpClient Credentials属性UseDefaultCredentials = false会导致忽略凭据。

Fails: 失败:

SmtpClient smtp = new SmtpClient;
smtp.Credentials = new NetworkCredential("user","pass");
smtp.UseDefaultCredentials = false;

Works: 作品:

SmtpClient smtp = new SmtpClient;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("user","pass");

Go figure. 去搞清楚。

It means your mail server does not allow Mail-Relay. 这意味着您的邮件服务器不允许邮件中继。 Your mail server only allows you to send mail from authenticated email-id as username. 您的邮件服务器只允许您从经过身份验证的电子邮件ID发送邮件作为用户名。 Generally this is done to prevent mails being sent as different identities other than the authenticated one. 通常这样做是为了防止邮件作为除经过身份验证的身份之外的不同身份发送。

Try this : 试试这个 :

MailMessage mail = new MailMessage("emailfrom","emailto");

mail.From = new MailAddress("emailfrom");
mail.Subject = txtsbjct.Text;
string Body = txtmsg.Text;
mail.Body = Body;

mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential("youremail", "yourpassword");

smtp.EnableSsl = true;
smtp.Send(mail);
txtemail.Text = "";
txtmsg.Text = "";
txtsbjct.Text = "";
Label1.Text = "your email has been send";
mail = null;
smtp = null;

The short answer : Do not use .net the internal SMTP client class - use MailKit . 简短的回答: 不要使用.net内部SMTP客户端类 - 使用MailKit

The long answer : 答案很长:

  1. It is marked as obsolete in MS docs 在MS文档中标记为已过时

[System.Obsolete("SmtpClient and its network of types are poorly designed, we strongly recommend you use https://github.com/jstedfast/MailKit and https://github.com/jstedfast/MimeKit instead")] public class SmtpClient : IDisposable [System.Obsolete(“SmtpClient及其网络类型设计不佳,我们强烈建议您使用https://github.com/jstedfast/MailKithttps://github.com/jstedfast/MimeKit代替”)公共课SmtpClient:IDisposable

  1. Use the oss lib MailKit . 使用oss lib MailKit
  2. Elaboration : Because you will face its deficiencies sooner rather than later. 详细说明:因为你将很快而不是后来面对它的缺陷。 Read here or read around the internet. 在这里阅读或阅读互联网。
  3. Here is an example for sending plain messages and multipart messages. 以下是发送纯文本邮件和多部分邮件的示例。

I intentionally did not copy-paste the examples from the docs here because the documentation may change over time and it is better to read . 我故意没有复制粘贴文档中的示例,因为文档可能会随着时间的推移而改变,最好阅读。

void sendEmail(string strFrom
                        , string strTo
                        , string strSubject
                        , string strBody)
   {

       MailMessage objMailMessage = new MailMessage();
       System.Net.NetworkCredential objSMTPUserInfo =
           new System.Net.NetworkCredential();
       SmtpClient objSmtpClient = new SmtpClient();

       try
       {
           objMailMessage.From = new MailAddress(strFrom);
           objMailMessage.To.Add(new MailAddress(strTo));
           objMailMessage.Subject = strSubject;
           objMailMessage.Body = strBody;

           objSmtpClient = new SmtpClient("172.0.0.1"); /// Server IP
           objSMTPUserInfo = new System.Net.NetworkCredential
           ("User name", "Password","Domain");
           objSmtpClient.Credentials = objSMTPUserInfo;
           objSmtpClient.UseDefaultCredentials = false;
           objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

           objSmtpClient.Send(objMailMessage);
       }
       catch (Exception ex)
       { throw ex; }

       finally
       {
           objMailMessage = null;
           objSMTPUserInfo = null;
           objSmtpClient = null;
       }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM