本文介绍了使用smtp gmail服务器在asp.net中发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在学习使用gmail smtp服务器在asp.net中发送电子邮件..但是有未处理的异常。
i am just learning to send email in asp.net using gmail smtp server.. but having unhandled exception.
Unable to connect to the SErver.
这里的代码是什么问题
here is the code whats the problem
private void button1_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
mail.To.Add(textBox1.Text);
mail.From = new MailAddress("[email protected]");
mail.Subject = "Email using Gmail";
string Body = "Hi, this mail is to test sending mail" +
"using Gmail in ASP.NET";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential
("[email protected]", "helloworld");
smtp.EnableSsl = true;
smtp.Send(mail);
}
}
推荐答案
public static Boolean SendingMail(string From, string To, string Subject, string Body)
{
try
{
MailMessage m = new MailMessage("Uma<[email protected]>", To);
m.Subject = Subject;
m.Body = Body;
m.IsBodyHtml = true;
m.From = new MailAddress(From);
m.To.Add(new MailAddress(To));
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
NetworkCredential authinfo = new NetworkCredential("[email protected]","password");
smtp.UseDefaultCredentials = false;
smtp.Credentials = authinfo;
smtp.Send(m);
return true;
}
catch (Exception ex)
{
return false;
}
}
public string SendMail(string toList, string from, string ccList, string subject, string body)
{
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(toList);
if (ccList != null && ccList != string.Empty)
message.CC.Add(ccList);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
smtpClient.Host = "smtp.gmail.com"; // We use gmail as our smtp client
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential("Your Gmail User Name", "Your Gmail Password");
smtpClient.Send(message);
msg = "Successful<BR>";
}
catch (Exception ex)
{
msg = ex.Message;
}
return msg;
}
参考链接: - []
或看看 []。
和 []解决完成发送邮件任务的许多问题。
Reference Link :- Sending Email using Gmail SMTP server[^]
or have a look there[^].
and the CP search[^] many question with solution to accomplishment of sending mail task.
这篇关于使用smtp gmail服务器在asp.net中发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!