用C#发送电子邮件?
我正在尝试发送邮件,但此代码引发错误“发送失败”用C#发送电子邮件?,c#,C#,我正在尝试发送邮件,但此代码引发错误“发送失败” 请告诉我解决方案?如果您有其他解决方案,请告诉我?gmail是否为smtp使用了不同的端口,而不是默认的25端口 您可能需要更改连接到的端口,我认为对于google来说,如果启用SSL,应该是465或587 还要检查您是否有可能干扰的防病毒软件。AV软件停止定制软件发送电子邮件是很常见的。可能还有一些防火墙不允许您通过所需的端口。我正在写一个控制台应用程序,请使用此示例进行尝试。 使用您的凭据发送至地址、发件人地址、密码、正文 using Sys
请告诉我解决方案?如果您有其他解决方案,请告诉我?gmail是否为smtp使用了不同的端口,而不是默认的25端口
您可能需要更改连接到的端口,我认为对于google来说,如果启用SSL,应该是465或587
还要检查您是否有可能干扰的防病毒软件。AV软件停止定制软件发送电子邮件是很常见的。可能还有一些防火墙不允许您通过所需的端口。我正在写一个控制台应用程序,请使用此示例进行尝试。 使用您的凭据发送至地址、发件人地址、密码、正文
using System;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace TestingConsole
{
class Program
{
static void Main(string[] args)
{
try
{
string to = "to@domain.com";
string from = "from@gmail.com";
string from_pwd = "mypassword";
string subject = "Sample Mail testing";
string body = "Wow this is testing body";
MailMessage mM = new MailMessage();
mM.From = new MailAddress(from);
mM.To.Add(to);
mM.Subject = subject;
mM.Body = body;
mM.IsBodyHtml = false;
mM.Priority = MailPriority.High;
SmtpClient sC = new SmtpClient("smtp.gmail.com");
sC.Port = 587;
sC.Credentials = new NetworkCredential(from, from_pwd);
sC.EnableSsl = true;
sC.Send(mM);
}
catch (Exception e)
{
Console.WriteLine(e.Message + " " + e.StackTrace);
}
}
}
}
您得到的确切异常是什么?根据验证过程,远程证书无效。我得到了这个例外
string to = "";
string from = "";
string from_pwd = "";
MailMessage mM = new MailMessage();
mM.From = new MailAddress(from);
mM.To.Add(to);
mM.Subject = subject;
mM.Body = body;
mM.IsBodyHtml = true;
mM.Priority = MailPriority.High;
SmtpClient sC = new SmtpClient("smtp.gmail.com");
sC.Port = 587;
sC.Credentials = new NetworkCredential(from, from_pwd );
sC.EnableSsl = true;
sC.Send(mM);
using System;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace TestingConsole
{
class Program
{
static void Main(string[] args)
{
try
{
string to = "to@domain.com";
string from = "from@gmail.com";
string from_pwd = "mypassword";
string subject = "Sample Mail testing";
string body = "Wow this is testing body";
MailMessage mM = new MailMessage();
mM.From = new MailAddress(from);
mM.To.Add(to);
mM.Subject = subject;
mM.Body = body;
mM.IsBodyHtml = false;
mM.Priority = MailPriority.High;
SmtpClient sC = new SmtpClient("smtp.gmail.com");
sC.Port = 587;
sC.Credentials = new NetworkCredential(from, from_pwd);
sC.EnableSsl = true;
sC.Send(mM);
}
catch (Exception e)
{
Console.WriteLine(e.Message + " " + e.StackTrace);
}
}
}
}