我正在尝试制作一个电子邮件页面部分,我在这个论坛中找到了一篇文章和一个答案,我在下面的代码中使用但是仍然说发送邮件失败,我几乎找不到原因?我需要协助找到解决方法,为什么我的代码无法发送电子邮件。这里我的代码在下面供您参考。请指教......谢谢。
protected void Button1_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
client.Host = "smtp.live.com"; //Im not sure about this,I just copy it from the article
client.Port = 4548; //This is my ASP.Net Development Server Port,Im not sure also if this is correct.
client.Credentials = new System.Net.NetworkCredential(
@"email_account",
@"email_password"); //Im not sure about this code if this correct, I just copy it
client.EnableSsl = true;
// create message
MailMessage message = new MailMessage();
message.From = new MailAddress(TextBox4.Text, "Mackmellow"); //Textbox4 is my email address
message.To.Add(new MailAddress(TextBox1.Text, "Mackmellow")); // Textbox1 is the email add I want to send
message.Subject = TextBox2.Text; //Textbox2 is my subject
message.Body = TextBox3.Text; // Textbox3 is my message
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
message.SubjectEncoding = System.Text.Encoding.UTF8;
// send message
try
{
client.Send(message);
}
catch (SmtpException ex)
{
Response.Write(ex.Message);
}
finally
{
// Clean up.
message.Dispose();
}
请纠正我的密码并告诉我我错过了什么?提前谢谢......
答案 0 :(得分:2)
要发送邮件,您无法使用开发服务器端口。
您必须使用邮件服务器的smtp服务器端口。
对于smtp.live.com,您应该使用端口25或587
对于以下代码,请指定VALID登录详细信息(电子邮件/密码)
client.Credentials = new System.Net.NetworkCredential(
@"email_account",
@"email_password");
答案 1 :(得分:0)
仅提供G-mail帐户ID。然后检查它。我认为SmtpClient
不接受任何其他域名邮件地址。所以你只能放gmail
地址,然后检查它。
修改
请参阅此示例和其他答案。
Error sending mail from Asp.Net/C#
how to send email in asp.net to any destination eg yahoo,gmail,hotmail etc c# code
答案 2 :(得分:-2)
试试这个。
SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
var mail = new MailMessage();
mail.From = new MailAddress("email@hotmail.com");
mail.To.Add("ToGmail.com");
mail.Subject = "Your Sub";
mail.IsBodyHtml = true;
string htmlBody;
htmlBody = "HTML code";
mail.Body = htmlBody;
SmtpServer.Port = 587;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("email@hotmail.com", "YourPassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);