在 C# 中从 smtp gmail 发送电子邮件
sending email from smtp gmail in c#
我正在尝试从我的网络应用程序发送电子邮件。我同样使用 smtp gmail
。但由于某些原因,我无法发送邮件。我尝试了 25,465,587 之类的端口号,但 none 其中的端口号正在工作....
public static void SendMail(string host, string senderName, string frmAddress, string toAddress, int port, string subject, string messageText)
{
String smtpHost, port1;
smtpHost = ConfigurationManager.AppSettings["smtphost"].ToString();
port1 = ConfigurationManager.AppSettings["smtpport"].ToString();
SmtpClient mailClient = new SmtpClient(smtpHost, Convert.ToInt16(port1));
mailClient.EnableSsl = true;
NetworkCredential cred = new NetworkCredential();
cred.UserName = ConfigurationManager.AppSettings["username"].ToString();
cred.Password = ConfigurationManager.AppSettings["password"].ToString();
mailClient.Credentials = cred;
mailClient.UseDefaultCredentials = false;
//mailClient.Credentials = cred;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
try
{
MailAddress fromAddress = new MailAddress(frmAddress, senderName);
message.From = fromAddress;
message.To.Add(toAddress);
message.Subject = subject;
message.IsBodyHtml = false;
message.Body = messageText;
mailClient.Send(message);
//Response.Write("<script language='javascript' type='text/javascript'>window.alert('Email Successfully Sent.');</script>");
}
catch
{
//Response.Write("<script language='javascript' type='text/javascript'>window.alert('Send Email Failed '" + ex.Message + ");</script>");
}
}
调用此方法:
protected void btnMail_Click1(object sender, EventArgs e)
{
string from = txtEmail.Text;
string to = "my gmail id";
string message = taMessage.InnerText;
Global.SendMail(ConfigurationManager.AppSettings["smtphost"].ToString(), txtName.Text, from, to, Convert.ToInt32(ConfigurationManager.AppSettings["smtpport"]), "Enquiry", taMessage.InnerText);
}
web.config 设置:
<appSettings>
<add key="smtphost" value="smtp.gmail.com"/>
<!--<add key="smtphost" value="103.231.41.229"/>-->
<add key="smtpport" value="587"/>
<!--<add key="port" value="25"/>-->
<add key="username" value="my gmail id/>
<add key="password" value="my password"/>
<add key="From" value="my gmail id"/>
</appSettings>
异常:SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 身份验证
这是一个安全问题,默认情况下 Gmail 会阻止自定义应用程序访问您的邮件帐户。您可以将其设置为接受来自您的应用程序的登录。
登录邮件后,
https://www.google.com/settings/security/lesssecureapps
更新:
msg.IsBodyHtml = true;
试试这个:-
protected void Button1_Click(object sender, EventArgs e)
{
// body
//subject
//reciever id
sendmail(recieverID, subject, body);
}
发送邮件的方法
protected void sendmail(string reciever, string subject, string body)
{
string senderID = "sender email id";
string pwd = "password ";
string result = "Message Sent Successfully";
try
{
SmtpClient smtpClient = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new System.Net.NetworkCredential(senderID, pwd),
Timeout = 30000
};
MailMessage msg = new MailMessage(senderID, reciever, subject, body);
msg.IsBodyHtml = true;
smtpClient.Send(msg);
}
catch
{
result = "Error Sending Mail";
}
Response.Write(result );
}
或
你也可以通过这个link
我正在尝试从我的网络应用程序发送电子邮件。我同样使用 smtp gmail
。但由于某些原因,我无法发送邮件。我尝试了 25,465,587 之类的端口号,但 none 其中的端口号正在工作....
public static void SendMail(string host, string senderName, string frmAddress, string toAddress, int port, string subject, string messageText)
{
String smtpHost, port1;
smtpHost = ConfigurationManager.AppSettings["smtphost"].ToString();
port1 = ConfigurationManager.AppSettings["smtpport"].ToString();
SmtpClient mailClient = new SmtpClient(smtpHost, Convert.ToInt16(port1));
mailClient.EnableSsl = true;
NetworkCredential cred = new NetworkCredential();
cred.UserName = ConfigurationManager.AppSettings["username"].ToString();
cred.Password = ConfigurationManager.AppSettings["password"].ToString();
mailClient.Credentials = cred;
mailClient.UseDefaultCredentials = false;
//mailClient.Credentials = cred;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
try
{
MailAddress fromAddress = new MailAddress(frmAddress, senderName);
message.From = fromAddress;
message.To.Add(toAddress);
message.Subject = subject;
message.IsBodyHtml = false;
message.Body = messageText;
mailClient.Send(message);
//Response.Write("<script language='javascript' type='text/javascript'>window.alert('Email Successfully Sent.');</script>");
}
catch
{
//Response.Write("<script language='javascript' type='text/javascript'>window.alert('Send Email Failed '" + ex.Message + ");</script>");
}
}
调用此方法:
protected void btnMail_Click1(object sender, EventArgs e)
{
string from = txtEmail.Text;
string to = "my gmail id";
string message = taMessage.InnerText;
Global.SendMail(ConfigurationManager.AppSettings["smtphost"].ToString(), txtName.Text, from, to, Convert.ToInt32(ConfigurationManager.AppSettings["smtpport"]), "Enquiry", taMessage.InnerText);
}
web.config 设置:
<appSettings>
<add key="smtphost" value="smtp.gmail.com"/>
<!--<add key="smtphost" value="103.231.41.229"/>-->
<add key="smtpport" value="587"/>
<!--<add key="port" value="25"/>-->
<add key="username" value="my gmail id/>
<add key="password" value="my password"/>
<add key="From" value="my gmail id"/>
</appSettings>
异常:SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 身份验证
这是一个安全问题,默认情况下 Gmail 会阻止自定义应用程序访问您的邮件帐户。您可以将其设置为接受来自您的应用程序的登录。
https://www.google.com/settings/security/lesssecureapps
更新:
msg.IsBodyHtml = true;
试试这个:-
protected void Button1_Click(object sender, EventArgs e)
{
// body
//subject
//reciever id
sendmail(recieverID, subject, body);
}
发送邮件的方法
protected void sendmail(string reciever, string subject, string body)
{
string senderID = "sender email id";
string pwd = "password ";
string result = "Message Sent Successfully";
try
{
SmtpClient smtpClient = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new System.Net.NetworkCredential(senderID, pwd),
Timeout = 30000
};
MailMessage msg = new MailMessage(senderID, reciever, subject, body);
msg.IsBodyHtml = true;
smtpClient.Send(msg);
}
catch
{
result = "Error Sending Mail";
}
Response.Write(result );
}
或
你也可以通过这个link