如果满足某些条件,我正试图从我的asp.net网页发送电子邮件。
这是我的代码。
SmtpClient smtpClient = new SmtpClient();
NetworkCredential basicCredential =
new NetworkCredential("username", "password");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("from@yourdomain.com");
smtpClient.Host = "mail.mydomain.com";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
message.From = fromAddress;
message.Subject = "your subject";
//Set IsBodyHtml to true means you can send HTML email.
message.IsBodyHtml = true;
message.Body = "<h1>your message body</h1>";
message.To.Add("to@anydomain.com");
try
{
smtpClient.Send(message);
}
catch(Exception ex)
{
//Error, could not send the message
Response.Write(ex.Message);
}
我没有分享服务器详细信息。我正试图从我的Outlook电子邮件地址发送它。但是,我一直收到错误: 发送邮件失败 。
答案 0 :(得分:1)
试试这段代码
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 25);
smtpClient.Credentials = new System.Net.NetworkCredential("youremailid@gmail.com", "yourPassword");
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
MailMessage mail = new MailMessage();
//Setting From , To and CC
mail.From = new MailAddress("fromemailid@gmail.com", "MyWeb Site");
mail.To.Add(new MailAddress("toemailid@gmail.com"));
mail.CC.Add(new MailAddress("ccemailid@gmail.com"));
mail.Subject = "your subject";
mail.IsBodyHtml = true;
mail.Body = "<h1>your message body</h1>";
smtpClient.Send(mail);
感谢,
答案 1 :(得分:0)
要发送邮件,您无法使用开发服务器端口。
您必须使用邮件服务器的smtp服务器端口。
对于smtp.live.com,您应该使用端口25 or 587
对于以下代码,请指定VALID登录详细信息(email/password)
client.Credentials = new System.Net.NetworkCredential(
@"email_account",
@"email_password");