如何通过C#在Windows应用程序中发送邮件 [英] How can I send mail in Windows Application by C#

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

问题描述

我的代码:

My code:

private void button1_Click(object sender, EventArgs e)
      {

          try
          {
              MailMessage mail = new MailMessage();
              mail.To.Add("shi01715@gmail.com");
              mail.To.Add(txtto.Text.ToString());
              mail.From = new MailAddress("shishir64092@gmail.com");
              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
                   ("shishir64092@gmail.com", "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>



错误:发送邮件失败

有人可以告诉我在Windows应用程序中发送邮件的问题在哪里吗?请帮助我.



Error: Failure sending mail

Can anybody tell me where is the problem for sending mail in windows application ? Please help me.

推荐答案

Gmail的发送端口是587,而不是25.
Gmail''s send port is 587 not 25.


看看在此提示上:使用C#发送带有或不带有C#的电子邮件附件:通用例程. [
Have a look at this tip: Sending an Email in C# with or without attachments: generic routine.[^]


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆