如何通过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.[^]