在 asp.net 中通过 C# 发送电子邮件
Send Email through c# in asp.net
下面是我从我的邮箱发送电子邮件的代码,但我收到了一个错误,请帮忙!
Error: Email not sent!System.Net.Mail.SmtpException: The operation has timed out. at System.Net.Mail.SmtpClient.Send(MailMessage message)
at
_Default.Button1_Click(Object sender, EventArgs e)
try{
MailMessage mailmessage = new MailMessage();
mailmessage.To.Add(TextBox3.Text);
mailmessage.From=new MailAddress("sadiazar05@gmail.com");
mailmessage.Subject = "User SignUp";
mailmessage.Body = "Hello You're registered!";
SmtpClient smtpclient = new SmtpClient("smtp.gmail.com",465);
mailmessage.Priority = MailPriority.High;
smtpclient.Timeout = 60000;
smtpclient.Send(mailmessage);
Response.Write("Email sent successfully!");
}
catch(Exception exp)
{
Response.Write("Email not sent!" +exp);
}
}
您可以尝试使用25端口,如果不行您可以尝试587端口:
SmtpClient smtpclient = new SmtpClient("smtp.gmail.com",The Other Port);
尝试this code并启用 SSL。
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
MailAddress from = new MailAddress("YourGmailUserName@gmail.com", "[ Your full name here]");
MailAddress to = new MailAddress("your recipient e-mail address", "Your recepient name");
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test e-mail message sent using gmail as a relay server ";
message.Subject = "Gmail test email with SSL and Credentials";
NetworkCredential myCreds = new NetworkCredential("YourGmailUserName@gmail.com", "YourPassword", "");
client.Credentials = myCreds;
client.Send(message);
你可以这样试试:
try{
// message details
MailMessage mailmessage = new MailMessage();
mailmessage.To.Add(TextBox3.Text);
mailmessage.From=new MailAddress("sadiazar05@gmail.com");
mailmessage.Subject = "User SignUp";
mailmessage.Body = "Hello You're registered!";
mailmessage.Priority = MailPriority.High;
//smtp Client details
smtpclient.UseDefaultCredentials = False
smtpclient.Credentials = New Net.NetworkCredential("email", "password")// here you have to give your username and password
smtpclient.Port = 587 // default port for gmail
smtpclient.EnableSsl = True
smtpclient.Host = "smtp.gmail.com"
smtpclient.Timeout = 60000;
smtpclient.Send(mailmessage);
Response.Write("Email sent successfully!");
}
catch(Exception exp)
{
Response.Write("Email not sent!" +exp);
}
}
587 是 Gmail 端口
smtpclient.Port = 587
SMTP 默认使用 TCP 端口 25。邮件提交协议相同,但使用端口 587。受 SSL 保护的 SMTP 连接,称为 SMTPS,默认使用端口 465(非标准,但有时出于遗留原因使用).
下面是我从我的邮箱发送电子邮件的代码,但我收到了一个错误,请帮忙!
Error: Email not sent!System.Net.Mail.SmtpException: The operation has timed out. at System.Net.Mail.SmtpClient.Send(MailMessage message) at _Default.Button1_Click(Object sender, EventArgs e)
try{
MailMessage mailmessage = new MailMessage();
mailmessage.To.Add(TextBox3.Text);
mailmessage.From=new MailAddress("sadiazar05@gmail.com");
mailmessage.Subject = "User SignUp";
mailmessage.Body = "Hello You're registered!";
SmtpClient smtpclient = new SmtpClient("smtp.gmail.com",465);
mailmessage.Priority = MailPriority.High;
smtpclient.Timeout = 60000;
smtpclient.Send(mailmessage);
Response.Write("Email sent successfully!");
}
catch(Exception exp)
{
Response.Write("Email not sent!" +exp);
}
}
您可以尝试使用25端口,如果不行您可以尝试587端口:
SmtpClient smtpclient = new SmtpClient("smtp.gmail.com",The Other Port);
尝试this code并启用 SSL。
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
MailAddress from = new MailAddress("YourGmailUserName@gmail.com", "[ Your full name here]");
MailAddress to = new MailAddress("your recipient e-mail address", "Your recepient name");
MailMessage message = new MailMessage(from, to);
message.Body = "This is a test e-mail message sent using gmail as a relay server ";
message.Subject = "Gmail test email with SSL and Credentials";
NetworkCredential myCreds = new NetworkCredential("YourGmailUserName@gmail.com", "YourPassword", "");
client.Credentials = myCreds;
client.Send(message);
你可以这样试试:
try{
// message details
MailMessage mailmessage = new MailMessage();
mailmessage.To.Add(TextBox3.Text);
mailmessage.From=new MailAddress("sadiazar05@gmail.com");
mailmessage.Subject = "User SignUp";
mailmessage.Body = "Hello You're registered!";
mailmessage.Priority = MailPriority.High;
//smtp Client details
smtpclient.UseDefaultCredentials = False
smtpclient.Credentials = New Net.NetworkCredential("email", "password")// here you have to give your username and password
smtpclient.Port = 587 // default port for gmail
smtpclient.EnableSsl = True
smtpclient.Host = "smtp.gmail.com"
smtpclient.Timeout = 60000;
smtpclient.Send(mailmessage);
Response.Write("Email sent successfully!");
}
catch(Exception exp)
{
Response.Write("Email not sent!" +exp);
}
}
587 是 Gmail 端口
smtpclient.Port = 587
SMTP 默认使用 TCP 端口 25。邮件提交协议相同,但使用端口 587。受 SSL 保护的 SMTP 连接,称为 SMTPS,默认使用端口 465(非标准,但有时出于遗留原因使用).