本文介绍了如何通过C#在Windows应用程序中发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

My code:

private void button1_Click(object sender, EventArgs e)
      {

          try
          {
              MailMessage mail = new MailMessage();
              mail.To.Add("[email protected]");
              mail.To.Add(txtto.Text.ToString());
              mail.From = new MailAddress("[email protected]");
              mail.Subject = "School Name";
              string Body = txtmsg.Text;
              mail.IsBodyHtml = true;

              SmtpClient smtp = new SmtpClient("localhost", 25);
              smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
              smtp.Credentials = new System.Net.NetworkCredential
                   ("[email protected]", "rima001");
              smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
              smtp.EnableSsl = true;
              smtp.Send(mail);
              MessageBox.Show("Mail Send");
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }



我的App.Config:



My App.Config :

<?xml version="1.0" encoding="utf-8" ?>

<configuration>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="PickupDirectoryFromIis">
        <network defaultCredentials="true" host="localhost" port="25"/>
      </smtp>
    </mailSettings>
  </system.net>
</configuration>



错误:发送邮件失败



Error: Failure sending mail

推荐答案



这篇关于如何通过C#在Windows应用程序中发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 11:03